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. can not quit the child event loop!
Forum Updated to NodeBB v4.3 + New Features

can not quit the child event loop!

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 957 Views
  • 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.
  • J Offline
    J Offline
    jgxy1123
    wrote on 16 Jun 2023, 07:30 last edited by
    #1

    Hello, All!

    I have problem with QEvent loop.

    I have created a child event loop in QApplication main loop and I want to quit the child loop by connectiong a signal of UI thread to QEventLoop::quit(), which does not work!

    The work flow is as following!

    1. create a window which contains a QTableWidget and run

      • QApplication.exec()
    2. connect the itemDoubleclicked signal of tablewidet to a slot.

      • connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
        .....do something;
        });
    3. in the slot, I will process something

      • in the middle of the slot, I will create a child loop to stop the thread
    • connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
      .....do something;
      QEventLoop loop;
      connect(this, signal(mySignal()), &loop,&EventLoop::quit);
      do something else;
      loop.exec();
      do something else;
      });

    The child loop does not quit even though I can successfully emit mySignal()

    Please help me!!!

    Thank you very much!!!

    J 1 Reply Last reply 16 Jun 2023, 07:42
    0
    • J jgxy1123
      16 Jun 2023, 07:30

      Hello, All!

      I have problem with QEvent loop.

      I have created a child event loop in QApplication main loop and I want to quit the child loop by connectiong a signal of UI thread to QEventLoop::quit(), which does not work!

      The work flow is as following!

      1. create a window which contains a QTableWidget and run

        • QApplication.exec()
      2. connect the itemDoubleclicked signal of tablewidet to a slot.

        • connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
          .....do something;
          });
      3. in the slot, I will process something

        • in the middle of the slot, I will create a child loop to stop the thread
      • connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
        .....do something;
        QEventLoop loop;
        connect(this, signal(mySignal()), &loop,&EventLoop::quit);
        do something else;
        loop.exec();
        do something else;
        });

      The child loop does not quit even though I can successfully emit mySignal()

      Please help me!!!

      Thank you very much!!!

      J Online
      J Online
      JonB
      wrote on 16 Jun 2023, 07:42 last edited by JonB
      #2

      @jgxy1123 said in can not quit the child event loop!:

      connect(this, signal(mySignal()), &loop,&EventLoop::quit);

      This either does not or should not work (should not compile). Use proper connect() syntax.

      J 1 Reply Last reply 16 Jun 2023, 09:53
      3
      • J JonB
        16 Jun 2023, 07:42

        @jgxy1123 said in can not quit the child event loop!:

        connect(this, signal(mySignal()), &loop,&EventLoop::quit);

        This either does not or should not work (should not compile). Use proper connect() syntax.

        J Offline
        J Offline
        jgxy1123
        wrote on 16 Jun 2023, 09:53 last edited by jgxy1123
        #3

        @JonB Thank you for your help!

        sorry for the wrong description of my code.
        actually, my actual code for connecting singal and slot is as below:
        connect(this, &WidgetClass::mySinal, &loop, &QEventLoop::quit);

        the other codes for connecting singal and slot all work well , but not only this one.

        do you have any other idea?

        J 1 Reply Last reply 16 Jun 2023, 09:56
        0
        • J jgxy1123
          16 Jun 2023, 09:53

          @JonB Thank you for your help!

          sorry for the wrong description of my code.
          actually, my actual code for connecting singal and slot is as below:
          connect(this, &WidgetClass::mySinal, &loop, &QEventLoop::quit);

          the other codes for connecting singal and slot all work well , but not only this one.

          do you have any other idea?

          J Online
          J Online
          JonB
          wrote on 16 Jun 2023, 09:56 last edited by
          #4

          @jgxy1123
          Your original code was not copied and pasted. I am doubting this is copied and pasted either? For my part at least, I will help only if you copy and paste code now, and show some context of where this appears and what your loop actually looks like.

          J 1 Reply Last reply 16 Jun 2023, 11:26
          0
          • J JonB
            16 Jun 2023, 09:56

            @jgxy1123
            Your original code was not copied and pasted. I am doubting this is copied and pasted either? For my part at least, I will help only if you copy and paste code now, and show some context of where this appears and what your loop actually looks like.

            J Offline
            J Offline
            jgxy1123
            wrote on 16 Jun 2023, 11:26 last edited by jgxy1123
            #5

            @JonB Hello, please refer to the below.
            As you can see, myFunction2() does not run even though I emit signalQuitLoop() to quit the child loop in myFunction1().

            ----myDemo.h-------

            #pragma once

            #include <QtWidgets/QWidget>
            #include "ui_MyDemo.h"
            #include <QTableWidget>
            #include <QEventLoop>
            #include <QDebug>
            #include <QThread>
            #include <QTableWidgetItem>

            QT_BEGIN_NAMESPACE
            namespace Ui { class MyDemoClass; };
            QT_END_NAMESPACE

            class MyDemo : public QWidget
            {
            Q_OBJECT

            public:
            MyDemo(QWidget *parent = nullptr);
            ~MyDemo();
            void setupUI();
            void setupEvent();
            void myFunction1();
            void myFunction2();

            signals:
            void signalQuitLoop();

            public slots:

            private:
            Ui::MyDemoClass *ui;
            QTableWidget * tbWidget;
            };

            --------myDemo.cpp-------------------
            #include "MyDemo.h"

            MyDemo::MyDemo(QWidget *parent)
            : QWidget(parent)
            , ui(new Ui::MyDemoClass())
            {
            tbWidget = new QTableWidget(this);
            setupUI();
            setupEvent();
            //ui->setupUi(this);
            }

            MyDemo::~MyDemo()
            {
            delete ui;
            }

            void MyDemo::setupUI()
            {
            //tbWidget
            tbWidget->setColumnCount(2);
            tbWidget->setRowCount(2);
            tbWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

            for (auto row : { 0,1 }) {
                for (auto col : { 0,1 }) {
            
                    QTableWidgetItem* item = new QTableWidgetItem(QString("row:%1,col:%2").arg(row).arg(col));
                    tbWidget->setItem(row, col, item);
                }
            }
            
            //Main UI
            setFixedSize(800, 600);
            

            }

            void MyDemo::setupEvent()
            {

            connect(tbWidget, &QTableWidget::itemDoubleClicked,this,[this](QTableWidgetItem *item) {
            
                qDebug() << "the slot for tbWidget triggered";
            
                QEventLoop loop;
            
                connect(this, &MyDemo::signalQuitLoop, &loop, &QEventLoop::quit);
            
                myFunction1();
            
                loop.exec();
            
                myFunction2();
            
            
                });
            

            }

            void MyDemo::myFunction1()
            {

            for (auto i : {1, 2, 3, 4, 5}) {
            
                qDebug() << "I am myFunction1()";
                QThread::sleep(2);
            }
            
            emit signalQuitLoop();
            

            }

            void MyDemo::myFunction2()
            {
            for (auto i : {1, 2, 3, 4, 5}) {

                qDebug() << "I am myFunction2()";
            }
            

            }

            jsulmJ 1 Reply Last reply 16 Jun 2023, 11:37
            0
            • J jgxy1123
              16 Jun 2023, 11:26

              @JonB Hello, please refer to the below.
              As you can see, myFunction2() does not run even though I emit signalQuitLoop() to quit the child loop in myFunction1().

              ----myDemo.h-------

              #pragma once

              #include <QtWidgets/QWidget>
              #include "ui_MyDemo.h"
              #include <QTableWidget>
              #include <QEventLoop>
              #include <QDebug>
              #include <QThread>
              #include <QTableWidgetItem>

              QT_BEGIN_NAMESPACE
              namespace Ui { class MyDemoClass; };
              QT_END_NAMESPACE

              class MyDemo : public QWidget
              {
              Q_OBJECT

              public:
              MyDemo(QWidget *parent = nullptr);
              ~MyDemo();
              void setupUI();
              void setupEvent();
              void myFunction1();
              void myFunction2();

              signals:
              void signalQuitLoop();

              public slots:

              private:
              Ui::MyDemoClass *ui;
              QTableWidget * tbWidget;
              };

              --------myDemo.cpp-------------------
              #include "MyDemo.h"

              MyDemo::MyDemo(QWidget *parent)
              : QWidget(parent)
              , ui(new Ui::MyDemoClass())
              {
              tbWidget = new QTableWidget(this);
              setupUI();
              setupEvent();
              //ui->setupUi(this);
              }

              MyDemo::~MyDemo()
              {
              delete ui;
              }

              void MyDemo::setupUI()
              {
              //tbWidget
              tbWidget->setColumnCount(2);
              tbWidget->setRowCount(2);
              tbWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

              for (auto row : { 0,1 }) {
                  for (auto col : { 0,1 }) {
              
                      QTableWidgetItem* item = new QTableWidgetItem(QString("row:%1,col:%2").arg(row).arg(col));
                      tbWidget->setItem(row, col, item);
                  }
              }
              
              //Main UI
              setFixedSize(800, 600);
              

              }

              void MyDemo::setupEvent()
              {

              connect(tbWidget, &QTableWidget::itemDoubleClicked,this,[this](QTableWidgetItem *item) {
              
                  qDebug() << "the slot for tbWidget triggered";
              
                  QEventLoop loop;
              
                  connect(this, &MyDemo::signalQuitLoop, &loop, &QEventLoop::quit);
              
                  myFunction1();
              
                  loop.exec();
              
                  myFunction2();
              
              
                  });
              

              }

              void MyDemo::myFunction1()
              {

              for (auto i : {1, 2, 3, 4, 5}) {
              
                  qDebug() << "I am myFunction1()";
                  QThread::sleep(2);
              }
              
              emit signalQuitLoop();
              

              }

              void MyDemo::myFunction2()
              {
              for (auto i : {1, 2, 3, 4, 5}) {

                  qDebug() << "I am myFunction2()";
              }
              

              }

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 16 Jun 2023, 11:37 last edited by
              #6

              @jgxy1123 said in can not quit the child event loop!:

              myFunction1();

              loop.exec();
              

              You're starting the event loop AFTER function myFunction1() terminates! So, I guess QEventLoop::quit is called BEFORE you start the event loop and has no effect...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply 17 Jun 2023, 01:59
              3
              • jsulmJ jsulm
                16 Jun 2023, 11:37

                @jgxy1123 said in can not quit the child event loop!:

                myFunction1();

                loop.exec();
                

                You're starting the event loop AFTER function myFunction1() terminates! So, I guess QEventLoop::quit is called BEFORE you start the event loop and has no effect...

                J Offline
                J Offline
                jgxy1123
                wrote on 17 Jun 2023, 01:59 last edited by jgxy1123
                #7

                @jsulm Thank you so much, you helped me a lot!
                As you said, the slot to quit loop is called before loop.exec(), so there is never a chance to quit loop.

                1 Reply Last reply
                0
                • J jgxy1123 has marked this topic as solved on 17 Jun 2023, 02:00

                1/7

                16 Jun 2023, 07:30

                • Login

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