Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushButton Emits Signal Multiple Times in a Single Click.
Forum Updated to NodeBB v4.3 + New Features

QPushButton Emits Signal Multiple Times in a Single Click.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phamy1289
    wrote on last edited by Phamy1289
    #1

    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->setFont(myTitleFont);
        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.
    
        blocker.unblock();
    }
    

    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.setIcon(QMessageBox::Critical);
                messBox.setText("Failed to generate schedule advice");
                messBox.exec();
                return;
            }
        }
    }
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Phamy1289

        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->setFont(myTitleFont);
            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.
        
            blocker.unblock();
        }
        

        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.setIcon(QMessageBox::Critical);
                    messBox.setText("Failed to generate schedule advice");
                    messBox.exec();
                    return;
                }
            }
        }
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Phamy1289
          wrote on last edited by Phamy1289
          #4

          @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.

          JonBJ 1 Reply Last reply
          0
          • P Phamy1289

            @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.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @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.

            P 1 Reply Last reply
            0
            • JonBJ JonB

              @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.

              P Offline
              P Offline
              Phamy1289
              wrote on last edited by Phamy1289
              #6

              @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
              {
                  Q_OBJECT
              
              public:
                  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);
                  }
              
              
              private:
                  std::shared_ptr<QNetworkAccessManager> mManager;
              };
              
              }
              

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

              SGaistS 1 Reply Last reply
              0
              • P Phamy1289

                @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
                {
                    Q_OBJECT
                
                public:
                    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);
                    }
                
                
                private:
                    std::shared_ptr<QNetworkAccessManager> mManager;
                };
                
                }
                

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

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                std::shared_ptr

                Why ?

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

                SendPostRequest

                Check everywhere where this might be called.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  Phamy1289
                  wrote on last edited by
                  #8

                  @SGaist There is only one instance of the SendPostRequest.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Phamy1289
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved