【问题标题】:Qt C++ Get data from every cell of a selected row from a QTableViewQt C++ 从 QTableView 中获取选定行的每个单元格的数据
【发布时间】:2013-09-28 20:29:42
【问题描述】:

有没有办法从表格视图中的选定行中获取数据?我用过 QModelIndexList ids = ui->tableView->selectionModel()->selectedRows(); 返回所选行的索引列表。我不需要索引。我需要所选行的每个单元格中的数据。

【问题讨论】:

  • 使用QModelIndex::data(int role) 有意义吗?

标签: c++ qt


【解决方案1】:

你可以试试这个

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());

【讨论】:

    【解决方案2】:
    QVariant data(const QModelIndex& index, int role) const
    

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

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

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

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

    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 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);
    

    example of implementation

    【讨论】:

      【解决方案3】:
      Try this for getting data. selectedRows(0) indicates first column of selected rows, selectedRows(1) indicates second column of selected rows row likewise
      
      QItemSelectionModel *select = ui->existingtable->selectionModel();
      qDebug()<<select->selectedRows(0).value(0).data().toString();
      qDebug()<<select->selectedRows(1).value(0).data().toString();
      qDebug()<<select->selectedRows(2).value(0).data().toString();
      qDebug()<<select->selectedRows(3).value(0).data().toString();
      

      【讨论】:

        猜你喜欢
        • 2011-05-13
        • 2014-02-12
        • 2012-07-27
        • 2014-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-06
        相关资源
        最近更新 更多