PDA

View Full Version : Display changes of integer variable to lcd number



micmec
10th November 2013, 19:36
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..
:)

anda_skoa
10th November 2013, 20:16
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,
_

micmec
11th November 2013, 17:13
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..

anda_skoa
11th November 2013, 18:17
You have never called a method on a C++ object?

Syntax for a pointer is: objectVariable->methodName(argumentlist);

E.g.


QCoreApplication *app = QCoreApplication::instance();
app->processEvents();

or in one line


QCoreApplication::instance()->processEvents();


Cheers,
_

micmec
12th November 2013, 16:03
Sorry, but that's not what I meant :) . I mean some example of using it. Here is my code.



Monitor::Monitor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Monitor)
{
ui->setupUi(this);
QCoreApplication *app = QCoreApplication::instance();

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... :(

Lesiok
12th November 2013, 17:35
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.

stampede
12th November 2013, 17:41
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.

micmec
12th November 2013, 17:47
This is my main function



#include "monitor.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, 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. :(

micmec
12th November 2013, 18:01
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. :)

stampede
12th November 2013, 18:53
If the transfer is via UDP then take a look at QUdpSocket: http://qt-project.org/doc/qt-5.0/qtnetwork/qudpsocket.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.

micmec
12th November 2013, 19:04
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. :)

stampede
12th November 2013, 19:16
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.

micmec
12th November 2013, 19:45
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


#include "udp.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

UDP w;
UDP Server;
UDP client;
QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
client.SayHello(*data);
++data;
}
w.show();

return a.exec();
}



udp.cpp


#include "myudp.h"

MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost,1234);
connect(socket,SIGNAL(readyRead()),this,SLOT(Ready Read()));

}

void MyUDP::SayHello()
{
QByteArray Data;
Data.append("Hello from UDP Land");
socket->writeDatagram(Data,QHostAddress::LocalHost,1234);
}

void MyUDP::ReadyRead()
{
QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize());

QHostAddress sender;
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);

qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << Buffer;
}

Lesiok
12th November 2013, 20:35
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.