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. How to get the value of a Checkbox?
Forum Updated to NodeBB v4.3 + New Features

How to get the value of a Checkbox?

Scheduled Pinned Locked Moved General and Desktop
22 Posts 4 Posters 21.5k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kanakas
    wrote on last edited by
    #1

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

    }
    @

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kanakas
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kanakas
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kanakas
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kanakas
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kanakas
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kanakas
                          wrote on last edited by
                          #12

                          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

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fluca1978
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kanakas
                              wrote on last edited by
                              #14

                              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?

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fluca1978
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  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.

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    kanakas
                                    wrote on last edited by
                                    #17

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

                                    }@

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #18

                                      How about you use QList::append() ?

                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        kanakas
                                        wrote on last edited by
                                        #19

                                        it was "list.append(cb);" and not "list[b].append(cb);"

                                        thanks!!

                                        1 Reply Last reply
                                        0
                                        • K Offline
                                          K Offline
                                          kanakas
                                          wrote on last edited by
                                          #20

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

                                          @

                                          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