Slot function executes on ui->show function
-
The program below executes the slot function once when the ui is shown. I attempted using the blockSignals() function on the QPlainTextEdit member of my class (e) and then showing the window but that did not work also. Can someone explain to me why the slot function executes anyway and how I can prevent the initial execution.
mainwindow.cpp
#include "mainwindow.h" #include "./ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); e = new QPlainTextEdit(); this->setCentralWidget(e); e->appendPlainText("This is a string"); e->blockSignals(true); connect(e, SIGNAL(textChanged()), this, SLOT(onTextChanged())); this->show(); e->blockSignals(false); //The connect function will connect an object with a signal to an object with a slot } MainWindow::~MainWindow() { delete ui; } void MainWindow::print(const QString& s){ } void MainWindow::onTextChanged(){ std::cout << "The text has been changed" << std::endl; }mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPlainTextEdit> #include <QWidget> #include <iostream> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void print(const QString& s); public slots: void onTextChanged(); private: Ui::MainWindow *ui; QPlainTextEdit *e; }; #endif // MAINWINDOW_Hmain.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; //const QString s = "Hello, World!"; //w.print(s); //w.show(); return a.exec(); }This example prints a string to the widget and makes it the central widget. If I had not printed a string it would still execute the slot function.
-
Your code does not do that on my system with or without the blockSignals() calls.
Please provide more detail on exactly what version of Qt, platform etc.BTW: The call to QMainWindow::show() should be outside the constructor, usually in main() where you have commented it out. Calling it inside the constructor limits reuse and potentially schedules the event on an incompletely constructed object.
-
qt version: 5.12.8
system: Ubuntu 20.04 focalThe slot function prints "This is a string" to the console and that is how I know the slot function executes. I have attached a screenshot showing that on my machine.
May I ask what your version of qt and platform is?
Also, I only provided the version and system information. What other details could be provided regarding my configuration?Thank you for the note on the call to QMainWindow::show(), I will make sure to call it in main().
Screenshot:

-
qt version: 5.12.8
system: Ubuntu 20.04 focalThe slot function prints "This is a string" to the console and that is how I know the slot function executes. I have attached a screenshot showing that on my machine.
May I ask what your version of qt and platform is?
Also, I only provided the version and system information. What other details could be provided regarding my configuration?Thank you for the note on the call to QMainWindow::show(), I will make sure to call it in main().
Screenshot:

-
@mpergand I just tried that, it didn't work. I also tried moving e->blockSignals(true); before setting e as the centralwidget. As such:
e->blockSignals(true); this->setCentralWiget(e);I also have new unknown behavior. When I click into and out of the application window the slot function is not executed, however, when I Alt-Tab into the window the slot function is executed once and the console prints out that the text has been changed. When I Alt-Tab out of the window the slot function is executed four time and the console indicates as such. This occurs whether the blockSignals() function is placed before or after the appendPlainText() and setCentralWidget().
Is this behavior specific only to my configuration?
-
Use a debugger and set a breakpoint to your slot. The slot can not be executed by the signal you connect since it's done after the appendPlainText() call and this function does not delay anything.
For me this also works fine on Linux
class MainWindow : public QMainWindow { public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { auto e = new QPlainTextEdit(); setCentralWidget(e); e->appendPlainText("This is a string"); connect(e, &QPlainTextEdit::textChanged, this, &MainWindow::onTextChanged); show(); } void onTextChanged() { std::cout << "The text has been changed" << std::endl; } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; return a.exec(); } -
qt version: 5.12.8
system: Ubuntu 20.04 focalThe slot function prints "This is a string" to the console and that is how I know the slot function executes. I have attached a screenshot showing that on my machine.
May I ask what your version of qt and platform is?
Also, I only provided the version and system information. What other details could be provided regarding my configuration?Thank you for the note on the call to QMainWindow::show(), I will make sure to call it in main().
Screenshot:

