can not quit the child event loop!
-
wrote on 16 Jun 2023, 07:30 last edited by
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!
-
create a window which contains a QTableWidget and run
- QApplication.exec()
-
connect the itemDoubleclicked signal of tablewidet to a slot.
- connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
.....do something;
});
- connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
-
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!!!
-
-
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!
-
create a window which contains a QTableWidget and run
- QApplication.exec()
-
connect the itemDoubleclicked signal of tablewidet to a slot.
- connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
.....do something;
});
- connect(tablewidiget, &QTableWidiget::itemDoubleClicked,this,[this](){
-
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!!!
wrote on 16 Jun 2023, 07:42 last edited by JonB@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. -
-
@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.wrote on 16 Jun 2023, 09:53 last edited by jgxy1123@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?
-
@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?
wrote on 16 Jun 2023, 09:56 last edited by@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. -
@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.wrote on 16 Jun 2023, 11:26 last edited by jgxy1123@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_NAMESPACEclass MyDemo : public QWidget
{
Q_OBJECTpublic:
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()"; }
}
-
@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_NAMESPACEclass MyDemo : public QWidget
{
Q_OBJECTpublic:
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()"; }
}
@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...
-
@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...
-
1/7