简体   繁体   中英

Qt 5.3 Signals and Slots, Simple Function and Lambda Expression

I tried to Write a program using Qt 5.3 and I try to using signals and slots as practice. I wrote the following code (part of the code) :

void exitToWin()
    {
        exit(0);
    }
int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    MainWindow w;
    QMessageBox EndBox;
    QObject::connect((EndBox.button(QMessageBox::Ok)),SIGNAL(clicked()),exitToWin);
    w.show();
    EndBox.show();
    return a.exec();
}

I even change the declaration of the function to static and I checked the expression with parentheses and without them while I am writing the connect command. but although what Qt documented and what its IDE guided to. also I read here and I tested it.
Moreover I tried with lambda expression as below:

QObject::connect((EndBox.button(QMessageBox::Ok)),SIGNAL(clicked()),[=](){
    exit(0);
});

but still I receive errors indicate "No matching function call".
And after all I have to say that I am using Microsoft Windows 7.

This works on Qt 5.3:

#include <QtWidgets>

void exitToWin()
{
    exit(0);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;
    QMessageBox endBox;
    endBox.addButton(QMessageBox::Ok); // (2)
    endBox.connect(endBox.button(QMessageBox::Ok), 
                   &QAbstractButton::clicked, exitToWin); // (1)
    /* This works, too:
    endBox.connect(endBox.button(QMessageBox::Ok),
                   &QAbstractButton::clicked,
                   [] () { exit(0); });
    */
    w.show();
    endBox.show();
    return a.exec();
}

Here's why:

(1) You can use endBox 's QObject to do the connection between the QAbstractButton 's clicked signal and your exitToWin simple function. You also can't connect a SIGNAL to a simple function or a lambda, so we use the member function variety, instead.

(2) endBox doesn't actually get an OK button by default. When you mention it on line (1) in your code, it creates it, but not in time (apparently) to pass the pointer back to connect , so we create it first here.

Your code won't work for two reasons:

Firstly, QMessageBox does not have such a signal. See the documentation for the signals it does have.

Secondly, when making connections from a signal to a slot (or lambda function), you must define the function signatures, not specific values.

If a signal can pass a variety of values and you only want your slot to perform a certain function on a selection of those values (in this case, only if the value QMessageBox::Ok is passed) it is up to the slot to interrogate the values, not the connect statement.

Since the connect() method is from QObject it has to be called from a QObject child containing the Q_OBJECT macro in its declaration. Running qmake prepare the class to send signals and receive slots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM