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 4.1k 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.
  • D Dean21
    19 Jan 2023, 17:09

    @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

    C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 19 Jan 2023, 17:27 last edited by
    #4

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

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

    1 Reply Last reply
    0
    • D Dean21
      19 Jan 2023, 17:09

      @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

      D Offline
      D Offline
      Dean21
      wrote on 19 Jan 2023, 18:35 last edited by
      #5

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

      C 1 Reply Last reply 19 Jan 2023, 19:10
      0
      • D Dean21
        19 Jan 2023, 18:35

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

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 19 Jan 2023, 19:10 last edited by
        #6

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

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

        D 2 Replies Last reply 20 Jan 2023, 09:28
        1
        • C Christian Ehrlicher
          19 Jan 2023, 19:10

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

          D Offline
          D Offline
          Dean21
          wrote on 20 Jan 2023, 09:28 last edited by
          #7

          @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

          J 1 Reply Last reply 20 Jan 2023, 09:32
          0
          • D Dean21
            20 Jan 2023, 09:28

            @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

            J Online
            J Online
            J.Hilk
            Moderators
            wrote on 20 Jan 2023, 09:32 last edited by
            #8

            @Dean21 uploading to the forum isn't really a thing :D

            but if you put your code inside code tags ``` it will make it so , that is its formatted and in its own scroll area. No real cluttering.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • C Christian Ehrlicher
              19 Jan 2023, 19:10

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

              D Offline
              D Offline
              Dean21
              wrote on 20 Jan 2023, 10:44 last edited by
              #9

              @Christian-Ehrlicher

              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();
              }
              //---------------------------------------------------
              
              
              J 1 Reply Last reply 20 Jan 2023, 10:48
              0
              • D Dean21
                20 Jan 2023, 10:44

                @Christian-Ehrlicher

                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();
                }
                //---------------------------------------------------
                
                
                J Offline
                J Offline
                JonB
                wrote on 20 Jan 2023, 10:48 last edited by
                #10

                @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 1 Reply Last reply 20 Jan 2023, 10:53
                0
                • 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 :(

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

                                jsulmJ Online
                                jsulmJ Online
                                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
                                • jsulmJ 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

                                  jsulmJ 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

                                    jsulmJ Online
                                    jsulmJ Online
                                    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
                                        • jsulmJ 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

                                          jsulmJ 1 Reply Last reply 23 Jan 2023, 06:17
                                          0

                                          13/24

                                          20 Jan 2023, 11:12

                                          • Login

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