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?

How to get the value of a Checkbox?

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

                        how can i find the position of the unchecked item in the qlist <qmap..?

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          KA51O
                          wrote on last edited by
                          #22

                          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.

                          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