How to get the value of a Checkbox?
-
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.
-
i would appreciate it if you could help me a little more. I make a simple program and i cant find how i can use this property to get the label of the checkbox.
thanks
-
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.
-
thanks. it works.
i have another question.
I have created many checkboxes with the name cb
@QCheckBox* cb = new QCheckBox(a.value(),this);@how can i access all checkboxes with the same name "cb" to uncheck them after the user made his/her choice?
-
You can't. Or at least you cannot easily.
The easiest solution is to place your checkboxes into a list or an array and iterate over it. -
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);
}@
-
How about you use QList::append() ?
-
it was "list.append(cb);" and not "list[b].append(cb);"
thanks!!
-
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{ }
@
-
how can i find the position of the unchecked item in the qlist <qmap..?
-
First of all why do you have a QList of QMultiMaps? From what I understand just the QMultiMap would suffice.
Finding the unchecked item in the multimap is then fairly straight forward using the key you entered for that checkbox when you inserted it into the multimap (if that key has any relation to the checkbox; e.g. the label text of the checkbox).
My suggestion is you should rethink the structure of your multimap to fit your needs. For example using the label text of the checkbox as a key and a custom struct containing the information you want to store as the value. Your current keys ( "id" and "test_id" ) are useless.