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. How to get the row value from Qtable widget
Forum Updated to NodeBB v4.3 + New Features

How to get the row value from Qtable widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.1k Views 2 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.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by ManiRon
    #1

    Actually i am inserting combo box in one column on my table and when one of the combo box from the table is selected i want to get the row value . How this can be done ?

    void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
    {
    qDebug("ROW %d",index.row());
    }

    I tried something like this but it didnt give the row value where i have inserted the combo box and from other places it was returning the row value

    Gojir4G 1 Reply Last reply
    0
    • ManiRonM ManiRon

      Actually i am inserting combo box in one column on my table and when one of the combo box from the table is selected i want to get the row value . How this can be done ?

      void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
      {
      qDebug("ROW %d",index.row());
      }

      I tried something like this but it didnt give the row value where i have inserted the combo box and from other places it was returning the row value

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @ManiRon

      void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
      {
           qDebug() << "ROW: " << index.row();
          qDebug() << "TEXT: " << itemFromIndex(index).text();
           if(index.column() == COLUMN_COMBOBOX) //Check it is the "ComboBox" column
               //Do something with value
      }
      
      ManiRonM 1 Reply Last reply
      4
      • Gojir4G Gojir4

        @ManiRon

        void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
        {
             qDebug() << "ROW: " << index.row();
            qDebug() << "TEXT: " << itemFromIndex(index).text();
             if(index.column() == COLUMN_COMBOBOX) //Check it is the "ComboBox" column
                 //Do something with value
        }
        
        ManiRonM Offline
        ManiRonM Offline
        ManiRon
        wrote on last edited by ManiRon
        #3

        @Gojir4 when i click on the combo box nothing is triggred from the QTablewidget so i couldnt find the row value for that

        Gojir4G VRoninV 2 Replies Last reply
        0
        • ManiRonM ManiRon

          @Gojir4 when i click on the combo box nothing is triggred from the QTablewidget so i couldnt find the row value for that

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by Gojir4
          #4

          @ManiRon Ok, here is another approach (I didn't test the code, it mays have errors)

          //At initilization
          QComboBox *cb = new QComboBox;
          cb.setProperty("row", row);
          connect(cb, qOverload<int>(&QComboBox::currentIndexChanged), this, &MainWindow::onCbIndexChanged)
          myTableWidget->setCellWidget(row, column, cb);
          
          //...
          //On Combo Box Index changed
          void MainWindow::onCbIndexChanged(int index){
              QObject *cb = sender();
              const int row = cb->property("row");
          
              int comboBoxSelectionIndex = index;
          
              qDebug() << "ROW: " << row;
              qDebug() << "Index: " << index << "  value: " << cb.currentText();
              
          }
          

          inspired from here: https://www.qtcentre.org/threads/66573-How-to-get-signal-when-checkbox-in-table-cell-changed

          1 Reply Last reply
          4
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Just as a note
            Using a combo box delegate instead of setCellWidget have better performance and overall just feels
            nicer than a floating widget. (IMHO)
            basic example:
            https://github.com/daviddoria/QtExamples/blob/master/Delegates/ComboBoxDelegate/ComboBoxDelegate.cpp

            ManiRonM 1 Reply Last reply
            5
            • ManiRonM ManiRon

              @Gojir4 when i click on the combo box nothing is triggred from the QTablewidget so i couldnt find the row value for that

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

              @ManiRon said in How to get the row value from Qtable widget:

              when i click on the combo box nothing is triggred

              because setIndexWidget is PURE EVIL

              Follow @mrjj 's suggestion. Ther's also a wiki page on this topic: https://wiki.qt.io/Combo_Boxes_in_Item_Views

              "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
              5
              • mrjjM mrjj

                Hi
                Just as a note
                Using a combo box delegate instead of setCellWidget have better performance and overall just feels
                nicer than a floating widget. (IMHO)
                basic example:
                https://github.com/daviddoria/QtExamples/blob/master/Delegates/ComboBoxDelegate/ComboBoxDelegate.cpp

                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #7

                @mrjj I am not understanding how this works , whether any sample will be available other than this

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

                  https://doc.qt.io/qt-5/model-view-programming.html#delegate-classes

                  "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

                  • Login

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