June 3, 2011

thuanvh thuanvh
Lab Rat
4 posts

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

June 3, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3137 posts

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

June 3, 2011

thuanvh thuanvh
Lab Rat
4 posts

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.

June 3, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3137 posts

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.

June 3, 2011

Andre Andre
Area 51 Engineer
6031 posts

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

  1. //piece of code that creates the checkboxes
  2. QSignalMapper* mapper = new QSignalMapper(this);
  3.  
  4. for (int i(0); i< 10; ++i) {
  5.    QCheckBox* cb = new QCheckBox(this);
  6.    // put cb in a layout or something like that
  7.    connect(cb, SIGNAL(toggled ( bool checked )), mapper, SLOT(map()));
  8.    mapper->setMapping(cb, i);
  9. }
  10.  
  11. connect(mapper, SIGNAL(mapped(int)), this, SLOT(checkboxClicked(int)));
  12.  
  13. // and later on:
  14. void MyClass::checkboxClicked(int checkboxId)
  15. {
  16.    //do something
  17. }

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 3, 2011

thuanvh thuanvh
Lab Rat
4 posts

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

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

Thank all.

December 13, 2011

kanakas kanakas
Lab Rat
28 posts

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.

  1. QSignalMapper* mapper = new QSignalMapper(this);
  2. QMultiMap<QString, QString>::iterator a = examination.find("test_id");
  3. while (a != examination.end() && a.key() == "test_id") {
  4.     QCheckBox* cb = new QCheckBox(a.value(),this);
  5.     _layout2->addWidget(cb);
  6.     xml.readNextStartElement();
  7.  ++a;
  8.   connect(cb, SIGNAL( stateChanged(int) ), mapper, SLOT(map()));
  9.   mapper->setMapping(cb, i);
  10. }
  11.  
  12. _layout2->addWidget(kataxwrisi);
  13. connect(kataxwrisi,SIGNAL(clicked()),this,SLOT(ReadXMLFile()));
  14. connect(mapper, SIGNAL(mapped(QString)), this, SLOT(checkboxClicked(QString)));
  15. }
  16.  
  17. void QXSRExample::checkboxClicked(QString checkbox){
  18.  
  19. }

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

 
  ‹‹ Handle press and release with QShortcut      How to write something at a specific line of a text file? ››

You must log in to post a reply. Not a member yet? Register here!