How to bind a event handler to qtCheckbox
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.
6 replies
What you might do, is use QSignalMapper to create something like this:
- //piece of code that creates the checkboxes
- for (int i(0); i< 10; ++i) {
- // 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*.
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.
- while (a != examination.end() && a.key() == "test_id") {
- _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()));
- }
- }
for example if the first checkbox has the value “A” i want to know..
what can i do?
You must log in to post a reply. Not a member yet? Register here!



