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
QtWS25 Last Chance

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.
  • elidrissizakE Offline
    elidrissizakE Offline
    elidrissizak
    wrote on 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);
        }
    
    JonBJ 1 Reply Last reply
    0
    • elidrissizakE elidrissizak

      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);
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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();
      }     
      
      elidrissizakE 1 Reply Last reply
      3
      • JonBJ JonB

        @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();
        }     
        
        elidrissizakE Offline
        elidrissizakE Offline
        elidrissizak
        wrote on 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

        JonBJ 1 Reply Last reply
        0
        • elidrissizakE elidrissizak

          @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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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.

          elidrissizakE 1 Reply Last reply
          0
          • JonBJ JonB

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

            elidrissizakE Offline
            elidrissizakE Offline
            elidrissizak
            wrote on 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();
                }
            
            JonBJ 1 Reply Last reply
            0
            • elidrissizakE elidrissizak

              @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();
                  }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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.

              elidrissizakE 2 Replies Last reply
              1
              • JonBJ JonB

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

                elidrissizakE Offline
                elidrissizakE Offline
                elidrissizak
                wrote on 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
                • JonBJ JonB

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

                  elidrissizakE Offline
                  elidrissizakE Offline
                  elidrissizak
                  wrote on 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

                  • Login

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