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
QtWS25 Last Chance

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.5k 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.
  • J JonB
    20 Jan 2023, 10:48

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

    C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 20 Jan 2023, 10:53 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

    J D 2 Replies Last reply 20 Jan 2023, 10:55
    1
    • C Christian Ehrlicher
      20 Jan 2023, 10:53

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

      J Offline
      J Offline
      JonB
      wrote on 20 Jan 2023, 10:55 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
      • C Christian Ehrlicher
        20 Jan 2023, 10:53

        @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 20 Jan 2023, 11:12 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

        C 1 Reply Last reply 20 Jan 2023, 11:50
        0
        • D Dean21
          20 Jan 2023, 11:12

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

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 20 Jan 2023, 11:50 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 20 Jan 2023, 12:25
          0
          • C Christian Ehrlicher
            20 Jan 2023, 11:50

            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 20 Jan 2023, 12:25 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
            
            J 1 Reply Last reply 20 Jan 2023, 12:54
            0
            • D Dean21
              20 Jan 2023, 12:25

              @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
              
              J Offline
              J Offline
              JonB
              wrote on 20 Jan 2023, 12:54 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 20 Jan 2023, 13:02 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 :(

                J 1 Reply Last reply 20 Jan 2023, 13:23
                0
                • D Dean21
                  20 Jan 2023, 13:02

                  @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 :(

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 20 Jan 2023, 13:23 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 20 Jan 2023, 13:31
                  0
                  • J jsulm
                    20 Jan 2023, 13:23

                    @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 20 Jan 2023, 13:31 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

                    J J 2 Replies Last reply 20 Jan 2023, 13:34
                    0
                    • D Dean21
                      20 Jan 2023, 13:31

                      @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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 20 Jan 2023, 13:34 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 20 Jan 2023, 13:56
                      0
                      • D Dean21
                        20 Jan 2023, 13:31

                        @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

                        J Offline
                        J Offline
                        JonB
                        wrote on 20 Jan 2023, 13:35 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 20 Jan 2023, 13:44
                        1
                        • J JonB
                          20 Jan 2023, 13:35

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

                          D Offline
                          D Offline
                          Dean21
                          wrote on 20 Jan 2023, 13:44 last edited by
                          #22

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

                          1 Reply Last reply
                          0
                          • J jsulm
                            20 Jan 2023, 13:34

                            @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 20 Jan 2023, 13:56 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

                            J 1 Reply Last reply 23 Jan 2023, 06:17
                            0
                            • D Dean21
                              20 Jan 2023, 13:56

                              @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

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 23 Jan 2023, 06:17 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

                              20/24

                              20 Jan 2023, 13:34

                              • Login

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