QTable查看如何确定是否选择了“行”

QTableView how to find out if Row is selected?

本文关键字:选择 是否 何确定 QTable      更新时间:2023-10-16

我有一个QTableview,它设置了一个QTableModel。

可以修改此视图,例如移动和删除行/列等。

我有一个函数将表格模型导出为 excel/csv,它采用 QTableModel,但是如果模型被修改,该模型不会反映视图,所以我有一个函数,可以根据 QTableViews 当前布局创建新的表格模型。

但是,我现在希望能够选择几行并仅导出所选行,因此本质上我只需要基于视图中的选定行而不是所有行创建一个模型。

下面显示了我当前的循环,

// Loop over the view's data and add it to the map for the model
for(int i = 0; i < rowIndexs.size(); ++i)
{
    // Loop over visible headers only as we are matching the view not the model
    for(int j = 0; j < headersIndexs.size(); ++j)
    {
        // Column is the logical index of the visual index of the current column, this         values is used as which column to look at in the model to get the cell data
        int column = this->horizontalHeader()->logicalIndex(headersIndexs.at(j));
        int row = this->verticalHeader()->logicalIndex(rowIndexs.at(i));
        /// add to some data container thats not important for this question....
    }

所以现在只让选定的行添加到我的容器中,我只想检查这行是否被选中,例如。

    if(this->VerticalHeader()->at(row).isSelected)
    { 
         // Add it to the container
    }
    else
    {
         // Ignore it and just go to the next one
    }

QTableView 行上是否存在这样的isSelected函数? 如果是这样,那是什么?

干杯

QItemSelectionModel *select = tableview->selectionModel();

QItemSelctionModel 具有以下调用来检索 QModelIndex 的列表。

QModelIndexList selectedColumns ( int row = 0 ) const
QModelIndexList selectedIndexes () const
QModelIndexList selectedRows ( int column = 0 ) const

从 QModelIndex 到 col 和 row

int row = modelindex.row()
int col = modelindex.col()

从(行,列)到QModelIndex

QModelIndex idx = QTableModel->index(row, col)