Results 1 to 14 of 14

Thread: Display changes of integer variable to lcd number

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    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.

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

    micmec (12th November 2013)

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

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 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
  •  
Qt is a trademark of The Qt Company.