How to get the value of a Checkbox?
-
My code is:
It reads a file before and it creates checkboxes with some examinations.
How can i get the value of the checkbox that was clicked?
The problem is that i dont known what code should i use in the function checkboxClicked.
Thanks.@QSignalMapper* mapper = new QSignalMapper(this);
QMultiMap<QString, QString>::iterator a = examination.find("test_id");
while (a != examination.end() && a.key() == "test_id") {
QCheckBox* cb = new QCheckBox(a.value(),this);
_layout2->addWidget(cb);
xml.readNextStartElement();
++a;
connect(cb, SIGNAL( stateChanged(int) ), mapper, SLOT(map()));
mapper->setMapping(cb, i);
}connect(kataxwrisi,SIGNAL(clicked()),this,SLOT(ReadXMLFile()));
connect(mapper, SIGNAL(mapped(QString)), this, SLOT(checkboxClicked(QString)));
}void QXSRExample::checkboxClicked(QString checkbox){
}
@ -
-
now it works, but how can i get the value of the checkbox? (the text that is writen next to the checkbox when i run the program). Now i can check is the checkbox is checked or not.
the code is:
@void QXSRExample::checkboxClicked(QWidget* checkbox){
QCheckBox cbx = qobject_cast<QCheckBox>(checkbox);
if (cbx->isChecked()){
qDebug()<<"checked";.
...
}
else{
qDebug()<<"unchecked";
...
}
}@thanks
-
The value of a checkbox is the check state, which you already have. The text next to it is just a label, which is in the text property (defined in [[doc:QAbstractButton]]).
I would advice against using the text property for business logic though. What happens if at one point in time, you decide to translate your application into another language? Instead, you could use a "dynamic property":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html#id-67f0a447-371e-4628-935b-b8e565c50d7a . These can be set both from code and from Designer.
-
The label of the checkbox is the text within it, so use the "text":http://developer.qt.nokia.com/doc/qt-4.8/qabstractbutton.html#id-69528e3c-ca67-4219-8ca5-91b54496c96c method.
-
-
The checkboxes don't have a name "cb". cb is a name of a variable that exists in the scope you declared it in, and that contains a pointer to the checkbox you created. That is something completely different. What's more, once compiled, this variable name is meaningless. It does not really exist as such, it is just a label you use during coding.
The solution is of course as fluca1978 remarks to keep all your pointers-to-checkboxes in a container (I would not use an array, use one of the Qt containers like QList instead; also note you cannot keep a QCheckBox in a container directly!) and iterate over that container.
-
I need little more help..
how can i add the checkboxes to the list?
i used this code and i get an error at line "6" "list[b]=cb" , "index out of range"@
int b=0;
QList<QCheckBox > list;
QSignalMapper mapper = new QSignalMapper(this);
QMultiMap<QString, QString>::iterator a = examinations.find("test_id");
while (a != examinations.end() && a.key() == "test_id") {
cb = new QCheckBox(a.value(),this);
list[b]=cb;
b++;_layout2->addWidget(cb); xml.readNextStartElement(); ++a; connect(cb, SIGNAL(stateChanged(int) ), mapper, SLOT(map())); mapper->setMapping(cb, cb);}@
-
hello, i have created the following function that inserts the ids (of my costumers) and the labels of the checked checkboxes into a qmultimap and then into a qlist. My problem is that when a checkbox is unchecked, i can't find a way to remove the specific "line" in the qlist
@QMultiMap<QString,QString> chosen_examinations;
QList< QMap<QString,QString> > chosen_examinations2;void QXSRExample::checkboxClicked(QWidget* checkbox){
cbx = qobject_cast<QCheckBox*>(checkbox);
label_cbx=cbx->text();if (cbx->isChecked()){ chosen_examinations.insert("id",itemId); chosen_examinations.insert("test_id",label_cbx); chosen_examinations2.append(chosen_examinations); } else{ }@