QTConcurent and lambda in QT6
-
@JulsPower said in QTConcurent and lambda in QT6:
I don't see how someone could increase the threadcount those its limited to 1 by: m_pool.setMaxThreadCount(1);
Another developer who is modifying your code and don't know about such strange constraints for instance.
I get the correct return value, but I tried to show a QMessageBox::critical in case of error
it shows but the program crash in sigsev shortly after.You must not show an ui element from outside the main (gui) thread.
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
You must not show an ui element from outside the main (gui) thread.
But the then function is run in the main thread that own the gui that shouldn't be the problem.
-
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
You must not show an ui element from outside the main (gui) thread.
But the then function is run in the main thread that own the gui that shouldn't be the problem.
@JulsPower said in QTConcurent and lambda in QT6:
But the then function is run in the main thread that own the gui that shouldn't be the problem.
You a) did not show us code and b) did not do what @JonB suggested so how should we help?
-
@JulsPower said in QTConcurent and lambda in QT6:
it shows but the program crash in sigsev shortly after.
Run under a debugger or show your code. And make sure you call no UI code from your secondary thread.
@JonB said in QTConcurent and lambda in QT6:
@JulsPower said in QTConcurent and lambda in QT6:
it shows but the program crash in sigsev shortly after.
Run under a debugger or show your code. And make sure you call no UI code from your secondary thread.
here is the code that open the db in the main thread (which call the thread to open the db in fact)
m_jdb->open().then([&](const bool &b){ if(!b) QMessageBox::critical(this, "Error", "Couldn't connect to DB"); });
the debugger doesn't help me much here it look like a gui bug but if I breakpoint into the then function "this" is the mainwindow pointer -
Here most of the related code
jdatabase.h#ifndef JDATABASE_H #define JDATABASE_H #include <QThreadPool> #include <QFuture> #include <QSqlDatabase> #include <QtConcurrent> class JDatabase : public QObject { Q_OBJECT signals: void Connected(); void FailToConnect(); public: JDatabase(QWidget *parent = nullptr); ~JDatabase(); QFuture<bool> open(); private: QThreadPool m_pool; }; #endif // JDATABASE_H
jdatabase.cpp
#include "jdatabase.h" JDatabase::JDatabase(QWidget *parent) { // limit to one thread m_pool.setMaxThreadCount(1); // prevent automatic deletion and recreation m_pool.setExpiryTimeout(-1); } JDatabase::~JDatabase() { // m_pool.deleteLater(); } QFuture<bool> JDatabase::open() { //m_pool, return QtConcurrent::run(&m_pool, [&]() { QString connectString = "Driver={SQL Server};Server=192.168.21.174;Database=DBMachines;"; auto db = QSqlDatabase::addDatabase("QODBC"); db.setUserName("Julien"); db.setPassword("MB6262"); db.setDatabaseName(connectString); auto s= db.connectOptions(); db.open(); if(db.isOpen()) emit Connected(); else emit FailToConnect(); return db.isOpen(); }); }
mainwindow.h declaration
JDatabase *m_jdb;
and in mainwindow.cpp
//Connection base de donnée m_jdb = new JDatabase(this); connect(m_jdb, &JDatabase::Connected, this, [&](){QMessageBox::critical(this, "YAY", "Connecté, refresh"); RefreshDB();}); // connect(m_jdb, &JDatabase::FailToConnect, this, [&](){QMessageBox::critical(this, "Erreur", "Impossible de ce connecter à la base de donnée");}); m_jdb->open().then([&](const bool &b){ if(!b) QMessageBox::critical(this, "Error", "Couldn't connect to DB"); });
the signal work as intended but I also tought that the then function would work
thanks guys
-
Here most of the related code
jdatabase.h#ifndef JDATABASE_H #define JDATABASE_H #include <QThreadPool> #include <QFuture> #include <QSqlDatabase> #include <QtConcurrent> class JDatabase : public QObject { Q_OBJECT signals: void Connected(); void FailToConnect(); public: JDatabase(QWidget *parent = nullptr); ~JDatabase(); QFuture<bool> open(); private: QThreadPool m_pool; }; #endif // JDATABASE_H
jdatabase.cpp
#include "jdatabase.h" JDatabase::JDatabase(QWidget *parent) { // limit to one thread m_pool.setMaxThreadCount(1); // prevent automatic deletion and recreation m_pool.setExpiryTimeout(-1); } JDatabase::~JDatabase() { // m_pool.deleteLater(); } QFuture<bool> JDatabase::open() { //m_pool, return QtConcurrent::run(&m_pool, [&]() { QString connectString = "Driver={SQL Server};Server=192.168.21.174;Database=DBMachines;"; auto db = QSqlDatabase::addDatabase("QODBC"); db.setUserName("Julien"); db.setPassword("MB6262"); db.setDatabaseName(connectString); auto s= db.connectOptions(); db.open(); if(db.isOpen()) emit Connected(); else emit FailToConnect(); return db.isOpen(); }); }
mainwindow.h declaration
JDatabase *m_jdb;
and in mainwindow.cpp
//Connection base de donnée m_jdb = new JDatabase(this); connect(m_jdb, &JDatabase::Connected, this, [&](){QMessageBox::critical(this, "YAY", "Connecté, refresh"); RefreshDB();}); // connect(m_jdb, &JDatabase::FailToConnect, this, [&](){QMessageBox::critical(this, "Erreur", "Impossible de ce connecter à la base de donnée");}); m_jdb->open().then([&](const bool &b){ if(!b) QMessageBox::critical(this, "Error", "Couldn't connect to DB"); });
the signal work as intended but I also tought that the then function would work
thanks guys
Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.
-
Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.
the breakpoint in the then lambda give me this
then I run the app the msgbox shows
and as soon my pointer the into the box I get sigsev
how could I find if its the right thread showing the msgbox?, did I understand the then function correctly?
-
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.
the breakpoint in the then lambda give me this
then I run the app the msgbox shows
and as soon my pointer the into the box I get sigsev
how could I find if its the right thread showing the msgbox?, did I understand the then function correctly?
Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())
-
Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())
HI
what is the "backtrace"?
here is a breakpoint to b
should the then part be executed in the main loop?
-
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())
HI
what is the "backtrace"?
here is a breakpoint to b
should the then part be executed in the main loop?
So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.
-
So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.
Hi
I understand the signal and slot and know how to use them for what I want to do
but I wanted to understand better the then function. from what I understood the qfuture would allow to execute code (lambda or a function call) at a later time when the result would have been received. but I don't get why it would be executed by the other thread.thanks again im just trying to understand how thing work (or should work) so I can use those function correctly.
-
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.
Hi
I understand the signal and slot and know how to use them for what I want to do
but I wanted to understand better the then function. from what I understood the qfuture would allow to execute code (lambda or a function call) at a later time when the result would have been received. but I don't get why it would be executed by the other thread.thanks again im just trying to understand how thing work (or should work) so I can use those function correctly.
@JulsPower said in QTConcurent and lambda in QT6:
but I don't get why it would be executed by the other thread.
Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.
-
@JulsPower said in QTConcurent and lambda in QT6:
but I don't get why it would be executed by the other thread.
Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
@JulsPower said in QTConcurent and lambda in QT6:
but I don't get why it would be executed by the other thread.
Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.
then I cannot see a usage for future then function ...
-
-
@Christian-Ehrlicher said in QTConcurent and lambda in QT6:
@JulsPower said in QTConcurent and lambda in QT6:
but I don't get why it would be executed by the other thread.
Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.
then I cannot see a usage for future then function ...
@JulsPower said in QTConcurent and lambda in QT6:
then I cannot see a usage for future then function
The documentation has a nice common use case for it.