qtableview with checkbox in first row in all row
-
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); }
-
@elidrissizak
You are mixing two different things:setData(querymodel->index(p,0), Qt::Unchecked, Qt::CheckStateRole)
has nothing to do withmodel->setIndexWidget(querymodel->index(p,0),new QCheckBox());
. You should avoidsetIndexWidget()
, and subclass the model to haveflags()
includeQt::ItemIsUserCheckable
, or use aQStyledItemDelegate
. 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-qsqlquerymodelIf 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(); }
-
@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.
-
@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 reasonfindChildren()
does not find the checkboxes (you did put aqDebug()
in to see what it visits, didn't you?), you can use the code earlier going through theindexWidget()
s in the table if that is necessary. Onw way or another you can visit all chexckboxes. -
@JonB
i have nothing :/ can you give me example to i can setchecked qcheckbox ? thanks youi 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 ; } }
-
@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); }