Hi guys,
I guess its a simple problem, but I cannot get rid of it. I try to invoke a function in a thread asynchronously. So I am using the signal-slot functionality of Qt. However, the thread blocks somehow the GUI. Strangely my GUI freezes after the signal was emitted. Even more strange is for me that a qDebug() is still executed. I made a simple showcase and attached it exemplifying the case.
It just consists of a QThread derived class testThread. The mainwindow has a private variable of testThread called tt. The code below is the mainwindow.cpp
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
QObject::connect(this, SIGNAL(invokeLoop()), &tt, SLOT(startLoop()), Qt::QueuedConnection);
}
MainWindow::~MainWindow(){ delete ui;}
// button in GUI is pressed
void MainWindow::on_pushButton_released(){
emit invokeLoop(); qDebug() << "test";
}
|
|
Source code
|
1
2
3
4
5
6
7
8
9
|
// testthread.cpp
#include "testthread.h"
testThread::testThread(QObject *parent) : QThread(parent){}
// to infinity and beyond
void testThread::startLoop(){
for (int i = 0; ; ++i){ --i; }
}
|
I am sorry if this is a too common problem, but I could not find a solution, since everywhere they say, use Qt::QueuedConnection....
Thanks!
edit:
sorry if forgot to start the thread in the code above and in the example. but changing the code to:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
tt.start();
QObject::connect(this, SIGNAL(invokeLoop()), &tt, SLOT(startLoop()), Qt::QueuedConnection);
}
MainWindow::~MainWindow(){ delete ui;}
void MainWindow::on_pushButton_released(){
emit invokeLoop(); qDebug() << "test";
}
|
Does not help