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. qtableview with checkbox in first row in all row
Forum Update on Monday, May 27th 2025

qtableview with checkbox in first row in all row

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.8k Views
  • 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.
  • E Offline
    E Offline
    elidrissizak
    wrote on 23 Aug 2020, 01:02 last edited by
    #1

    hello all , i want to creat a tableview with 2 columns checkbox and email, to checked some email and send him a mail, its more than 2 week i search on google but nothing , i have found some code to i can add checkbox in first column of all rows but i can't checkstat which checkbox is checked , sorry for my language if anyone can help me please

    this is my code :

    model = new QTableView;
    querymodel = new QSqlQueryModel();
    querymodel->setQuery("SELECT email FROM fiche_clt ");
    querymodel->insertColumn(0);
    querymodel->setHeaderData(0,Qt::Horizontal,tr(" "));
    querymodel->setHeaderData(1,Qt::Horizontal,tr("Email"));
    
    for(int p=0; p<querymodel->rowCount(); p++)
        {
            model->setIndexWidget(querymodel->index(p,0),new QCheckBox());
           querymodel->setData(querymodel->index(p,0),Qt::Unchecked,Qt::CheckStateRole);
        }
    
    J 1 Reply Last reply 23 Aug 2020, 06:56
    0
    • E elidrissizak
      23 Aug 2020, 01:02

      hello all , i want to creat a tableview with 2 columns checkbox and email, to checked some email and send him a mail, its more than 2 week i search on google but nothing , i have found some code to i can add checkbox in first column of all rows but i can't checkstat which checkbox is checked , sorry for my language if anyone can help me please

      this is my code :

      model = new QTableView;
      querymodel = new QSqlQueryModel();
      querymodel->setQuery("SELECT email FROM fiche_clt ");
      querymodel->insertColumn(0);
      querymodel->setHeaderData(0,Qt::Horizontal,tr(" "));
      querymodel->setHeaderData(1,Qt::Horizontal,tr("Email"));
      
      for(int p=0; p<querymodel->rowCount(); p++)
          {
              model->setIndexWidget(querymodel->index(p,0),new QCheckBox());
             querymodel->setData(querymodel->index(p,0),Qt::Unchecked,Qt::CheckStateRole);
          }
      
      J Offline
      J Offline
      JonB
      wrote on 23 Aug 2020, 06:56 last edited by JonB
      #2

      @elidrissizak
      You are mixing two different things: setData(querymodel->index(p,0), Qt::Unchecked, Qt::CheckStateRole) has nothing to do with model->setIndexWidget(querymodel->index(p,0),new QCheckBox());. You should avoid setIndexWidget(), and subclass the model to have flags() include Qt::ItemIsUserCheckable, or use a QStyledItemDelegate. You might look at, say, https://stackoverflow.com/questions/52324633/adding-checkbox-with-qsqlquerymodel-shown-in-qtableview-and-other-columns-are-em or https://stackoverflow.com/questions/14912192/add-widget-to-qsqlquerymodel

      If for whatever reason you do wish to stay with your setIndexWidget()

      but i can't checkstat which checkbox is checked

      for(int p=0; p<querymodel->rowCount(); p++)
      {
          QCheckBox *cb = qobject_cast<QCheckBox *>(model->indexWidget(querymodel->index(p,0)));
          if (cb != nullptr)
              qDebug() << cb->isChecked();
      }     
      
      E 1 Reply Last reply 24 Aug 2020, 15:28
      3
      • J JonB
        23 Aug 2020, 06:56

        @elidrissizak
        You are mixing two different things: setData(querymodel->index(p,0), Qt::Unchecked, Qt::CheckStateRole) has nothing to do with model->setIndexWidget(querymodel->index(p,0),new QCheckBox());. You should avoid setIndexWidget(), and subclass the model to have flags() include Qt::ItemIsUserCheckable, or use a QStyledItemDelegate. You might look at, say, https://stackoverflow.com/questions/52324633/adding-checkbox-with-qsqlquerymodel-shown-in-qtableview-and-other-columns-are-em or https://stackoverflow.com/questions/14912192/add-widget-to-qsqlquerymodel

        If for whatever reason you do wish to stay with your setIndexWidget()

        but i can't checkstat which checkbox is checked

        for(int p=0; p<querymodel->rowCount(); p++)
        {
            QCheckBox *cb = qobject_cast<QCheckBox *>(model->indexWidget(querymodel->index(p,0)));
            if (cb != nullptr)
                qDebug() << cb->isChecked();
        }     
        
        E Offline
        E Offline
        elidrissizak
        wrote on 24 Aug 2020, 15:28 last edited by
        #3

        @JonB thanks you so much for your help ! now it work, last question please, you know how i can add a checkbox in header to check/uncheck all checkboxs in qtableview ?

        this is my code if someone elyse search it

        J 1 Reply Last reply 24 Aug 2020, 15:43
        0
        • E elidrissizak
          24 Aug 2020, 15:28

          @JonB thanks you so much for your help ! now it work, last question please, you know how i can add a checkbox in header to check/uncheck all checkboxs in qtableview ?

          this is my code if someone elyse search it

          J Offline
          J Offline
          JonB
          wrote on 24 Aug 2020, 15:43 last edited by JonB
          #4

          @elidrissizak said in qtableview with checkbox in first row in all row:

          how i can add a checkbox in header to check/uncheck all checkboxs in qtableview

          This is not easy, as Qt does not have support for it. See this link https://wiki.qt.io/Technical_FAQ#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F A different approach is shown in http://codingexodus.blogspot.com/2018/05/add-checkbox-in-qtableview-header-using.html.

          E 1 Reply Last reply 24 Aug 2020, 15:51
          0
          • J JonB
            24 Aug 2020, 15:43

            @elidrissizak said in qtableview with checkbox in first row in all row:

            how i can add a checkbox in header to check/uncheck all checkboxs in qtableview

            This is not easy, as Qt does not have support for it. See this link https://wiki.qt.io/Technical_FAQ#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F A different approach is shown in http://codingexodus.blogspot.com/2018/05/add-checkbox-in-qtableview-header-using.html.

            E Offline
            E Offline
            elidrissizak
            wrote on 24 Aug 2020, 15:51 last edited by
            #5

            @JonB not problem if i can't add it in header i can just use qpushbutton if its possible ? , i have try this code but nothing

                foreach(QCheckBox* le, findChildren<QCheckBox*>()) {
                    le->clear();
                }
            
            J 1 Reply Last reply 24 Aug 2020, 15:56
            0
            • E elidrissizak
              24 Aug 2020, 15:51

              @JonB not problem if i can't add it in header i can just use qpushbutton if its possible ? , i have try this code but nothing

                  foreach(QCheckBox* le, findChildren<QCheckBox*>()) {
                      le->clear();
                  }
              
              J Offline
              J Offline
              JonB
              wrote on 24 Aug 2020, 15:56 last edited by JonB
              #6

              @elidrissizak
              You can use an external pushbutton to check/uncheck all if you wish (much simpler).

              If you're saying the code you show does nothing, check what your are findChildren()ing on. If for whatever reason findChildren() does not find the checkboxes (you did put a qDebug() in to see what it visits, didn't you?), you can use the code earlier going through the indexWidget()s in the table if that is necessary. Onw way or another you can visit all chexckboxes.

              E 2 Replies Last reply 24 Aug 2020, 16:11
              1
              • J JonB
                24 Aug 2020, 15:56

                @elidrissizak
                You can use an external pushbutton to check/uncheck all if you wish (much simpler).

                If you're saying the code you show does nothing, check what your are findChildren()ing on. If for whatever reason findChildren() does not find the checkboxes (you did put a qDebug() in to see what it visits, didn't you?), you can use the code earlier going through the indexWidget()s in the table if that is necessary. Onw way or another you can visit all chexckboxes.

                E Offline
                E Offline
                elidrissizak
                wrote on 24 Aug 2020, 16:11 last edited by
                #7

                @JonB
                i have nothing :/ can you give me example to i can setchecked qcheckbox ? thanks you

                i have try this to but nothing

                    QModelIndex index;
                    index= model->model()->index(0,0, QModelIndex());
                    modell->setData( index, Qt::Unchecked, Qt::CheckStateRole );
                

                this is my code

                    checkall= new QPushButton;
                    checkall->setFixedSize(65,65);
                    checkall->setToolTip("Annuler");
                    checkall->setFocusPolicy(Qt::NoFocus);
                   connect(checkall, SIGNAL(clicked(bool)), this, SLOT(Raz()));
                .
                
                void SendMail::Raz()
                {
                    foreach(QCheckBox* le, findChildren<QCheckBox*>()) {
                        le->setChecked(false);
                        qWarning() << le ;
                    }
                }
                
                1 Reply Last reply
                0
                • J JonB
                  24 Aug 2020, 15:56

                  @elidrissizak
                  You can use an external pushbutton to check/uncheck all if you wish (much simpler).

                  If you're saying the code you show does nothing, check what your are findChildren()ing on. If for whatever reason findChildren() does not find the checkboxes (you did put a qDebug() in to see what it visits, didn't you?), you can use the code earlier going through the indexWidget()s in the table if that is necessary. Onw way or another you can visit all chexckboxes.

                  E Offline
                  E Offline
                  elidrissizak
                  wrote on 24 Aug 2020, 16:17 last edited by
                  #8

                  @JonB its work with this code , thanks you so much for your help !

                      for(int i = 0 ; i < modell->rowCount() ; i++)
                      {
                          QModelIndex index;
                          index= model->model()->index(i,0, QModelIndex());
                          modell->setData( index, Qt::Checked, Qt::CheckStateRole );
                          model->update(index);
                      }
                  
                  1 Reply Last reply
                  0

                  3/8

                  24 Aug 2020, 15:28

                  topic:navigator.unread, 5
                  • Login

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