Solved 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->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; } } }
-
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 ? -
@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? -
-
@Phamy1289
Well there doesn't look to be anything of significance left :) If you remove theQSignalBlocker
does it work or still misbehave.Also are you sure the
connect()
is executed only once? Look for anyconnect()
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 { 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.
-
@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.
-
@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.
-
@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.