how to automatically update GUI status
-
while(1)
is an endless loop! Why do you need an endless loop? How often do you actually need to iterate?
"whenever I will enter input" see: -
@Mijaz said in how to automatically update GUI status:
Yes, I need an endless loop.
No, you don't. See my other post.
-
@Mijaz Did you read my previous post?
I gave you two signals. You can use editingFinished signal and connect a slot to it where you execute the code you posted but without loop.Do you actually know that an endless loop will never end? You're basically blocking your application forever with such a loop and it is simply not needed at all...
-
@Mijaz
here's one way you can do it, using the signals @jsulm pointed you toMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->lineEdit, &QLineEdit::textEdited, this, [=]()->void{ QString str = ui->lineEdit->text(); qDebug()<<"Input (HEX) = " << str; bool ok; int iVal = str.toInt(&ok,16); QString binnumber = str.setNum(iVal, 2); if(ok) ui->lineEdit_2->setText(binnumber); else ui->lineEdit_2->setText("Not a number"); qDebug()<<"output in binary = " << binnumber; }); }
replace
textEdited
witheditingFinished
if you want to run this lambda(function) only when the user presses enter/return or the line edit looses focus -
@J-Hilk
Thank you very much. It works great.
But Run and Stop button not functioned. How we can make this function dependent on the Run and Stop. When I push Run it automatically starts to convert the input to output and when press Stop it should not accept any input further. -
@J-Hilk
This program correctly on Desktop. When I try to deply to my SDR Board then following error occures:
1).
/usr/local/Qt-4.8.7-arm/include/QtGui/qlineedit.h:196: error: ‘void QLineEdit::textEdited(const QString&)’ is protected
void textEdited(const QString &);
^2).
/home/ijaz/Qt_applications/update_automatically/mainwindow.cpp:11: error: within this context
connect(ui->lineEdit, &QLineEdit::textEdited, this, =->void
^3).
/home/ijaz/Qt_applications/update_automatically/mainwindow.cpp:23: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
});
^
4).
/home/ijaz/Qt_applications/update_automatically/mainwindow.cpp:23: error: no matching function for call to ‘MainWindow::connect(QLineEdit*&, void (QLineEdit::)(const QString&), MainWindow const, MainWindow::MainWindow(QWidget*)::__lambda0)’
});
^ -
@Mijaz said in how to automatically update GUI status:
Qt-4.8.7-arm
Is there a reason why you use Qt4?
Also your compiler for the device does not support C++11. -
@J-Hilk
I have changed this code to the old version and it works fine. There are two cases I want to execute, cases are follows.case 1: Working correctly.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lineEdit_input, SIGNAL(textEdited(QString)), this, SLOT(myFunction()));}
void MainWindow::myFunction()
{
QString str = ui->lineEdit_input->text();
qDebug()<<"Input (HEX) = " << str;
ui->lineEdit_1->setText(str);
bool ok1;
qint64 iVal = str.toLongLong(&ok1,16);
ui->lineEdit_2->setText(QString::number(iVal));
QString binnumber = str.setNum(iVal, 2);
ui->lineEdit_3->setText(binnumber);
qDebug()<<"Convert to Int = " << QString::number(iVal);
qDebug()<<"Convert to Binary = " << binnumber;
qDebug()<<"size of string binnumber = " << binnumber.size();
}
case 2: Not working correctly,
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lineEdit_input, SIGNAL(textEdited(QString)), this, SLOT(myFunction()));
QProcess process;
process.start("sudo devmem2 0x41210000");
process.waitForFinished(-1); // will wait forever until finished
QString stdout = process.readAllStandardOutput();
qDebug()<<"Input (HEX) = " << stdout;
QChar data[8] = { stdout[98], stdout[99], stdout[100], stdout[101], stdout[102], stdout[103], stdout[104],stdout[105]};
QString str1(data, 8);
qDebug()<<"Input (HEX) = " << str1;}
void MainWindow::myFunction()
{
QString str = ui->lineEdit_input->setText(str1);
qDebug()<<"Input (HEX) = " << str;
ui->lineEdit_1->setText(str);
bool ok1;
qint64 iVal = str.toLongLong(&ok1,16);
ui->lineEdit_2->setText(QString::number(iVal));
QString binnumber = str.setNum(iVal, 2);
ui->lineEdit_3->setText(binnumber);
qDebug()<<"Convert to Int = " << QString::number(iVal);
qDebug()<<"Convert to Binary = " << binnumber;
qDebug()<<"size of string binnumber = " << binnumber.size();
}"QString str1" is unkown "void MainWindow::myFunction()"
-
@Mijaz said in how to automatically update GUI status:
str1
Where is it defined?
The compiler actually tells you that it does not exist at that location... -
@Mijaz said in how to automatically update GUI status:
QString str1 is defined in MainWindow::MainWindow(QWidget *parent) :
as local variable which only exists inside MainWindow::MainWindow(QWidget *parent)...
-
@Mijaz
now I have defined str1 in mainwindow.h
/////////////
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QString str1;///////////////////////
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lineEdit_input, SIGNAL(textEdited(QString)), this, SLOT(myFunction()));
QProcess process;
process.start("sudo devmem2 0x41210000");
process.waitForFinished(-1); // will wait forever until finished
QString stdout = process.readAllStandardOutput();
qDebug()<<"Input (HEX) = " << stdout;
QChar data[8] = { stdout[98], stdout[99], stdout[100], stdout[101], stdout[102], stdout[103], stdout[104],stdout[105]};
QString str11(data, 8);
qDebug()<<"local string = " << str11;
bool ok1;
str1= str11.toInt(&ok1,16);
qDebug()<<"Globle string = " << QString::number(str1);}
void MainWindow::myFunction()
{
//QString str = ui->lineEdit_input->text();
QString str = str1;
qDebug()<<"Input (HEX) = " << str;
ui->lineEdit_1->setText(str);
bool ok1;
qint64 iVal = str.toLongLong(&ok1,16);
ui->lineEdit_2->setText(QString::number(iVal));
QString binnumber = str.setNum(iVal, 2);
ui->lineEdit_3->setText(binnumber);
qDebug()<<"Convert to Int = " << QString::number(iVal);
qDebug()<<"Convert to Binary = " << binnumber;
qDebug()<<"size of string binnumber = " << binnumber.size();
}
//////////////
error now:/home/ijaz/QT_Final_project/my_push_button_update/mainwindow.cpp:22: error: no matching function for call to ‘QString::number(QString&)’
qDebug()<<"Globle string = " << QString::number(str1);
^