how to use Qtconcurrent::run with either a member function or maybe a lamdba function
-
@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
-
@jsulm sorry i forgot to comment a line of code, this is working now thank you so much for your help I was really stuck and dont think I would have fixed without help from everyone here thank you.
But would this prevent any functionality of any other code from working, for example I have a lineEdit box and once I enter a value its suppose to show in another lineEdit box but that functionality seems to have stopped working, since this new part started working