How to connect a forms signal to application class slot?



  • This would seem to be a very basic question but I seem to not be able to find an answer to it.

    I want to design a form in QtCreator. I want then to use the signals/slots editor to connect a widget's signal to the slot of some non-GUI class that I have created. I cannot seem to find a way to do this - the signals/slot editor does not know about any classes or object created outside of the form, even though the classes are defined in the project. How, for example, would I connect a button's clicked() signal to my class MyClass's slot activate()? I would think that I should be able to do this from within the signals/slots editor. Would someone help me here?

    Thanks



  • You need to use the connect function



  • I was thinking I should be able to connect a widget's signal to a QObject defined outside of QtDesigner - the receiver column in QtDesigner does not show anything but the widgets defined in the form. I was thinking that I should be able to direct a signal from a form object to any other slot in the project - is this not so?



  • I might be wrong, but as far as i know the signal-slot editor in Designer can't connect signals and slots from your C++ code (and not even from two different "forms") so you have to do something like this in your C++ code:
    @
    QObject::connect(pointerOfYourFormInstance, SIGNAL(yourSignal()),
    pointerToYourOtherClassInstance, SLOT(doSomething()));
    @
    You do this where you can have pointers to both QObject derived classes instances (_form _and MyClass).
    //You can connect a signal to another signal so: the clicked() signal can be connected to a public signal in your form class (what i called yourSignal()) and this is a better approach than directly exposing the button (or ui pointer) by making it public

    Read more about signals and slots "here":http://qt-project.org/doc/qt-4.8/signalsandslots.html

    If you have some implementation that doesn't work, read that page try to understand as much as you can ask if you think you didn't understand and then try to implement again - if still doesn't work show us some code



  • OK, I poked around and figured this out for myself. Here is the answer.

    I was operating under the misconception that the signals/slots editor of QT Designer would allow me to connect widget signals to any slot in the project. This is incorrect. The signals/slots editor in Qt Designer only allows you to work with the signals and slots of the form being edited. But there is a way to connect the widgets signals to a slot. One has to right click on the form's widget and then select "Goto slot..." which will then open a dialog box that allows you to choose the signal of the widget. For a button widget, right click and then choose the "clicked()" signal. Qt Designer will then add the appropriate code to both the header and implementation files of the form's widget. That is the hook you need to then call into non-GUI code.

    TextFinder.h:
    @
    #ifndef TEXTFINDER_H
    #define TEXTFINDER_H

    #include "ui_textfinder.h"
    #include "findimplementation.h"

    class TextFinder : public QWidget, private Ui::TextFinder
    {
    Q_OBJECT

    public:
    explicit TextFinder(QWidget *parent = 0);
    private slots:
    void on_findButton_clicked();
    void on_otherButton_clicked();

    private:
    FindImplementation *fi;
    };

    #endif // TEXTFINDER_H
    @

    TextFinder.cpp has the "on_[object]_[signal]..." calls. In this file, include the header of the non-GUI files that implements the action of the GUI widget:

    @
    #include "findimplementation.h"
    #include <QDebug>

    TextFinder::TextFinder(QWidget *parent) :
    QWidget(parent),fi(new(FindImplementation))
    {
    qDebug() << "TextFinder::TextFinder(QWidget *parent)";
    setupUi(this);
    }

    void TextFinder::on_findButton_clicked()
    {
    qDebug() << "TextFinder::on_findButton_clicked()";
    fi->findProcessing();
    }

    void TextFinder::on_otherButton_clicked()
    {

    }
    @

    And then implement the class that reacts to the GUI's signal - this class is not known to Qt Designer.

    FindImplementation.cpp:

    @
    #ifndef FINDIMPLEMENTATION_H
    #define FINDIMPLEMENTATION_H

    #include <QObject>

    class FindImplementation : public QObject
    {
    Q_OBJECT
    public:
    explicit FindImplementation(QObject *parent = 0);

    signals:

    public slots:
    void findProcessing(void);

    };

    #endif // FINDIMPLEMENTATION_H
    @

    FindImplementation.cpp:

    @
    #include <QDebug>
    #include "findimplementation.h"

    FindImplementation::FindImplementation(QObject *parent) :
    QObject(parent)
    {
    qDebug() << "FindImplementation::FindImplementation()";
    }

    void FindImplementation::findProcessing(void)
    {
    qDebug() << "FindImplementation::findProcessing(void)";
    }
    @

    [Edit: replaced <code> tags with @ tags for pretty formatting; mlong]


Log in to reply
 

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