简体   繁体   中英

Qt: either signal won't send, or slot is not called

I have two Qt classes implied in this problem: MainWindow and otaManager .
I want to send a signal from otaManager and let MainWindow call a slot when it does.
Here's MainWindow.h ( https://github.com/Kobo-InkBox/inkbox/blob/1d48a6d85ee5b0a4db86c1d9f49510d0d923b297/mainwindow.h#L104 ):

private slots:
    void openUpdateDialogOTA(bool open);
    ...

private:
    ...
    otaManager *otaManagerWindow;

MainWindow.cpp ( https://github.com/Kobo-InkBox/inkbox/blob/1d48a6d85ee5b0a4db86c1d9f49510d0d923b297/mainwindow.cpp#L979-L993 ):

...
void MainWindow::openUpdateDialogOTA(bool open) {
    if(open == true) {
        global::otaUpdate::isUpdateOta = true;
        openUpdateDialog();
    }
    else {
        ;
    }
}


void MainWindow::launchOtaUpdater() {
    otaManagerWindow = new otaManager(this);
    connect(otaManagerWindow, SIGNAL(canOtaUpdate(bool)), SLOT(openUpdateDialogOTA(bool)));
    otaManagerWindow->setAttribute(Qt::WA_DeleteOnClose);
}

otaManager.h ( https://github.com/Kobo-InkBox/inkbox/blob/1d48a6d85ee5b0a4db86c1d9f49510d0d923b297/otamanager.h#L21-L22 ):

...
signals:
    void canOtaUpdate(bool yesno);
...

otaManager.cpp ( https://github.com/Kobo-InkBox/inkbox/blob/1d48a6d85ee5b0a4db86c1d9f49510d0d923b297/otamanager.cpp#L15-L30 ):

        ...
        if(global::otaUpdate::downloadOta != true) {
            string_writeconfig("/opt/ibxd", "ota_update_check\n");
            while(true) {
                if(QFile::exists("/run/can_ota_update") == true) {
                    if(checkconfig("/run/can_ota_update") == true) {
                        emit canOtaUpdate(true);
                        qDebug() << "OTA update is available!";
                        break;
                    }
                    else {
                        emit canOtaUpdate(false);
                        qDebug() << "No OTA update available.";
                        break;
                    }
                }
            }
       ...

When compiled and running, the program passes all the conditions in otaManager and outputs the "OTA update is available." message via a QDebug call.
Though, either the slot is not being called by MainWindow when receiving the signal or it is not being sent, because the openUpdateDialogOTA function in MainWindow, even when I put some QDebug calls to see if it was even launched, does not output nor do anything.

I'm quite puzzled by all this because I did it this way for other classes in my project and it worked just fine.

You forget about receiver object:

connect(otaManagerWindow, SIGNAL(canOtaUpdate(bool)), SLOT(openUpdateDialogOTA(bool)));

Try to use new style for connect method:

connect(a, &A::OnSignal, b, &B::OnSlot);

It seems that the while(true) loop was the culprit. I still don't know why, and I wonder if it is even normal. I switched to a QTimer implementation instead, as you can see here: https://github.com/Kobo-InkBox/inkbox/blob/1f539e6bf3020d2a4069295133e0cd470efb8ca9/otamanager.cpp
The signals are now sent without any issue.

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