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 bind a event handler to qtCheckbox
Forum Updated to NodeBB v4.3 + New Features

How to bind a event handler to qtCheckbox

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 11.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.
  • T Offline
    T Offline
    thuanvh
    wrote on 3 Jun 2011, 12:23 last edited by
    #1

    Hi all,
    I'm newbie with Qt.
    Now, I'm writing a program generate a list of checkbox. With each checkbox, I want to bind it with a checked event.
    Is there a way to do this? How to create a event delegate and bind a handler to a checkbox in the fly?

    Thanks.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on 3 Jun 2011, 12:25 last edited by
      #2

      Sorry, I do not understand what you want to do.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thuanvh
        wrote on 3 Jun 2011, 12:34 last edited by
        #3

        Sorry for my english.
        In fact, I want to create a list of checkbox dynamically, in the fly.
        I want that when I click any checkbox, only one function is called.
        So, is it clear? I don't know how to connect the function executing and the checkbox created.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on 3 Jun 2011, 12:43 last edited by
          #4

          So where is the problem? You create some checkboxes and connect their checked() signal to some slot... nothing tricky there.

          You might want to use a QSignalMapper or sender() to find out which checkbox has triggered the signal.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 3 Jun 2011, 12:48 last edited by
            #5

            What you might do, is use QSignalMapper to create something like this:

            @
            //piece of code that creates the checkboxes
            QSignalMapper* mapper = new QSignalMapper(this);

            for (int i(0); i< 10; ++i) {
            QCheckBox* cb = new QCheckBox(this);
            // put cb in a layout or something like that
            connect(cb, SIGNAL(toggled ( bool checked )), mapper, SLOT(map()));
            mapper->setMapping(cb, i);
            }

            connect(mapper, SIGNAL(mapped(int)), this, SLOT(checkboxClicked(int)));

            // and later on:
            void MyClass::checkboxClicked(int checkboxId)
            {
            //do something
            }
            @

            Of course, you can also choose one of the other mappings if those are more convenient for you. You can map to an int, to a QString, to a QObject* or to a QWidget*.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thuanvh
              wrote on 3 Jun 2011, 13:20 last edited by
              #6

              Thanks, I've done it.
              It's better to change "toggled ( bool checked )" to "stateChanged(int)" for checkbox.

              @connect(cb, SIGNAL( stateChanged(int) ), mapper, SLOT(map()));@

              Thank all.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kanakas
                wrote on 13 Dec 2011, 17:04 last edited by
                #7

                Hi, i am new to qt.

                I have create a same function as andre but i cant find a way the get the value of the checkbox that was clicked.

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

                _layout2->addWidget(kataxwrisi);
                connect(kataxwrisi,SIGNAL(clicked()),this,SLOT(ReadXMLFile()));
                connect(mapper, SIGNAL(mapped(QString)), this, SLOT(checkboxClicked(QString)));
                }

                void QXSRExample::checkboxClicked(QString checkbox){

                }@

                for example if the first checkbox has the value "A" i want to know..
                what can i do?

                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