9

テーブル ビューから選択した行からデータを取得する方法はありますか? QModelIndexList ids = ui->tableView->selectionModel()->selectedRows();選択した行のインデックスのリストを返すものを使用 しました。インデックスは必要ありません。選択した行のすべてのセルからのデータが必要です。

4

3 に答える 3

2
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();
于 2016-09-23T10:16:04.467 に答える
2
QVariant data(const QModelIndex& index, int role) const

データを返すために使用されています。データを取得する必要がある場合は、QModelIndex行と列に基づいてここで実行し、コンテナから取得します。おそらく

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

このようなマッピングを定義し、それをdata()およびsetData()関数で使用して、基になるモデル データとの相互作用を処理する必要があります。

または、クラス、つまり each を割り当てる方法を提供するためQAbstractItemModel、次にQModelIndex.internalPointer ()関数から返されたポインターを使用して各データへのポインターを取得できます。QTreeViewTreeItemQModelIndexstatic_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 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);

実装例

于 2013-09-28T21:50:59.977 に答える