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. Slot function executes on ui->show function
Forum Updated to NodeBB v4.3 + New Features

Slot function executes on ui->show function

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 914 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.
  • Q Offline
    Q Offline
    qutie123
    wrote on last edited by
    #1

    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_H
    

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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        qutie123
        wrote on last edited by
        #3

        qt version: 5.12.8
        system: Ubuntu 20.04 focal

        The 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:
        onTextChanged().png

        M JonBJ C 3 Replies Last reply
        0
        • Q qutie123

          qt version: 5.12.8
          system: Ubuntu 20.04 focal

          The 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:
          onTextChanged().png

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @qutie123
          Maybe moving e->blockSignals(true);
          before e->appendPlainText("This is a string");

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qutie123
            wrote on last edited by
            #5

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

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

              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 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
              1
              • Q qutie123

                qt version: 5.12.8
                system: Ubuntu 20.04 focal

                The 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:
                onTextChanged().png

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • Q qutie123

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

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

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qutie123
                    wrote on last edited by qutie123
                    #9

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

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • Q qutie123

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

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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 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
                      0
                      • Q qutie123

                        qt version: 5.12.8
                        system: Ubuntu 20.04 focal

                        The 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:
                        onTextChanged().png

                        C Offline
                        C Offline
                        ChrisW67
                        wrote on last edited by
                        #11

                        @qutie123 said in Slot function executes on ui->show function:

                        May I ask what your version of Qt and platform is?

                        Ubuntu 22.04
                        Qt 6.4.0 and Qt 5.15.2 from the online installer
                        Qt 5.15.3 from the Ubuntu distro

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qutie123
                          wrote on last edited by
                          #12

                          @JonB @Christian-Ehrlicher
                          I was able to fix the problem by by using qmake and building from the terminal. The .pro file is below.

                          test.pro

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

                          The 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!

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • Q qutie123

                            @JonB @Christian-Ehrlicher
                            I was able to fix the problem by by using qmake and building from the terminal. The .pro file is below.

                            test.pro

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

                            The 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!

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

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

                            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
                            1

                            • Login

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