Connect slot from QWidget



  • Hello,
    Iam nwe in QT and hope to get some help for better understanding of QT here. In VC I worked in the past much with PostMessage to call a function on a child or embeeded dialog or custom control. IN Qt I read about actions/slots and to connect them. Here is the point where I have problems with this new kind of "messages":
    How can I connect a function that is placed on a QDialog with an action that is done inside a QWidget? As example, I use a QDialog and a QWidget that shows "configuration" pages on the QDialog, similar to the Configuration sample of the QT SDK.
    Now I want that a combobox indexchange event have to call a slot in the QDialog, but all I do is not really working.

    I use:
    @connect(serverCombo, SIGNAL(currentIndexChanged(int)),
    parent, SLOT(enableApply(int)));@

    I also try:
    @connect(serverCombo, SIGNAL(currentIndexChanged(int)),
    parent, SLOT(ConfigDialog::enableApply(int)));@

    This connect is inside the QWidget that is dyanmicly created after ConfigDialog was created. I also make enableApply as a public slot on the QDialog, but the function is never called from the combobox change.
    Any idea how to connect the combobox signal to the slot on the QDialog?

    Ingo


  • Moderators

    Your description is incomplete. Does the signal and slot exist (defined with signals/ Q_SIGNAL and slots/ Q_SLOT in a header file)? When is this connect applied (maybe too late or in a wrong place)? Is the connection successful (the application will print a debug message if connection is not successful)? Are there any compilation or runtime errors?

    The first code snippet you have provided is a valid connection statement in Qt4 and 5 (the second one is not).



  • I use:
    @public slots:
    void enableApply(int index);@

    in the header file.

    @currentIndexChanged(int)@ is a common combobox signal.

    There is no error with the first connect (second crash). But this signal never happens.


  • Moderators

    That is unusual. Are you sure moc does not complain on runtime (I don't remember the exact message, but it should push something like "QObject connection failed")?

    You have not answered my other questions, too. I suspect you call connect() in a place that is not reached by the running program (a bit odd that you connect a slot in a parent widget).



  • ok, maybe your last comment helped. I connect it now on the dialog, not in the widget, and it works. I use it this way:

    @
    pagesWidget->addWidget(new ConfigurationPage);
    //Now get the combobox handle and connect
    ConfigurationPage activeSubWindow = dynamic_cast<ConfigurationPage>(pagesWidget->widget(0));
    connect(activeSubWindow->serverCombo, SIGNAL(currentIndexChanged(int)),
    this , SLOT(enableApply(int)));@

    is this the right way? Or is it possible to make it shorter?

    Ingo


  • Moderators

    I'm not sure casting is required. And in any case you can do this:
    @
    ConfigurationPage *temp = new ConfigurationPage();
    pagesWidget->addWidget(new ConfigurationPage);
    connect(temp->serverCombo, SIGNAL(currentIndexChanged(int)),
    this , SLOT(enableApply(int)));
    @

    Any way that works is correct. The question is rather whether it is a clean and right way to do ;)



  • mybe you ment:

    @ ConfigurationPage *temp = new ConfigurationPage();
    pagesWidget->addWidget(temp);
    connect(temp->serverCombo, SIGNAL(currentIndexChanged(int)),
    this , SLOT(enableApply(int)));@


  • Moderators

    Yeah, that's what I've meant.



  • Many thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.