Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. tableWidget question
Qt 6.12 LTS is out! See what's new in the release blog

tableWidget question

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 7.2k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #11

    Hi
    This might come in handy
    https://wiki.qt.io/How_to_Use_QTableWidget

    J 1 Reply Last reply
    1
    • mrjjM mrjj

      Hi
      This might come in handy
      https://wiki.qt.io/How_to_Use_QTableWidget

      J Offline
      J Offline
      jenya7
      wrote on last edited by
      #12

      @mrjj said in tableWidget question:

      Hi
      This might come in handy
      https://wiki.qt.io/How_to_Use_QTableWidget

      Thank you. One more question - after populating the table - how can I check/uncheck a check box in a row or to check a check box state?

      mrjjM 1 Reply Last reply
      0
      • J jenya7

        @mrjj said in tableWidget question:

        Hi
        This might come in handy
        https://wiki.qt.io/How_to_Use_QTableWidget

        Thank you. One more question - after populating the table - how can I check/uncheck a check box in a row or to check a check box state?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #13

        @jenya7
        Hi
        There is https://doc.qt.io/qt-5/qtablewidget.html#item
        where you can access the items to check or uncheck it.

        But often you use some of the signals
        like
        https://doc.qt.io/qt-5/qtablewidget.html#itemChanged
        and there you directly get the item.

        J 1 Reply Last reply
        1
        • mrjjM mrjj

          @jenya7
          Hi
          There is https://doc.qt.io/qt-5/qtablewidget.html#item
          where you can access the items to check or uncheck it.

          But often you use some of the signals
          like
          https://doc.qt.io/qt-5/qtablewidget.html#itemChanged
          and there you directly get the item.

          J Offline
          J Offline
          jenya7
          wrote on last edited by
          #14

          @mrjj said in tableWidget question:

          @jenya7
          Hi
          There is https://doc.qt.io/qt-5/qtablewidget.html#item
          where you can access the items to check or uncheck it.

          But often you use some of the signals
          like
          https://doc.qt.io/qt-5/qtablewidget.html#itemChanged
          and there you directly get the item.

          Thank you.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jenya7
            wrote on last edited by
            #15

            I did so

            void sys::ParamLoadToTable(QTableWidget *table, int rows)
            {
            
                QTableWidgetItem item;
            
                table->setRowCount(rows);
            
                for (int i = 0; i < rows; i++)
                {
                    auto box = new QCheckBox();
                    box->setCheckState(Qt::Unchecked);
            
                    table->setCellWidget(i, 3, box);
            
                    item.setText(param_list[i].name);
                    table->setItem(i, 1, &item);
                }
            }
            

            I see all rows but column 1 (name) is empty.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jenya7
              wrote on last edited by
              #16

              OK. This way it works

              for (int i = 0; i < rows; i++)
                  {
                      auto box = new QCheckBox();
                      box->setCheckState(Qt::Unchecked);
              
                      table->setCellWidget(i, 3, box);
              
                       QTableWidgetItem  * item1 = new QTableWidgetItem();
                       table->setItem(i, 1, item1);
                     
                       QTableWidgetItem  * item2 = new QTableWidgetItem();
                        table->setItem(i, 2, item2);
              
                      item1->setText(param_list[i].name);
                      item2->setText(param_list[i].value);
                  }
              
              1 Reply Last reply
              0
              • J Offline
                J Offline
                jenya7
                wrote on last edited by
                #17

                How can I check if a check box in the 4-th column is set?

                mrjjM 1 Reply Last reply
                0
                • J jenya7

                  How can I check if a check box in the 4-th column is set?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #18

                  @jenya7
                  Hi
                  You can use
                  https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
                  to gain access to the checkbox.

                  J 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @jenya7
                    Hi
                    You can use
                    https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
                    to gain access to the checkbox.

                    J Offline
                    J Offline
                    jenya7
                    wrote on last edited by
                    #19

                    @mrjj said in tableWidget question:

                    @jenya7
                    Hi
                    You can use
                    https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
                    to gain access to the checkbox.

                     QWidget *box = table->cellWidget(i, 3);
                    

                    Then what? How do I get the check box state from the object?

                    mrjjM 1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #20

                      @jenya7 said in tableWidget question:

                      How do I get the check box state from the object?

                      Try to cast it to a QCheckBox (with e.g. dynamic_cast or Qt's specific qobject_cast) and retrieve the information.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      2
                      • J jenya7

                        @mrjj said in tableWidget question:

                        @jenya7
                        Hi
                        You can use
                        https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
                        to gain access to the checkbox.

                         QWidget *box = table->cellWidget(i, 3);
                        

                        Then what? How do I get the check box state from the object?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #21

                        @jenya7
                        Hi
                        It returns a base Widget pointer so you need to cast it.

                        QCheckBox * checkbox = qobject_cast<QCheckBox * > (table->cellWidget(i, 3)) ;
                        if (checkbox) { // must check. can be NULL
                        if ( checkbox->isChecked() ) ....
                        }

                        J 1 Reply Last reply
                        3
                        • mrjjM mrjj

                          @jenya7
                          Hi
                          It returns a base Widget pointer so you need to cast it.

                          QCheckBox * checkbox = qobject_cast<QCheckBox * > (table->cellWidget(i, 3)) ;
                          if (checkbox) { // must check. can be NULL
                          if ( checkbox->isChecked() ) ....
                          }

                          J Offline
                          J Offline
                          jenya7
                          wrote on last edited by
                          #22

                          @mrjj said in tableWidget question:

                          @jenya7
                          Hi
                          It returns a base Widget pointer so you need to cast it.

                          QCheckBox * checkbox = qobject_cast<QCheckBox * > (table->cellWidget(i, 3)) ;
                          if (checkbox) { // must check. can be NULL
                          if ( checkbox->isChecked() ) ....
                          }

                          Thank you.

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @jenya7
                            Yes, you should do so via https://doc.qt.io/qt-5/qabstractitemview.html#setItemDelegateForColumn with a https://doc.qt.io/qt-5/qstyleditemdelegate.html which you write for each of the desired column types.

                            Although you might be tempted to use https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget, you will have @VRonin descending on you like a ton of bricks here if you do so :)

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

                            @JonB said in tableWidget question:

                            you might be tempted to use https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget, you will have @VRonin descending on you like a ton of bricks here if you do so

                            And here I am!

                            Also QCheckBox is the worst excuse to use this method. It's much easier to do without a widget.

                            To activate a checkbox in a cell you can just call:

                            QTableWidgetItem  *checkBoxitem = new QTableWidgetItem;
                            checkBoxitem->setCheckState(Qt::Unchecked); // displays the checkbox as unchecked
                            checkBoxitem->setFlags(checkBoxitem->flags() | Qt::ItemIsUserCheckable); // allows the user to click on the checkbox
                            table->setItem(i, 3, checkBoxitem);
                            

                            You can then check the item state using item->checkState() or widget->model()->index(row,column).data(Qt::CheckStateRole)

                            "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
                            3

                            • Login

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