Display changes of integer variable to lcd number
I'm new in QT and I was given a project from my senior..
In this project I am not using widget like slider, spinbox, etc. I just want to display the changes of the value in variable.
For example, I have a variable named x with integer type. And there is a loop that changes the value of x. I need to make ui lcd number to display the changes of x's value. When I use loop, the result display always displaying the last value of x at the end of the loop. How to do it? I have searched through google and I don't find the answer. I want to display the value in lcd number without using widget like slider, etc.
Sorry if my english is not good enough.
Any solution will help..
:)
Re: Display changes of integer variable to lcd number
If you run a loop then the main thread can't do anything other than that, i.e. it can't update the UI. So the first chance it gets is when you exit the loop and return to event processing.
You can either break up the loop, e.g. have a timer call a slot, or call QCoreApplication::processEvent() inside the loop.
Cheers,
_
Re: Display changes of integer variable to lcd number
Thanks for the reply.. :)
Could you please show a simple code example..?
I'm still in the middle of learning the timer and process event because I never use it before..
Re: Display changes of integer variable to lcd number
You have never called a method on a C++ object?
Syntax for a pointer is: objectVariable->methodName(argumentlist);
E.g.
Code:
app->processEvents();
or in one line
Cheers,
_
Re: Display changes of integer variable to lcd number
Sorry, but that's not what I meant :) . I mean some example of using it. Here is my code.
Code:
Monitor
::Monitor(QWidget *parent
) : ui(new Ui::Monitor)
{
ui->setupUi(this);
this->count=0;
for(int i=0;i<100;i++){
this->ui->lcdNumber->display(count);
for(int j=0;j<5000000;j++){
}
this->count++;
app->processEvents();
}
}
The truth is I have to make a monitoring program with Qt. My code just displays 99, not 0 to 99. Or is there another way to do monitoring with Qt?
Sorry if I'm too much asking... :(
Re: Display changes of integer variable to lcd number
What You are monitoring ? Using infinite loop probably is a bad design.
Perhaps as a result of the optimization for loop is not executed because it is empty and You don't see changes of lcdNumber.
PS
I think I know where the problem is. Show us a main() function. I suspect that You are trying to play with GUI in constructor of main widget before start QApplication.
Re: Display changes of integer variable to lcd number
If your compiler is gcc and optimization is O2 (default for qmake generated Makefiles), your empty loop is removed for sure. I don't know about other compilers.
If you really want to see the cool effect of changing a number on lcd widget (which is apparently your only goal here), connect a timer with a non-zero timeout to a slot, in which you increment a variable by 1 and display it.
Re: Display changes of integer variable to lcd number
This is my main function
Code:
#include "monitor.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Monitor w;
w.show();
return a.exec();
}
Sorry if it's kind of embarassing because I'm still new to this. The programming languages that I have learned are just C and Java, but I'm still just a student so I just know the basics. :(
Re: Display changes of integer variable to lcd number
Currently I have to monitor a robot. And I have to read the datas broadcasted from the robot's miniPC via UDP. And I want the data to always be updated until I close the program so I think I need to learn how tow display integer datas to lcd number with loop. If I'm wrong, please correct it because everything that wrong needs to be corrected. :)
Re: Display changes of integer variable to lcd number
If the transfer is via UDP then take a look at QUdpSocket: http://qt-project.org/doc/qt-5.0/qtn...udpsocket.html
Basically you can bind a socket to an address and wait for readyRead() signal. Connect this signal to a slot and read the packet there - your slot will be called everytime a new data is ready, so you don't have to perform busy waiting.
Take look at the source code of "broadcastreceiver" example that comes with Qt installation.
Re: Display changes of integer variable to lcd number
Thanks for the reply stampede.
I will learn about QUdpSocket. One more question. Do I have to use timer to make it always be updated until I close it? I have already learned some about timer. :)
Re: Display changes of integer variable to lcd number
Quote:
Do I have to use timer to make it always be updated until I close it?
I don't know how this robot works, but if it's constantly broadcasting packets, then it's enough to connect to a "readyRead()" signal of the QUdpSocket object. Your slot will be called each time a new data arrives on the socket. So it should be enough to do the ui update in this slot.
Re: Display changes of integer variable to lcd number
Thanks a lot for giving so much help. :)
I have tried some examples with QUdpSocket, but the data was updated too fast. Is there a way to slow it down? And the example I tried is sending and receiving data with a type like String or char. Is there a way to send and receive integer or float value?
This is the example code.
main.cpp
Code:
#include "udp.h"
#include <QApplication>
int main(int argc, char *argv[])
{
UDP w;
UDP Server;
UDP client;
char *data = ba.data();
while (*data) {
client.SayHello(*data);
++data;
}
w.show();
return a.exec();
}
udp.cpp
Code:
#include "myudp.h"
{
connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyRead()));
}
void MyUDP::SayHello()
{
Data.append("Hello from UDP Land");
}
void MyUDP::ReadyRead()
{
Buffer.resize(socket->pendingDatagramSize());
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << Buffer;
}
Re: Display changes of integer variable to lcd number
Update is very fast because You are receiving all data after start QApplication (line 19). Remember that the signals and slots only works when event loop is working which is run by the QApplication::exec(). Thus, all the QUdpSocket::readyRead signals be handled only after line 19.