Results 1 to 4 of 4

Thread: Can I use arbitrary constants in a connect()?

  1. #1
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Can I use arbitrary constants in a connect()?

    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:

    Qt Code:
    1. connect(buttonAlice, SIGNAL(clicked(bool)), this, SLOT(handleButtonClick(1)));
    2. connect(buttonBetty, SIGNAL(clicked(bool)), this, SLOT(handleButtonClick(2)));
    3. connect(buttonCindy, SIGNAL(clicked(bool)), this, SLOT(handleButtonClick(3)));
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can I use arbitrary constants in a connect()?

    Quote Originally Posted by WinchellChung View Post
    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.

  3. The following user says thank you to jacek for this useful post:

    WinchellChung (18th February 2008)

  4. #3
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can I use arbitrary constants in a connect()?

    Thanks! I never heard of QSignalMapper, but it looks just perfect. Thanks again.

  5. #4
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can I use arbitrary constants in a connect()?

    Just tried it. Worked like magic. Thank you so much

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.