issue getting a lineEdit to update
-
Hi, so as the title says I am trying to get a lineEdit to update a value when a value is received via MQTT, the problem is I cant get the lineEdit to update and even if I put debug statements in they never show so I feel the code is never run but I cant understand why.
I have tried having a function that I pass the parameter to, which would then update the lineEdit, I have tried just placing the lineEdit setText within the function where the value is that I want to display and a couple other approaches but cant get it to work. Below is a close to minimum example code.
There are no errors it just doesnt update the line edit when I expect it to,
Any help would be great,
Thanks, Dean#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; //------------IP Adresses------------ char RPi_IP[] = "130.246.58.64"; //----------------------------------- 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, this ,[this](){ qDebug() << " QMMqttClient::onConnected handler, subscribe to topic..."; client.subscribeTopic("command/stop"); }); QObject::connect(&client, &QMMqttClient::onMessageReceived, [](const QString &topic, const QByteArray &msg) { msg_as_qstring = QString(msg); qDebug() << "test value is"<< msg_as_qstring; }); 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"; ui->Light_Level_Line_Edit->setText(msg_as_qstring); }
FYI: Using Ubuntu, and creating widget applications in qt creator 6.4.2
-
@Dean21 said in issue getting a lineEdit to update:
even if I put debug statements in they never show
Do you mean
qDebug() << "here";
is never printed?
-
@Dean21
Comment out every line inWidget::Widget()
till there is onlymsg_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. -
@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 -
@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; });
-
@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 -
@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.
-
@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); }
-
@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()?
-
@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
-
@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
-
@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