how to use Qtconcurrent::run with either a member function or maybe a lamdba function
-
Hi, I have looked at examples in the documentation and on here and other forums but I dont fully understand it yet. I have this bit of code shown below which is a library for a qt wrapper around the mosquitto mqtt library. The library works great if I run the code in the main.cpp file. However when using it in conjunction with my other code in my widget.cpp file the line
client.connect()
makes it crash.
So i thought I would use the Qtconcurrent::run function to run it in a seperate thread. Shown below is my code
QMMqttClient client; QObject::connect(&client, &QMMqttClient::onConnected, [&client](){ qDebug() << Q_FUNC_INFO << " QMMqttClient::onConnected handler, subscribe to topic..."; client.subscribeTopic("command/stop"); }); QObject::connect(&client, &QMMqttClient::onMessageReceived, [](const QString &topic, const QByteArray &msg) { qDebug() << Q_FUNC_INFO << " QMMqttClient::onMessageReceived: topic: " << topic << ", message: " << QString::fromStdString(msg.toStdString()); }); /* An example of unsecure connection */ client.initialize("12", "130.246.58.64", 1883); /* The second and third argument can be leve and just set to QString() default value. */ // client.setTls("absolute path to root CA file", QString(), QString()); QFuture<void> future = QtConcurrent::run(QMMqttClient::connect(), &client);
But the last line (QFuture) I can an error saying
/home/dave/FETS_GUI/widget.cpp:186: error: cannot call member function ‘void QMMqttClient::connect()’ without object
Overall I am trying to run the function client.connect() but would like the member function to be ran in a different thread using Qtconcurrent::run
Any help with this would be great.
Thanks in advance,
Dean -
@Dean21 said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
QMMqttClient client;
How long do you think is the livetime of this (function local) object? Or you posted to few code.
Why do you need a future/separate thread to call connect() at all? -
@Christian-Ehrlicher because when if i just call client.connect() it crashes my program saying an error similar to memory tried to go out of bounds. And if i run it in a clean program in the .cpp file it works fine so I assumed what I have going on in the rest of my program causes it to crash so i thought id try to put it in a different thread
-
@Dean21 said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
because when if i just call client.connect() it crashes my program saying an error similar to memory tried to go out of bounds.
Please read my complete post, esp. the part about the lifetime of the object.
-
@Dean21 said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
also umsure of the live time of the object as this is code from a library I used, link is here https://github.com/KostiantynBushko/QMosqMqttClient
I don't see what this has to do with your code above except you use this class but does not has anything to do with my question... please post your whole code /function. If the stuff you posted above is in a single function call then I'm correct and 'client' will be destroyed at the end of the function - basic C stuff...
-
@Christian-Ehrlicher Hi, is there a way to upload my widget.cpp file here, or should I just copy the code in here, just its a little long and didnt want to cluter.
Thanks Dean
-
This is my widget.cpp file and if you want to see the code for the QMMqtClient.h its in the github link in the other comment.
The bit we were talking about is near the top I have tried to dynamically allocate memory to my object but couldnt figure it out as have never really used dynamic memory allocation before. Any help would be great,
Thanks, Dean#include "widget.h" #include "./ui_widget.h" #include <QPalette> #include <QDebug> #include <QTimer> #include <QFuture> #include <QThreadPool> #include <QtConcurrent/QtConcurrent> #include <string> #include <cstring> #include <iostream> #include <time.h> #include </home/dave/mosquitto/include/mosquitto.h> //needed for mosquitto MQTT //-----for qt qrapper for mosquitto----- #include <QCoreApplication> #include <QTime> #include "QMMqttClient.h" //-------------------------------------- #define LV_option 0 #define HV_option 1 int is_digit_or_string, is_digit_or_string_HV; Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //----------To set background colour---------- QPalette p(palette()); p.setColor(QPalette::Window, Qt::darkGray); setPalette(p); //-------------------------------------------- //-------------To set Font and Size------------- QFont f( "Ubuntu Regular", 13, QFont::Bold); ui->LV_Title->setFont(f); ui->HV_Title->setFont(f); ui->Currently_set_LV->setFont(f); ui->Currently_set_HV->setFont(f); ui->Light_Level_Title->setFont(f); ui->Number_of_photons_Title->setFont(f); ui->Light_Level_before_amp_Title->setFont(f); ui->Currently_set_LV_line->setReadOnly(true); ui->Currently_set_HV_line->setReadOnly(true); ui->Light_Level_Line_Edit->setReadOnly(true); ui->Number_of_Photons_Line_Edit->setReadOnly(true); //----------------------------------------------- //QTimer* timer = new QTimer(); //timer->setInterval(0); //timer->setSingleShot(false); // connect(timer, &QTimer::timeout, this, [=](){ //QCoreApplication a(argc, argv); QMMqttClient client; //QMMqttClient* new QMMqttClient; QObject::connect(&client, &QMMqttClient::onConnected, [&client](){ qDebug() << Q_FUNC_INFO << " QMMqttClient::onConnected handler, subscribe to topic..."; client.subscribeTopic("command/stop"); }); QObject::connect(&client, &QMMqttClient::onMessageReceived, [](const QString &topic, const QByteArray &msg) { qDebug() << Q_FUNC_INFO << " QMMqttClient::onMessageReceived: topic: " << topic << ", message: " << QString::fromStdString(msg.toStdString()); }); /* An example of unsecure connection */ client.initialize("12", "130.246.58.64", 1883); /* The second and third argument can be leve and just set to QString() default value. */ // client.setTls("absolute path to root CA file", QString(), QString()); // client.connect(); // }); //timer->start(); } Widget::~Widget() { delete ui; } //----------function to see if input is int/string---------- bool check_number(std::string str) { for (long unsigned int i = 0; i < str.length(); i++) if (isdigit(str[i]) == false) return false; return true; } //---------------------------------------------------------- void Widget::Vbias_min_max_check(QString string_in, int HV_LV_option){ //string_in is the inputted value from lineEdit box std::string string_holder = string_in.toStdString(); // needed to convert string_in of type QString to std::string int Vbias_Value = std::stoi(string_holder); //string to int converion qDebug() << "Vbias is: " << Vbias_Value; //-----bounds checking for input value----- if(Vbias_Value >= 53){ //----------HV or LV Error Option---------- if(HV_LV_option == LV_option){ Display_Range_Error(LV_option); }else if(HV_LV_option == HV_option){ Display_Range_Error(HV_option); } //----------------------------------------- }else{ //Publish value via MQTT here before the check //of which error msg to display if(HV_LV_option == LV_option){ publish(string_holder, LV_option); } else if(HV_LV_option == HV_option){ publish(string_holder, HV_option); } //----------HV or LV currently set text Option------------------------- if(HV_LV_option == LV_option){ QString VBias_QString_LV = QString::number(Vbias_Value); ui->Currently_set_LV_line->setText(VBias_QString_LV); } else if(HV_LV_option == HV_option){ QString VBias_QString_HV = QString::number(Vbias_Value); ui->Currently_set_HV_line->setText(VBias_QString_HV); } //--------------------------------------------------------------------- //----------HV or LV success msg Option---------- if(HV_LV_option == LV_option){ Display_success_msg(LV_option); }else if(HV_LV_option == HV_option){ Display_success_msg(HV_option); } //----------------------------------------- } } //----------------------------------------- void Widget::Display_Int_Error(int error_choice){ if(error_choice == LV_option){ QPalette pal = ui->label->palette(); pal.setColor(QPalette::WindowText, Qt::darkRed); ui->label->setPalette(pal); ui->label->setText("Error enter integers only"); }else if(error_choice == HV_option){ QPalette pal = ui->HV_Label->palette(); pal.setColor(QPalette::WindowText, Qt::darkRed); ui->HV_Label->setPalette(pal); ui->HV_Label->setText("Error enter integers only"); } } void Widget::Display_Range_Error(int error_choice){ if(error_choice == LV_option){ QPalette pal = ui->label->palette(); pal.setColor(QPalette::WindowText, Qt::darkRed); ui->label->setPalette(pal); ui->label->setText("Error value entered not in range of 0-53V"); }else if(error_choice == HV_option){ QPalette pal = ui->HV_Label->palette(); pal.setColor(QPalette::WindowText, Qt::darkRed); ui->HV_Label->setPalette(pal); ui->HV_Label->setText("Error value entered not in range of 0-53V"); } } void Widget::Display_Default(int error_choice){ if(error_choice == LV_option){ QPalette pal = ui->label->palette(); pal.setColor(QPalette::WindowText, Qt::black); ui->label->setPalette(pal); ui->label->setText("Please Enter VBias Value"); }else if(error_choice == HV_option){ QPalette pal = ui->HV_Title->palette(); pal.setColor(QPalette::WindowText, Qt::black); ui->HV_Title->setPalette(pal); ui->HV_Title->setText("Please Enter VBias Value"); } } void Widget::Display_success_msg(int error_choice){ if(error_choice == LV_option){ QPalette pal = ui->label->palette(); pal.setColor(QPalette::WindowText, Qt::black); ui->label->setPalette(pal); ui->label->setText("Value Inputted successfully"); }else if(error_choice == HV_option){ QPalette pal = ui->HV_Label->palette(); pal.setColor(QPalette::WindowText, Qt::black); ui->HV_Label->setPalette(pal); ui->HV_Label->setText("Value Inputted successfully"); } } void Widget::Empty_string_Error(int error_choice){ if(error_choice == LV_option){ qDebug() << "here"; QPalette pal = ui->label->palette(); pal.setColor(QPalette::WindowText, Qt::darkRed); ui->label->setPalette(pal); ui->label->setText("Nothing inputted"); }else if(error_choice == HV_option){ QPalette pal = ui->HV_Label->palette(); pal.setColor(QPalette::WindowText, Qt::darkRed); ui->HV_Label->setPalette(pal); ui->HV_Label->setText("Nothing inputted"); } } //----------test for if inputted value is an int or string---------- void Widget::int_or_string_LV(){ std::string holder = ui->lineEdit->text().toStdString(); // for std string method QString input = ui->lineEdit->text(); if(holder.empty() == true){ qDebug() << "empty string test"; Empty_string_Error(LV_option); }else if(holder.empty() == false){ qDebug() << "empty is false"; is_digit_or_string = check_number(holder); //function call for if string or int if(is_digit_or_string == 0){ //if input a string ui->lineEdit->clear(); input.clear(); qDebug() << "holder is:" << QString(input); Display_Int_Error(LV_option); //here }else{ //if input is an int qDebug() <<"default"; Display_Default(LV_option); Vbias_min_max_check(input, LV_option); } } } //------------------------------------------------------------------ //----------test for if inputted HV value is an int or string---------- void Widget::int_or_string_HV(){ std::string holder_HV = ui->HV_lineEdit->text().toStdString(); // for std string method QString input_HV = ui->HV_lineEdit->text(); if(holder_HV.empty() == true){ //check if string entered is empty qDebug() << "empty string test"; Empty_string_Error(HV_option); }else if(holder_HV.empty() == false){ //check if string entered is empty is_digit_or_string_HV = check_number(holder_HV); //function call for if string or int //-----------------------HV or LV Error Option----------------------- if(is_digit_or_string_HV == LV_option){ //if input a string / if == 0 ui->HV_lineEdit->clear(); //clear text in the HV line edit box input_HV.clear(); //clear contents of string qDebug() << "holder HV is:" << QString(input_HV); Display_Int_Error(HV_option); //for HV for the Error function (string entered instead of int) }else{ //if input is an int Display_Default(HV_option); //for HV for the display func Vbias_min_max_check(input_HV, HV_option); //function parameters are input_HV which is what the } //func does bounds check on and HV_option which is defined as //1 this is passed so for error checking inside the if statement //so this if statement knows what error to display } //------------------------------------------------------------------- } //--------------------------------------------------------------------- //-----for being able to input when press button----- void Widget::on_pushButton_clicked() { // std::string holder = ui->lineEdit->text().toStdString(); // for std string method int_or_string_LV(); ui->lineEdit->clear(); } //--------------------------------------------------- //-----for being able to input by pressing enter----- void Widget::on_lineEdit_returnPressed() { ui->pushButton->click(); } //--------------------------------------------------- //-----for being able to input when press button----- void Widget::on_HV_pushButton_pressed() { int_or_string_HV(); ui->HV_lineEdit->clear(); } //--------------------------------------------------- //-----for being able to input by pressing enter----- void Widget::on_HV_lineEdit_returnPressed() { ui->HV_pushButton->click(); } //---------------------------------------------------
-
@Dean21 said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
QMMqttClient client;
I don't know whether it's relevant to whatever your issue is, but are you aware this is a local variable in the
Widget::Widget()
constructor and goes out of scope at the end of that constructor? -
@JonB said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
constructor and goes out of scope at the end of that constructor?
This is what I'm try to tell him since my first post... :(
How long do you think is the livetime of this (function local) object?
-
@Christian-Ehrlicher said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
This is what I'm try to tell him since my first post... :(
Yep, just seen!
-
@Christian-Ehrlicher and @JonB I have tried moving QMMqttClient client; to globally defined and globally defined as static and still have the same result
-
And did you add it as member as the people @so told you? Please post your code after you did it.
-
This was the minimal producible example where it still crashes
#include "widget.h" #include "./ui_widget.h" #include <QPalette> #include <QDebug> #include <QTimer> //#include <QFuture> //#include <QThreadPool> //#include <QtConcurrent/QtConcurrent> #include <string> #include <cstring> #include <iostream> #include <time.h> #include </home/dave/mosquitto/include/mosquitto.h> //needed for mosquitto MQTT //-----for qt qrapper for mosquitto----- #include <QCoreApplication> #include <QTime> #include "QMMqttClient.h" //-------------------------------------- Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //----------To set background colour---------- QPalette p(palette()); p.setColor(QPalette::Window, Qt::darkGray); setPalette(p); //-------------------------------------------- //-------------To set Font and Size------------- QFont f( "Ubuntu Regular", 13, QFont::Bold); ui->LV_Title->setFont(f); ui->HV_Title->setFont(f); ui->Currently_set_LV->setFont(f); ui->Currently_set_HV->setFont(f); ui->Light_Level_Title->setFont(f); ui->Number_of_photons_Title->setFont(f); ui->Light_Level_before_amp_Title->setFont(f); ui->Currently_set_LV_line->setReadOnly(true); ui->Currently_set_HV_line->setReadOnly(true); ui->Light_Level_Line_Edit->setReadOnly(true); ui->Number_of_Photons_Line_Edit->setReadOnly(true); //----------------------------------------------- QObject::connect(&client, &QMMqttClient::onConnected, [&client](){ qDebug() << Q_FUNC_INFO << " QMMqttClient::onConnected handler, subscribe to topic..."; client.subscribeTopic("command/stop"); }); QObject::connect(&client, &QMMqttClient::onMessageReceived, [](const QString &topic, const QByteArray &msg) { qDebug() << Q_FUNC_INFO << " QMMqttClient::onMessageReceived: topic: " << topic << ", message: " << QString::fromStdString(msg.toStdString()); }); /* An example of unsecure connection */ client.initialize("12", "130.246.58.64", 1883); client.connect(); / //timer->start(); } Widget::~Widget() { delete ui; }
and here is the widget.h file where I tried to add as a member
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <string> #include "QMMqttClient.h" QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); QMMqttClient client; private slots: private: Ui::Widget *ui; }; #endif // WIDGET_H
But adding the QMMqttClient client; line here produces errors:
capture of non variable Widget::client, This was not captured by this lamdba function Invalid use of non-static data member Widget::client
-
@Dean21 said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
QObject::connect(&client, &QMMqttClient::onConnected, [&client](){ qDebug() << Q_FUNC_INFO << " QMMqttClient::onConnected handler, subscribe to topic..."; client.subscribeTopic("command/stop"); });
I'm not sure whether that
[&client]
doesn't pass as pointer rather than reference? Try[=]
or[client]
or even[&]
, does that make it compile??Alternatively try passing
this
for slot object:QObject::connect(&client, &QMMqttClient::onConnected, this, [&client](){
does that make it compile?
-
@jsulm said in how to use Qtconcurrent::run with either a member function or maybe a lamdba function:
connect(&client, &QMMqttClient::onConnected, this, this{ // Use client here
Hi jsulm, thanks for your help, but that didnt work, the error message was
client' is not captured
and it the error says the error is on the client.subscribeTopic("command/stop") ; line