Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is (QStandardItemModel) Table column Selected iterator - help
Forum Updated to NodeBB v4.3 + New Features

Is (QStandardItemModel) Table column Selected iterator - help

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mmikeinsantarosaM Offline
    mmikeinsantarosaM Offline
    mmikeinsantarosa
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can use the QItemSelectionModel from the associated view.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        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();
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • mmikeinsantarosaM Offline
          mmikeinsantarosaM Offline
          mmikeinsantarosa
          wrote on last edited by VRonin
          #4

          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!

          VRoninV 1 Reply Last reply
          0
          • mmikeinsantarosaM mmikeinsantarosa

            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!

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @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(';');
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • mmikeinsantarosaM Offline
              mmikeinsantarosaM Offline
              mmikeinsantarosa
              wrote on last edited by
              #6

              @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!

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                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();
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • mmikeinsantarosaM Offline
                  mmikeinsantarosaM Offline
                  mmikeinsantarosa
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved