Qt C从QTableView中获取所选行的每个单元格中的数据

Qt C从QTableView中获取所选行的每个单元格中的数据,第1张

概述有没有办法从表视图中的选定行中获取数据?我用过 QModelIndexList ids = ui-> tableView-> selectionModel() – > selectedRows();它返回所选行的索引列表.我不需要索引.我需要来自所选行的每个单元格的数据. QVariant data(const QModelIndex& index, int role) const 用于返回数据. 有没有办法从表视图中的选定行中获取数据?我用过
QModelindexList IDs = ui-> tableVIEw-> selectionModel() – > selectedRows();它返回所选行的索引列表.我不需要索引.我需要来自所选行的每个单元格的数据.解决方法
QVariant data(const QModelindex& index,int role) const

用于返回数据.如果您需要根据QModelindex行和列在此处获取数据并从某个容器中检索它,也许

std::vector<std::vector<MyData> > data;

您必须定义此类映射并在data()和setData()函数中使用它来处理与底层模型数据的交互.

或者,qabstractitemmodel和QTreeVIEw提供了将类(即TreeItem)分配给每个QModelindex的方法,因此您可以使用从QModelindex.internalPointer()函数返回的指针的static_cast来检索指向每个数据的指针:

TreeItem *item = static_cast<TreeItem*>(index.internalPointer());

那么你可以创建一些映射:

// sets the role data for the item at <index> to <value> and updates // affected TreeItems and ModuleInfo. returns true if successful// otherwise returns falsebool 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);

example of implementation

总结

以上是内存溢出为你收集整理的Qt C从QTableView中获取所选行的每个单元格中的数据全部内容,希望文章能够帮你解决Qt C从QTableView中获取所选行的每个单元格中的数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存