PDA

View Full Version : Can I use arbitrary constants in a connect()?



WinchellChung
18th February 2008, 21:29
I'm pretty sure the answer is "no", but I am hoping against hope.

Say I had a widget with three QPushButtons: buttonAlice, buttonBetty, and buttonCindy. What I want to do is write in my enclosing QObject something like this:



connect(buttonAlice, SIGNAL(clicked(bool)), this, SLOT(handleButtonClick(1)));
connect(buttonBetty, SIGNAL(clicked(bool)), this, SLOT(handleButtonClick(2)));
connect(buttonCindy, SIGNAL(clicked(bool)), this, SLOT(handleButtonClick(3)));

where I pass an arbitrary constant to handleButtonClick().

I fear that you all will point out that what I should do is sub-class QPushButton and add a signal which contains the appropriate constant. I'm not sure this is a viable option due to the constraints of the rest of the application.

The gory details:
I'm writing a "form engine." This is intended to display arbitrary user-defined forms and allow the user to change from form to form.

The basic engine is created from a QStackedWidget. It loads each form into a different index in the QStackedWidget.
Each form is created by the user using Qt Designer and saved as a *.ui file.
An XML file gives a list of the *.ui files, and a list of QtPushButton names of buttons whose function is to switch from one form to another. The XML file lists the name of the button and the index of the form to switch to.

The point being that the details about the buttons is not known at compile time.

So I read in each *.ui file, use QUiLoader to convert it into a widget and stuff it into QStackedWidget::addWidget(). I use a map to translate from the arbitrary form index in the XML file to the index used by the QStackedWidget. I have the names of all the magic buttons and the form indices they should change to. I get handles to the magic buttons with formWidget->findChild<QPushButton *>(buttonName). I use that in a connect statement.

But I cannot figure out how to pass a parameter to handleButtonClick(). If there was some way to put an arbitrary constant into a connect() this would solve my problem.

If I sub-class QPushButton I will have to write some kind of plug-in for Qt Designer so the user can use the new class and set the form index to use. Ugh.
Or is it possible to "promote" a child widget in a form widget into a different class of widget?

jacek
18th February 2008, 21:40
I'm pretty sure the answer is "no", but I am hoping against hope.
Yes, the answer is no, but you can take a look on QSignalMapper and QButtonGroup.

WinchellChung
18th February 2008, 22:30
Thanks! I never heard of QSignalMapper, but it looks just perfect. Thanks again.

WinchellChung
18th February 2008, 22:48
Just tried it. Worked like magic. Thank you so much