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. issue getting a lineEdit to update
Forum Updated to NodeBB v4.3 + New Features

issue getting a lineEdit to update

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++lineeditfunction
18 Posts 3 Posters 1.7k 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.
  • jsulmJ jsulm

    @Dean21 said in issue getting a lineEdit to update:

    but still no update in the line edit

    What is the value you're trying to set? Maybe it is empty string?

    "error: ‘this’ was not captured for this lambda function" - simply capture "this" in your lambda:

    QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                msg_as_qstring = QString(msg);
                qDebug() << "test value is"<< msg_as_qstring;
            });
    
    D Offline
    D Offline
    Dean21
    wrote on last edited by
    #7

    @jsulm I did try adding the [this] but is still the same, I do expect it to be an empty string on the first time the code is ran because I havent sent anything on MQTT. Just to let you know I have a raspberry pi that when I run a cmd line command sends the MQTT data.
    I feel as though I need to be able to have the function call inside of the onMessageRecieved function, as when I receive a message I then want to update the lineEdit

    jsulmJ 1 Reply Last reply
    0
    • D Dean21

      @jsulm I did try adding the [this] but is still the same, I do expect it to be an empty string on the first time the code is ran because I havent sent anything on MQTT. Just to let you know I have a raspberry pi that when I run a cmd line command sends the MQTT data.
      I feel as though I need to be able to have the function call inside of the onMessageRecieved function, as when I receive a message I then want to update the lineEdit

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

      @Dean21 Please post your current code, capturing "this" should work.
      Checking value of the string you're setting is also easy...

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

      D 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Dean21 Please post your current code, capturing "this" should work.
        Checking value of the string you're setting is also easy...

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

        @jsulm I can post the code but it uses a library that had to be downloaded and setup inside the .pro, just cause if you try to run it without then it wont work, plus you would need to be able to send MQTT data to it, to check if the line edit updates when it should.

        jsulmJ 1 Reply Last reply
        0
        • D Dean21

          @jsulm I can post the code but it uses a library that had to be downloaded and setup inside the .pro, just cause if you try to run it without then it wont work, plus you would need to be able to send MQTT data to it, to check if the line edit updates when it should.

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

          @Dean21 I don't ask you to post all your code, just the relevant parts (especially your current connect statement with captured "this").

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

          D 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Dean21 I don't ask you to post all your code, just the relevant parts (especially your current connect statement with captured "this").

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

            @jsulm sorry for long reply I have to wait 10 minutes between post as I am new user, for completness the code for the QMMqtClient.h and .cpp files are found here in case you wanted to see that as well https://github.com/KostiantynBushko/QMosqMqttClient

            #include "widget.h"
            #include "./ui_widget.h"
            #include <QPalette>
            #include <QDebug>
            #include <QTimer>
            
            
            #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"
            //--------------------------------------
            
            QString msg_as_qstring;
            
            
            Widget::Widget(QWidget *parent)
                : QWidget(parent)
                , ui(new Ui::Widget)
            
            {
                ui->setupUi(this);
            
            
                    QObject::connect(&client, &QMMqttClient::onConnected, this ,[this](){
                        qDebug() << " QMMqttClient::onConnected handler, subscribe to topic...";
                        client.subscribeTopic("command/stop");
                    });
            
                    QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                        msg_as_qstring = QString(msg);
                        qDebug() << "test value is"<< msg_as_qstring;
                        //msg_functionality();
                    });
            
                    qDebug() << "msg_as_qstring"<< msg_as_qstring;
            
                    //msg_functionality(); // issue here
            
            //        /* An example of unsecure connection */
                    client.initialize("12", RPi_IP, 1883);
            
                    client.connect();
            
            
            }
            
            Widget::~Widget()
            {
                delete ui;
            }
            
            
            void Widget::msg_functionality(){
            
                qDebug() << "here";
                qDebug() << msg_as_qstring;
                ui->Light_Level_Line_Edit->setText(msg_as_qstring);
            }
            
            jsulmJ JonBJ 2 Replies Last reply
            0
            • D Dean21

              @jsulm sorry for long reply I have to wait 10 minutes between post as I am new user, for completness the code for the QMMqtClient.h and .cpp files are found here in case you wanted to see that as well https://github.com/KostiantynBushko/QMosqMqttClient

              #include "widget.h"
              #include "./ui_widget.h"
              #include <QPalette>
              #include <QDebug>
              #include <QTimer>
              
              
              #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"
              //--------------------------------------
              
              QString msg_as_qstring;
              
              
              Widget::Widget(QWidget *parent)
                  : QWidget(parent)
                  , ui(new Ui::Widget)
              
              {
                  ui->setupUi(this);
              
              
                      QObject::connect(&client, &QMMqttClient::onConnected, this ,[this](){
                          qDebug() << " QMMqttClient::onConnected handler, subscribe to topic...";
                          client.subscribeTopic("command/stop");
                      });
              
                      QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                          msg_as_qstring = QString(msg);
                          qDebug() << "test value is"<< msg_as_qstring;
                          //msg_functionality();
                      });
              
                      qDebug() << "msg_as_qstring"<< msg_as_qstring;
              
                      //msg_functionality(); // issue here
              
              //        /* An example of unsecure connection */
                      client.initialize("12", RPi_IP, 1883);
              
                      client.connect();
              
              
              }
              
              Widget::~Widget()
              {
                  delete ui;
              }
              
              
              void Widget::msg_functionality(){
              
                  qDebug() << "here";
                  qDebug() << msg_as_qstring;
                  ui->Light_Level_Line_Edit->setText(msg_as_qstring);
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @Dean21 said in issue getting a lineEdit to update:

              QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
              msg_as_qstring = QString(msg);
              qDebug() << "test value is"<< msg_as_qstring;
              //msg_functionality();
              });

              What exact error do you get if you uncomment msg_functionality()?

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

              D 1 Reply Last reply
              1
              • D Dean21

                @jsulm sorry for long reply I have to wait 10 minutes between post as I am new user, for completness the code for the QMMqtClient.h and .cpp files are found here in case you wanted to see that as well https://github.com/KostiantynBushko/QMosqMqttClient

                #include "widget.h"
                #include "./ui_widget.h"
                #include <QPalette>
                #include <QDebug>
                #include <QTimer>
                
                
                #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"
                //--------------------------------------
                
                QString msg_as_qstring;
                
                
                Widget::Widget(QWidget *parent)
                    : QWidget(parent)
                    , ui(new Ui::Widget)
                
                {
                    ui->setupUi(this);
                
                
                        QObject::connect(&client, &QMMqttClient::onConnected, this ,[this](){
                            qDebug() << " QMMqttClient::onConnected handler, subscribe to topic...";
                            client.subscribeTopic("command/stop");
                        });
                
                        QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                            msg_as_qstring = QString(msg);
                            qDebug() << "test value is"<< msg_as_qstring;
                            //msg_functionality();
                        });
                
                        qDebug() << "msg_as_qstring"<< msg_as_qstring;
                
                        //msg_functionality(); // issue here
                
                //        /* An example of unsecure connection */
                        client.initialize("12", RPi_IP, 1883);
                
                        client.connect();
                
                
                }
                
                Widget::~Widget()
                {
                    delete ui;
                }
                
                
                void Widget::msg_functionality(){
                
                    qDebug() << "here";
                    qDebug() << msg_as_qstring;
                    ui->Light_Level_Line_Edit->setText(msg_as_qstring);
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #13

                @Dean21
                ...And while you are answering @jsulm please show widget.h just in case...

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Dean21 said in issue getting a lineEdit to update:

                  QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                  msg_as_qstring = QString(msg);
                  qDebug() << "test value is"<< msg_as_qstring;
                  //msg_functionality();
                  });

                  What exact error do you get if you uncomment msg_functionality()?

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

                  @jsulm that doesnt give any errors but I also dont see the debug "here" and same as before lineEdit doesnt update

                  and here is the widget.h file code

                  #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:
                      void on_pushButton_clicked();
                      void int_or_string_LV();
                      void int_or_string_HV();
                      void Display_Int_Error(int);
                      void Empty_string_Error(int);
                      void Display_Range_Error(int);
                      void Display_Default(int);
                      void Display_success_msg(int);
                      void Vbias_min_max_check(QString, int);
                      void msg_functionality();
                  
                      void on_lineEdit_returnPressed();
                  
                      void on_HV_pushButton_pressed();
                  
                      void on_HV_lineEdit_returnPressed();
                  
                  
                  private:
                      Ui::Widget *ui;
                  
                  
                  };
                  #endif // WIDGET_H
                  
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • D Dean21

                    @jsulm that doesnt give any errors but I also dont see the debug "here" and same as before lineEdit doesnt update

                    and here is the widget.h file code

                    #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:
                        void on_pushButton_clicked();
                        void int_or_string_LV();
                        void int_or_string_HV();
                        void Display_Int_Error(int);
                        void Empty_string_Error(int);
                        void Display_Range_Error(int);
                        void Display_Default(int);
                        void Display_success_msg(int);
                        void Vbias_min_max_check(QString, int);
                        void msg_functionality();
                    
                        void on_lineEdit_returnPressed();
                    
                        void on_HV_pushButton_pressed();
                    
                        void on_HV_lineEdit_returnPressed();
                    
                    
                    private:
                        Ui::Widget *ui;
                    
                    
                    };
                    #endif // WIDGET_H
                    
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    @Dean21 Do you see the debug output from

                    qDebug() << "test value is"<< msg_as_qstring;
                    

                    ?

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

                    D 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Dean21 Do you see the debug output from

                      qDebug() << "test value is"<< msg_as_qstring;
                      

                      ?

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

                      @jsulm sorry my bad I had the "here" accidentally commented, when I run the program is my full output, I do see both "here" and test value is

                      msg_as_qstring ""
                      void QMMqttClient::initialize(const QString&, const QString&, int) "12" "130.246.58.64" 1883
                      void QMMqttClient::connect()
                      virtual void QMMqttClient::on_connect(int)  Sucessfully connected to host:  "130.246.58.64"  port:  1883
                       QMMqttClient::onConnected handler, subscribe to topic...
                      void QMMqttClient::subscribeTopic(const QString&) : topic:  "command/stop"
                      virtual void QMMqttClient::on_subscribe(int, int, const int*)  mid:  1 , QoS:  1
                      

                      And then when I receive an MQTT message this is what is displayed I added to the msg_functionality to add new debug line its shown below

                      void Widget::msg_functionality(){
                          qDebug() << "here";
                          qDebug() << "msg as string "<< msg_as_qstring;
                          ui->Light_Level_Line_Edit->setText(msg_as_qstring);
                      }
                      
                      Topic:  "command/stop" , Msg:  "2\u0000"
                      test value is "2\u0000"
                      here
                      msg as string  "2\u0000"
                      

                      edit: Oh I just seen that it is working now but I feel we didnt change anything

                      jsulmJ 1 Reply Last reply
                      0
                      • D Dean21

                        @jsulm sorry my bad I had the "here" accidentally commented, when I run the program is my full output, I do see both "here" and test value is

                        msg_as_qstring ""
                        void QMMqttClient::initialize(const QString&, const QString&, int) "12" "130.246.58.64" 1883
                        void QMMqttClient::connect()
                        virtual void QMMqttClient::on_connect(int)  Sucessfully connected to host:  "130.246.58.64"  port:  1883
                         QMMqttClient::onConnected handler, subscribe to topic...
                        void QMMqttClient::subscribeTopic(const QString&) : topic:  "command/stop"
                        virtual void QMMqttClient::on_subscribe(int, int, const int*)  mid:  1 , QoS:  1
                        

                        And then when I receive an MQTT message this is what is displayed I added to the msg_functionality to add new debug line its shown below

                        void Widget::msg_functionality(){
                            qDebug() << "here";
                            qDebug() << "msg as string "<< msg_as_qstring;
                            ui->Light_Level_Line_Edit->setText(msg_as_qstring);
                        }
                        
                        Topic:  "command/stop" , Msg:  "2\u0000"
                        test value is "2\u0000"
                        here
                        msg as string  "2\u0000"
                        

                        edit: Oh I just seen that it is working now but I feel we didnt change anything

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

                        @Dean21 So, works now.
                        Depending on what data you're receiving this could be wrong:

                        msg_as_qstring = QString(msg);
                        

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

                        D 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @Dean21 So, works now.
                          Depending on what data you're receiving this could be wrong:

                          msg_as_qstring = QString(msg);
                          
                          D Offline
                          D Offline
                          Dean21
                          wrote on last edited by Dean21
                          #18

                          @jsulm the data that is sent is in the form of a char array, example,

                          char message[]  = "message here";
                          

                          I use QString because before I was using std::string and I was getting errors when trying to submit that to the line edit, I think conversion type errors, using a QString removed that error and thus I just stuck with it.

                          edit: oh it is working now, I feel like we didnt change anything though and we were just going through debug statements

                          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