-
@mpergand I just tried that, it didn't work. I also tried moving e->blockSignals(true); before setting e as the centralwidget. As such:
e->blockSignals(true); this->setCentralWiget(e);I also have new unknown behavior. When I click into and out of the application window the slot function is not executed, however, when I Alt-Tab into the window the slot function is executed once and the console prints out that the text has been changed. When I Alt-Tab out of the window the slot function is executed four time and the console indicates as such. This occurs whether the blockSignals() function is placed before or after the appendPlainText() and setCentralWidget().
Is this behavior specific only to my configuration?
@qutie123 said in Slot function executes on ui->show function:
when I Alt-Tab into the window the slot function is executed once and the console prints out that the text has been changed. When I Alt-Tab out of the window the slot function is executed four time
This should not be happening at all. To diagnose you need to do as @Christian-Ehrlicher has said by running under debugger and placing a breakpoint on your slot to find out where it seems to be being called from.
One thought: because you use Designer you will be using auto-connect to slots by name. I wonder whether something from there is somehow connected to your
onTextChanged()slot, by accident?? -
@JonB Could you point me to documentation regarding the auto-connect to slots by name feature. How would I check what connections currently exist?
@Christian-Ehrlicher I tried running the code provided and the slot function was executed. I am moving forward with the debugging.
-
@JonB Could you point me to documentation regarding the auto-connect to slots by name feature. How would I check what connections currently exist?
@Christian-Ehrlicher I tried running the code provided and the slot function was executed. I am moving forward with the debugging.
@qutie123 said in Slot function executes on ui->show function:
How would I check what connections currently exist?
https://doc.qt.io/qt-6/designer-using-a-ui-file.html#automatic-connections
But your slot does not match the criteria
-
qt version: 5.12.8
system: Ubuntu 20.04 focalThe slot function prints "This is a string" to the console and that is how I know the slot function executes. I have attached a screenshot showing that on my machine.
May I ask what your version of qt and platform is?
Also, I only provided the version and system information. What other details could be provided regarding my configuration?Thank you for the note on the call to QMainWindow::show(), I will make sure to call it in main().
Screenshot:

-
@JonB @Christian-Ehrlicher
I was able to fix the problem by by using qmake and building from the terminal. The .pro file is below.QT += core gui QT += widgets greaterThan(QT_MAJOR_VERSION, X): QT += widgets TEMPLATE = app TARGET = Examples00 INCLUDEPATH += . # Input SOURCES += main.cpp/ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.uiThe slot function is executed once when running the program executable the first time so I am back to my initial problem. As I am new to debugging, could someone explain to me what I should look for (As reasons why the slot function is executed once on the initial run of the program).
To clarify further. I ran the program in debug mode and the before changing any text in the application or alt-tabbing in or out, the slot function was executed once. I know this because on the initial run in debug mode I was able to continue the GDB debugger one step forward in the program execution, that step being the onTextChanged() function being called and the console printing out the statement "The text has been changed: 1 many times".
I will do more analysis on why building the program with qmake and executing from the terminal fixed the alt-tab problem, perhaps by using qmake inside qt creator and seeing the results.
Any advice regarding the debugging would be helpful, thank you!
-
@JonB @Christian-Ehrlicher
I was able to fix the problem by by using qmake and building from the terminal. The .pro file is below.QT += core gui QT += widgets greaterThan(QT_MAJOR_VERSION, X): QT += widgets TEMPLATE = app TARGET = Examples00 INCLUDEPATH += . # Input SOURCES += main.cpp/ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.uiThe slot function is executed once when running the program executable the first time so I am back to my initial problem. As I am new to debugging, could someone explain to me what I should look for (As reasons why the slot function is executed once on the initial run of the program).
To clarify further. I ran the program in debug mode and the before changing any text in the application or alt-tabbing in or out, the slot function was executed once. I know this because on the initial run in debug mode I was able to continue the GDB debugger one step forward in the program execution, that step being the onTextChanged() function being called and the console printing out the statement "The text has been changed: 1 many times".
I will do more analysis on why building the program with qmake and executing from the terminal fixed the alt-tab problem, perhaps by using qmake inside qt creator and seeing the results.
Any advice regarding the debugging would be helpful, thank you!
@qutie123 said in Slot function executes on ui->show function:
on the initial run in debug mode I was able to continue the GDB debugger one step forward in the program execution, that step being the onTextChanged() function being called and the console printing out the statement "The text has been changed: 1 many times".
Then look at the backtrace from where this function was called.