3

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

AngryDuck
  • 4,358
  • 13
  • 57
  • 91
  • 1
    this seems similar to your question http://stackoverflow.com/q/5927499/942596 – andre Feb 11 '14 at 14:35
  • not MY question my cheers ill take a look – AngryDuck Feb 11 '14 at 14:38
  • This also doesnt actually answer my question, i should maybe be more clear in my description. I have got my columns and rows already based on if they are hidden or not with `this->verticalHeader()->isSectionHidden(i)` so i cant really use the `SelectedIndexs()` function thats why i ask if there is a `isSelected` function for a specific row... – AngryDuck Feb 11 '14 at 14:44
  • Yes, there is such function: `QItemSelectionModel::isSelected (const QModelIndex &index)` – vahancho Feb 11 '14 at 14:48
  • Cheers that seems exactly what i need but i dont have the QModelIndes position of my row and column i just have the row and column integer values?? – AngryDuck Feb 11 '14 at 15:01
  • @AngryDuck, you can get the `QModelIndex` from your table's model: `QAbstractTableModel::index(int row, int column, ...)` – vahancho Feb 11 '14 at 15:06
  • so with use of variable from my code shown above the answer would be `if(this->selectionModel()->isSelected(this->indexAt(QPoint(row, column))))` – AngryDuck Feb 11 '14 at 15:11
  • Use http://qt-project.org/doc/qt-4.8/qabstractitemview.html#selectionModel to get list of selected items. – Dmitry Sazonov Feb 11 '14 at 15:13
  • I dont need the list of selected items please read the question i just need to check if a single row is selected or not i see on that link a function `isRowSelected` but it wants a row and a QModelIndex? what is the QModelIndex i pass thats the bit i dont get – AngryDuck Feb 11 '14 at 15:22
  • As in QModelindex &parent ..... whats parent refering to?? – AngryDuck Feb 11 '14 at 15:32

1 Answers1

8
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)
user3195397
  • 226
  • 1
  • 2
  • `QItemSelectionModel` also has a method `isRowSelected()`, but in addition to a row number it also wants `const QModelIndex &parent`, and I'm a bit mystified as to where to obtain that... – bhaller Feb 23 '20 at 18:33