有没有办法从表视图中获取选定行的数据?我用过
QModelIndexList ids = ui->tableView->selectionModel()->selectedRows();,它返回所选行的索引列表.我不需要索引.我需要来自所选行的每个单元格的数据.
小智 7
你可以尝试这个
int rowidx = ui->tblView->selectionModel()->currentIndex().row();
ui->txt1->setText(model->index(rowidx , 0).data().toString());
ui->txt2->setText(model->index(rowidx , 1).data().toString());
ui->txt3->setText(model->index(rowidx , 2).data().toString());
ui->txt4->setText(model->index(rowidx , 3).data().toString());
Run Code Online (Sandbox Code Playgroud)
QVariant data(const QModelIndex& index, int role) const
Run Code Online (Sandbox Code Playgroud)
用于返回数据。如果您需要获取数据,您可以根据QModelIndex行和列在此处执行此操作,并从某个容器中检索它,也许
std::vector<std::vector<MyData> > data;
Run Code Online (Sandbox Code Playgroud)
您必须定义此类映射并在函数中使用它data()来setData()处理与底层模型数据的交互。
或者QAbstractItemModel,并提供将您的类 ie 分配给 every 的QTreeView方法,因此您接下来可以使用从QModelIndex.internalPointer()函数返回的指针检索指向每个数据的指针:TreeItemQModelIndexstatic_cast
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
Run Code Online (Sandbox Code Playgroud)
那么你可以创建一些映射:
// sets the role data for the item at <index> to <value> and updates
// affected TreeItems and ModuleInfo. returns true if successful
// otherwise returns false
bool ModuleEnablerDialogTreeModel::setData(const QModelIndex & index,
const QVariant & value, int role) {
if (role
== Qt::CheckStateRole&& index.column()==ModuleEnablerDialog_CheckBoxColumn) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
Qt::CheckState checkedState;
if (value == Qt::Checked) {
checkedState = Qt::Checked;
} else if (value == Qt::Unchecked) {
checkedState = Qt::Unchecked;
} else {
checkedState = Qt::PartiallyChecked;
}
//set this item currentlyEnabled and check state
if (item->hierarchy() == 1) { // the last level in the tree hierarchy
item->mModuleInfo.currentlyEnabled = (
checkedState == Qt::Checked ? true : false);
item->setData(ModuleEnablerDialog_CheckBoxColumn, checkedState);
if (mRoot_Systems != NULL) {
updateModelItems(item);
}
} else { // every level other than last level
if (checkedState == Qt::Checked || checkedState == Qt::Unchecked) {
item->setData(index.column(), checkedState);
// update children
item->updateChildren(checkedState);
// and parents
updateParents(item);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13070 次 |
| 最近记录: |