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

issue getting a lineEdit to update

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++lineeditfunction
18 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JonB
    24 Jan 2023, 11:42

    @Dean21
    Comment out every line in Widget::Widget() till there is only

    msg_functionality(); // issue here
    

    left. Do you now get the qDebug() << "here";? Assuming you do, uncomment other lines till you find the problem. Standard programming techniques.

    D Offline
    D Offline
    Dean21
    wrote on 24 Jan 2023, 11:57 last edited by Dean21
    #5

    @JonB I did what you said and here does show and for some reason now I have commented it out, ran it and now uncommented everything, I do see "here" each time, but still no update in the line edit. If i try to put the function call inside the on_message function then I get an error saying

    error: ‘this’ was not captured for this lambda function
       98 |             msg_functionality();
          |             ~~~~~~~~~~~~~~~~~^~
    
    
    error: cannot call member function ‘void Widget::msg_functionality()’ without object
    

    I have tried creating an object for the function inside my widget.h file object called

    Widget test;
    

    which gives the error

    error: field ‘test’ has incomplete type ‘Widget’
       20 |     Widget test;
          |            ^~~~
    ../FETS_GUI/widget.h:12:7: note: definition of ‘class Widget’ is not complete until the closing brace
       12 | class Widget : public QWidget
          |       ^~~~~~
    

    and globally defined in the widget.cpp at the top of the file, same object name gives

    QWidget: Must construct a QApplication before a QWidget
    

    Thanks in advance for your help,
    Dean

    J 1 Reply Last reply 24 Jan 2023, 12:07
    0
    • D Dean21
      24 Jan 2023, 11:57

      @JonB I did what you said and here does show and for some reason now I have commented it out, ran it and now uncommented everything, I do see "here" each time, but still no update in the line edit. If i try to put the function call inside the on_message function then I get an error saying

      error: ‘this’ was not captured for this lambda function
         98 |             msg_functionality();
            |             ~~~~~~~~~~~~~~~~~^~
      
      
      error: cannot call member function ‘void Widget::msg_functionality()’ without object
      

      I have tried creating an object for the function inside my widget.h file object called

      Widget test;
      

      which gives the error

      error: field ‘test’ has incomplete type ‘Widget’
         20 |     Widget test;
            |            ^~~~
      ../FETS_GUI/widget.h:12:7: note: definition of ‘class Widget’ is not complete until the closing brace
         12 | class Widget : public QWidget
            |       ^~~~~~
      

      and globally defined in the widget.cpp at the top of the file, same object name gives

      QWidget: Must construct a QApplication before a QWidget
      

      Thanks in advance for your help,
      Dean

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Jan 2023, 12:07 last edited by
      #6

      @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;
              });
      

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

      D 1 Reply Last reply 24 Jan 2023, 12:25
      0
      • J jsulm
        24 Jan 2023, 12:07

        @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 24 Jan 2023, 12:25 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

        J 1 Reply Last reply 24 Jan 2023, 12:28
        0
        • D Dean21
          24 Jan 2023, 12:25

          @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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 24 Jan 2023, 12:28 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 24 Jan 2023, 12:45
          0
          • J jsulm
            24 Jan 2023, 12:28

            @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 24 Jan 2023, 12:45 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.

            J 1 Reply Last reply 24 Jan 2023, 12:46
            0
            • D Dean21
              24 Jan 2023, 12:45

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 24 Jan 2023, 12:46 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 24 Jan 2023, 12:55
              0
              • J jsulm
                24 Jan 2023, 12:46

                @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 24 Jan 2023, 12:55 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);
                }
                
                J J 2 Replies Last reply 24 Jan 2023, 13:01
                0
                • D Dean21
                  24 Jan 2023, 12:55

                  @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);
                  }
                  
                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 24 Jan 2023, 13:01 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 24 Jan 2023, 13:10
                  1
                  • D Dean21
                    24 Jan 2023, 12:55

                    @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);
                    }
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 24 Jan 2023, 13:03 last edited by
                    #13

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

                    1 Reply Last reply
                    0
                    • J jsulm
                      24 Jan 2023, 13:01

                      @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 24 Jan 2023, 13:10 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
                      
                      
                      J 1 Reply Last reply 24 Jan 2023, 13:15
                      0
                      • D Dean21
                        24 Jan 2023, 13:10

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

                          @Dean21 Do you see the debug output from

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

                          ?

                          D Offline
                          D Offline
                          Dean21
                          wrote on 24 Jan 2023, 13:23 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

                          J 1 Reply Last reply 24 Jan 2023, 13:26
                          0
                          • D Dean21
                            24 Jan 2023, 13:23

                            @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

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 24 Jan 2023, 13:26 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 24 Jan 2023, 13:34
                            0
                            • J jsulm
                              24 Jan 2023, 13:26

                              @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 24 Jan 2023, 13:34 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

                              14/18

                              24 Jan 2023, 13:10

                              • Login

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