Answer the question
In order to leave comments, you need to log in
Why does the GUI hang in Qt?
There is a main class in which all elements are registered. I write in the constructor so that the thread_import method works in another thread:
OneThing::OneThing(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::OneThing)
{
// какой-то код
qDebug() << "MAIN THREAD: " << QThread::currentThread();
QFuture<bool> future = QtConcurrent::run(this, &OneThing::thread_import);
QFutureWatcher<bool> watcher;
watcher.setFuture(future);
}
bool OneThing::thread_import() {
qDebug() << "IMPORT THREAD: " << QThread::currentThread();
connect(ui->button_import, &QPushButton::clicked, this, [this]() {
int count = 0;
qDebug() << "CONNECT THREAD: " << QThread::currentThread();
while (true) {
qDebug() << "THREAD: " << count;
count++;
QThread::sleep(1);
}
});
return true;
}
MAIN THREAD: QThread(0x1fd1f88)
IMPORT THREAD: QThread(0x41bfbe8, name = "Thread (pooled)")
CONNECT THREAD: QThread(0x1fd1f88)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question