Is (QStandardItemModel) Table column Selected iterator - help



  • NooB here.
    I am filling a QtableView on a form by loading data etc into a QStandardItemModel. I'd like to be able to iterate over the table columns to determine which columns the user has selected (clicked & hi-lighted) the header. I'm lost at how to approach this. from what I've read QItemSelectionModel, QModelIndex and IsSelected may be involved. Any help here will be thunderously applauded. Here's a snip

    QStandardItemModel *model = new QStandardItemModel(6,3,this);
    
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column 1 Header")));
    model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column 2 Header")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("Column 3 Header")));
    
    ui->tblFileData->setModel(model);
    
    for (int i = 0; i < model->columnCount(); i++)
    {
        int j = ui->tblFileData->columnAt(0);
        qDebug() << "That value is " << j;
    }
    

    thanks - mike


  • Lifetime Qt Champion

    Hi,

    You can use the QItemSelectionModel from the associated view.



  • from what I've read QItemSelectionModel, QModelIndex and IsSelected may be involved

    Good job!

    const auto selectedIndexes = ui->tblFileData->selctionModel()->selectedIndexes();
    for(auto&& singleIndex : selectedIndexes){
    qDebug() << "row: " << singleIndex.row() << " col: " << singleIndex.column()  << " data: " << singleIndex.data();
    }
    


  • I'm actually looking to create a list of the selected columns. Here's what I have:

        QString del = ";";
        QString values = "";
        QString value;
        int col;
        const auto selectedIndexes = ui->tblFileData->selectionModel()->selectedIndexes();
        for(auto&& singleIndex : selectedIndexes)
        {
            //qDebug() << "row: " << singleIndex.row() << " col: " << singleIndex.column()  << " data: " << singleIndex.data();
            col = singleIndex.column();
            value = QString::number(col);
            qDebug() << "col = " << col << " value = " << value;
            // delete duplicates
            if (!values.contains(value))
            {
                values.append(value);
                values.append(del);
            }
            qDebug() << " values " << values;
        }
    }
    
    // if all 3 columns have been selected I get 0;1;2;
    // I'll need to strip the trailing ";"
    
    /* There's got to be a better way but this works */
    

    Thanks to all!



  • @mmikeinsantarosa said in Is (QStandardItemModel) Table column Selected iterator - help:

    There's got to be a better way

    QSet<int> valuesSet;
    const auto selectedIndexes = ui->tblFileData->selectionModel()->selectedIndexes();
    for(auto&& singleIndex : selectedIndexes)
    valuesSet << singleIndex.column();
    QStringList valuesList;
    valuesList.alloc(valuesSet.size());
    Q_FOREACH(int tempValue,valuesSet){valuesList << tempValue;}
    qDebug() << valuesList.join(';');
    


  • @VRonin said in Is (QStandardItemModel) Table column Selected iterator - help:

    for(auto&& singleIndex : selectedIndexes)

    Is there a way to limit the foreach loop?
    I was thinking something like:
    for(auto&& singleIndex : selectedIndexes(0:1))

    but of course this doesn't work.

    thanks again!



  • selectedIndexes is just a QList:

    for(auto&& singleIndex : selectedIndexes)
    valuesSet << singleIndex.column();
    

    is the same as:

    for(int i=0;i<selectedIndexes.size();++i)
    valuesSet << selectedIndexes.at(i).column();
    


  • Thanks for your input. I learned a lot on this one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.