質問

I have a QTableview that has a QTableModel set to it.

This view can be modified eg. rows/columns shifted and removed etc.

I have a function to export a table model to excel/csv which takes a QTableModel, however the model doesnt reflect the view if its been modified, so i have a function that creates a new table model based on the QTableViews current layout.

However i now want to be able to select a few rows and export only the selected, so in essence i need to just create a model based on selected rows in the view not all of them.

below shows my current loop,

// 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....

    }

so now to make only rows that are selected get added into my container i want to just check is this row selected eg.

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

Does such an isSelected function exist on QTableView Rows? if so what is it ??

Cheers

役に立ちましたか?

解決

QItemSelectionModel *select = tableview->selectionModel();

QItemSelctionModel has following calls to retrieve the list of QModelIndex.

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

From QModelIndex to col and row

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

From (row, col) to QModelIndex

QModelIndex idx = QTableModel->index(row, col)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top