Important: Please read the Qt Code of Conduct -

QPushButton Emits Signal Multiple Times in a Single Click.

  • I have a QPushButton when clicked grabs data from service side. I have it set up where it will grab the json data and check if its there or empty/invalid. When I click on the button the first time, it works as intended. However, subsequent clicks will call the slot a number of times equal to the number of clicks. For example: 2 clicks will call the slot twice, 3 clicks will call the slot 3 times, and so on. So, it will grab the data and then show a popup window saying "failed to generate schedule advice" the rest of the time. I used QSignalBlocker in the slot but I'm not sure if I'm using it correctly. Any help will be greatly appreciated.

    My Button:

    QPushButton *recButton = new QPushButton;
        QFont myTitleFont("times", 20);
        recButton->setText("Recommend Schedule");
        recButton->setStyleSheet("QPushButton {background:lightBlue;}");
        connect(recButton, SIGNAL(clicked()), this, SLOT(onRecommendSchedule()));

    My slot function:

    void ScheduleAdvice::onRecommendSchedule()
        QSignalBlocker blocker(sender());
        // Do some stuff and call HandlePostReply.

    My POST function:

    void ScheduleAdvice::HandlePostReply(QNetworkReply *reply)
       //Check if reply is empty or not.
      //If not empty, check if JSON is valid or not.
            //if JSON is empty or invalid, show the following popup window.
                QMessageBox messBox;
                messBox.setWindowTitle("Schedule Advice");
                messBox.setText("Failed to generate schedule advice");

  • Lifetime Qt Champion


    Where is that button created ?
    Are you sure it's created only once ?
    Are you sure the connection is done only once ?
    Are you sure the slot is connected only to the button ?

  • @Phamy1289
    Why don't you reduce this so some minimal example? 95% of the code seems to be to do with JSON, messages etc., surely cannot be related to a push button issue?

  • @SGaist It's built in the class costructor ScheduleAdvice::ScheduleAdvice. Yes it's the only button that connects to that slot. I have other buttons created by are connected to different slots.

    @JonB Sorry, I cut out most of the code out. Hopefully, it's more legible and understanding.

  • @Phamy1289
    Well there doesn't look to be anything of significance left :) If you remove the QSignalBlocker does it work or still misbehave.

    Also are you sure the connect() is executed only once? Look for any connect()s to that slot anywhere in your program.

  • @JonB It still misbehaves.

    I don't if this helps but this is the class is getting called with the postRequest. I feel like this is getting called multiple times with each subsequent click.

    #pragma once
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkRequest>
    #include <QtNetwork/QNetworkReply>
    #include <memory>
    #include <functional>
    #include <iostream>
    namespace network
    class HttpAccess : public QObject
        HttpAccess() : mManager(new QNetworkAccessManager)
        void SendPostRequest(const std::string& url, QByteArray bytePart, std::function<void(QNetworkReply*)> replyFunction)
            QNetworkRequest request(QUrl(QString::fromStdString(url)));
            QObject::connect(mManager.get(), &QNetworkAccessManager::finished, replyFunction);
            request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
            mManager->post(request, bytePart);
        std::shared_ptr<QNetworkAccessManager> mManager;

    I've checked through out the code of any other instance of the connects with that specific slot.

  • Lifetime Qt Champion

    @Phamy1289 said in QPushButton Emits Signal Multiple Times in a Single Click.:


    Why ?

    @Phamy1289 said in QPushButton Emits Signal Multiple Times in a Single Click.:


    Check everywhere where this might be called.

  • @SGaist There is only one instance of the SendPostRequest.

  • I figured it out. It wasn't the button that was the issue. It was the connection in the SendPostRequest. It keeps making a new connection to the service while already being connected to it. I just needed to add a conditional to have it only connect to it once. Sorry for all the trouble.

  • Lifetime Qt Champion

    @Phamy1289 said in QPushButton Emits Signal Multiple Times in a Single Click.:

    just needed to add a conditional to have it only connect to it once

    or do the connect in the ctor.

Log in to reply