Results 1 to 14 of 14

Thread: Display changes of integer variable to lcd number

  1. #1
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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:rocessEvent() inside the loop.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.
    Qt Code:
    1. app->processEvents();
    To copy to clipboard, switch view to plain text mode 
    or in one line
    Qt Code:
    1. QCoreApplication::instance()->processEvents();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    micmec (12th November 2013)

  6. #5
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Qt Code:
    1. Monitor::Monitor(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::Monitor)
    4. {
    5. ui->setupUi(this);
    6. QCoreApplication *app = QCoreApplication::instance();
    7.  
    8. this->count=0;
    9. for(int i=0;i<100;i++){
    10. this->ui->lcdNumber->display(count);
    11. for(int j=0;j<5000000;j++){
    12.  
    13. }
    14. this->count++;
    15. app->processEvents();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

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

  7. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,508
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  8. The following user says thank you to Lesiok for this useful post:

    micmec (12th November 2013)

  9. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

  10. The following user says thank you to stampede for this useful post:

    micmec (12th November 2013)

  11. #8
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Display changes of integer variable to lcd number

    This is my main function

    Qt Code:
    1. #include "monitor.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Monitor w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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.

  12. #9
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  13. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

  14. The following user says thank you to stampede for this useful post:

    micmec (12th November 2013)

  15. #11
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  16. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Display changes of integer variable to lcd number

    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.

  17. The following user says thank you to stampede for this useful post:

    micmec (12th November 2013)

  18. #13
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include "udp.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. UDP w;
    9. UDP Server;
    10. UDP client;
    11. QByteArray ba("Hello world");
    12. char *data = ba.data();
    13. while (*data) {
    14. client.SayHello(*data);
    15. ++data;
    16. }
    17. w.show();
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    udp.cpp
    Qt Code:
    1. #include "myudp.h"
    2.  
    3. MyUDP::MyUDP(QObject *parent) :
    4. QObject(parent)
    5. {
    6. socket = new QUdpSocket(this);
    7. socket->bind(QHostAddress::LocalHost,1234);
    8. connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyRead()));
    9.  
    10. }
    11.  
    12. void MyUDP::SayHello()
    13. {
    14. QByteArray Data;
    15. Data.append("Hello from UDP Land");
    16. socket->writeDatagram(Data,QHostAddress::LocalHost,1234);
    17. }
    18.  
    19. void MyUDP::ReadyRead()
    20. {
    21. QByteArray Buffer;
    22. Buffer.resize(socket->pendingDatagramSize());
    23.  
    24. QHostAddress sender;
    25. quint16 senderPort;
    26. socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
    27.  
    28. qDebug() << "Message from: " << sender.toString();
    29. qDebug() << "Message port: " << senderPort;
    30. qDebug() << "Message: " << Buffer;
    31. }
    To copy to clipboard, switch view to plain text mode 

  19. #14
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,508
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

Similar Threads

  1. Replies: 5
    Last Post: 19th February 2013, 15:49
  2. Replies: 1
    Last Post: 5th June 2010, 21:34
  3. display number on label
    By aj2903 in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 07:24
  4. qmake - how to extract number from variable
    By Vanir in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2009, 18:12
  5. Display row Number in QMessageBox
    By arunvv in forum Newbie
    Replies: 6
    Last Post: 1st May 2008, 23:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.