Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connect a function to a slot (action)



  • Hi,

    First off: Qt newbie here, excuse the simple question:
    How can I hook trigger a simple function on the click of a button?
    I've added the following to my ui_main.h header file:

    #include <QtWidgets/QMessageBox>
    class Ui_MainWindow
    public:
    ...
    ...
    void setupUi((QMainWindow *MainWindow)
    {
    ...
    ...
    QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
    }
    ..
    ...
    public slots:
     void foo1(QString msg) {QMessageBox msgBox;msgBox.setText(msg);msgBox.exec();}
    };
    

    and upon click on the checkallPushButton, I would expect a dialog box to pup up with the string bar! but this isn't happening and I'm wondering why...

    Some context: I'm trying to re-use an existing ui (from someone else) but have to first learn how things exactly connect with each other and the above, is what I thought I've read on various pages on the web....
    Thanks for any help/hints to get this working the way I imagine ;)


  • Lifetime Qt Champion

    Hi @cerr,

    If you have Qt5 and C++11 compiler, using a lambda would be the simplest:

    QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
        QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
    });
    

    (I have not tried your actual message box calls, but it should world I think.

    Regards


  • Lifetime Qt Champion

    Hi
    Just a few notes.
    The connect is not valid. It should include the types of the parameters, not an actual parameter.
    like
    QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1(QString )));

    However, since clicked() signal do not have a QString as the parameter, they are not compatible.

    That kind of syntax errors can be caught using the new syntax
    https://wiki.qt.io/New_Signal_Slot_Syntax

    or simply checking the return type of connect

    bool res= QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
    if (!res)
    qDebug() << "connect didnt work"



  • @aha_1980 said in Connect a function to a slot (action):

    Hi @cerr,

    If you have Qt5 and C++11 compiler, using a lambda would be the simplest:

    QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
        QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
    }
    

    (I have not tried your actual message box calls, but it should world I think.

    Regards

    Hi
    Just a few notes.
    The connect is not valid. It should include the types of the parameters, not an actual parameter.
    like
    QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1(QString )));

    However, since clicked() signal do not have a QString as the parameter, they are not compatible.

    That kind of syntax errors can be caught using the new syntax
    https://wiki.qt.io/New_Signal_Slot_Syntax

    or simply checking the return type of connect

    bool res= QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
    if (!res)
    qDebug() << "connect didnt work"

    Thanks to both, @mrjj and @aha_1980 , both your answers did help me to get something going that will bring me further towards my end goal with this project! Your help is appreciated! Thank you!
    a small addition for whoever else will find this thread:
    @aha_1980 's solutions is missing ); at the end, i.e. the complete lambda style solution looks like:

    QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
        QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
    });
    

  • Lifetime Qt Champion

    @cerr Thanks for the feedback, I fixed it.


  • Lifetime Qt Champion

    @cerr said in Connect a function to a slot (action):

    I've added the following to my ui_main.h header file:

    You should NOT do this as ui_* files are generated and you will loose your changes next time these files are generated again!
    The correct location to do this is in the class to which your form belongs.



  • @jsulm said in Connect a function to a slot (action):

    @cerr said in Connect a function to a slot (action):

    I've added the following to my ui_main.h header file:

    You should NOT do this as ui_* files are generated and you will lose your changes next time these files are generated again!
    The correct location to do this is in the class to which your form belongs.

    Right, I've realized this too, the "hard"/stupid way... after losing some of the changes & additions (luckily it wasn't that much) :)


Log in to reply