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){

    }
    @



  • Isn't the text passed to checkboxClicked the value you are searching for?



  • No, i wrote in the function "checkboxClicked" the code
    @qDebug()<<"in";@
    but it seems that the function isnt called.



  • Myabe this is stupid, but is your i variable a text?

    @ mapper->setMapping(cb, i);@



  • oh.., i am very stupid! it was int.
    thanks a lot!!!



  • I need some more help. I have another question. in the function checkboxclicked i get the value of the checkbox with QString checkbox. how can i get the whole checkbox? I need the value and i need to check if the checkbox is checked or unchecked.

    thanks



  • QSignalMapper also supports sending a QObject* pointer as a value. You could use that instead of number you now seem to use.



  • and at "mapper->setMapping" i should use (cb,cb)?
    I changed the QString with QObject at the connect..
    but when i run the program it says "no such signal.."



  • Please just take a look at the [[doc:QSignalMapper]] documentation. It lists quite clearly what mappings are possible, but yes, you basically use
    @
    mapper->setMapping(cb, cb);
    @

    Then you can connect to the mapped(QWidget*) signal.



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.