Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to use Qtconcurrent::run with either a member function or maybe a lamdba function
Forum Updated to NodeBB v4.3 + New Features

how to use Qtconcurrent::run with either a member function or maybe a lamdba function

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtconcurrentthreadfunctionc++qfuture
24 Posts 5 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @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?

    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @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?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    JonBJ D 2 Replies Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      @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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #12

      @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!

      1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @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?

        D Offline
        D Offline
        Dean21
        wrote on last edited by
        #13

        @Christian-Ehrlicher and @JonB I have tried moving QMMqttClient client; to globally defined and globally defined as static and still have the same result

        Christian EhrlicherC 1 Reply Last reply
        0
        • D Dean21

          @Christian-Ehrlicher and @JonB I have tried moving QMMqttClient client; to globally defined and globally defined as static and still have the same result

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #14

          And did you add it as member as the people @so told you? Please post your code after you did it.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            And did you add it as member as the people @so told you? Please post your code after you did it.

            D Offline
            D Offline
            Dean21
            wrote on last edited by
            #15

            @Christian-Ehrlicher

            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
            
            JonBJ 1 Reply Last reply
            0
            • D Dean21

              @Christian-Ehrlicher

              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
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #16

              @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?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dean21
                wrote on last edited by
                #17

                @JonB using [=] and [client] both introduce errors, using [&] didnt add errors but doesnt fix it crashing
                and using this for slot object also still causes crash :(

                jsulmJ 1 Reply Last reply
                0
                • D Dean21

                  @JonB using [=] and [client] both introduce errors, using [&] didnt add errors but doesnt fix it crashing
                  and using this for slot object also still causes crash :(

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #18

                  @Dean21 If client is member of the class then simply capture this:

                  connect(&client, &QMMqttClient::onConnected, this, [this](){ // Use client here
                  

                  If you get error then please post those....

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  D 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Dean21 If client is member of the class then simply capture this:

                    connect(&client, &QMMqttClient::onConnected, this, [this](){ // Use client here
                    

                    If you get error then please post those....

                    D Offline
                    D Offline
                    Dean21
                    wrote on last edited by Dean21
                    #19

                    @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

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • D Dean21

                      @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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #20

                      @Dean21 Please show your current code.
                      This code works for me just fine (ui is member of the class calling connect):

                      connect(ui->pushButton, &QPushButton::pressed, [this]() { ui->pushButton->setText("DONE"); });
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      D 1 Reply Last reply
                      0
                      • D Dean21

                        @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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #21

                        @Dean21
                        Let's start with: did you actually delete the QMMqttClient client; statement in the constructor?

                        D 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @Dean21
                          Let's start with: did you actually delete the QMMqttClient client; statement in the constructor?

                          D Offline
                          D Offline
                          Dean21
                          wrote on last edited by
                          #22

                          @JonB yes sorry I forgot to uncomment this i think this is working now

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Dean21 Please show your current code.
                            This code works for me just fine (ui is member of the class calling connect):

                            connect(ui->pushButton, &QPushButton::pressed, [this]() { ui->pushButton->setText("DONE"); });
                            
                            D Offline
                            D Offline
                            Dean21
                            wrote on last edited by
                            #23

                            @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

                            jsulmJ 1 Reply Last reply
                            0
                            • D Dean21

                              @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

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #24

                              @Dean21 Please show your current code if something does not work...

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved