Results 1 to 6 of 6

Thread: updating a widget's display

  1. #1
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default updating a widget's display

    I'm trying to continuously update a widget to show numbers h:m:s:ms which are returned from a method in another api linked to Qt. That api generally uses a control loop for the method which returns the time elapsed since a method was started. I finally got QLabel to update using my own signal, and repaint, but the time displayed is very slow; much slower than realtime. The float value for the elapsed time looks fine when shown in the console.

    Is there a better way to go about this to get an h:m:s:ms display which receives values from another api's method, or is it possible to display updated text in Qt without using a widget? I'm not sure what is causing the slow down.

    Qt Code:
    1. //myclass.h
    2. #ifndef MYCLASS_H
    3. #define MYCLASS_H
    4. #include <QWidget>
    5.  
    6. #include "API.hpp"
    7. class QLabel;
    8.  
    9. class myClass : public QWidget
    10. Q_OBJECT
    11. public:
    12. QLabel *mylabel;
    13. double timeVal;
    14. int runapi();
    15. public slots:
    16. void setNum (double num);
    17. signals:
    18. void mySignal(double newnum);
    19. private:
    20. API *myapi;
    21. -----
    22. //myclass.cpp
    23.  
    24. #include "myclass.h"
    25. myClass::myClass()
    26. {
    27. myapi = new API;
    28. mylabel = new QLabel;
    29. connect (this, SIGNAL ( mySignal(double) ), mylabel, SLOT ( setNum(double) ) );
    30. }
    31. int myClass::runapi()
    32. {
    33. while ( myapi->myintreturn() ==0 )
    34. {
    35. timeVal->myapi->mymethodelapsedtime(); //returns double
    36. mylabel->setNum(timeVal);
    37. mylabel->repaint();
    38. }
    39. }
    40. void myClass::setNum (double num)
    41. {
    42. emit mySignal(num);
    43. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: updating a widget's display

    1) Does API class inherit QObject? If yes, it would be better to create signal in it. fro example, timeCahnged(double), and your code will like this:

    Qt Code:
    1. myClass::myClass()
    2. {
    3. myapi = new API;
    4. mylabel = new QLabel;
    5. connect (myapi, SIGNAL ( timeCahnged(double) ), mylabel, SLOT (setNum(double) ) );
    6. }
    To copy to clipboard, switch view to plain text mode 

    2) Set value by QTimer

    Qt Code:
    1. myClass::myClass()
    2. {
    3. myapi = new API;
    4. mylabel = new QLabel;
    5.  
    6. timer = new QTimer(this);
    7. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    8. }
    9.  
    10. int myClass::runapi()
    11. {
    12. // init API
    13.  
    14. timer->start(1000);
    15. }
    16.  
    17. void myClass::update ()
    18. {
    19. mylabel->setNum(myapi->mymethodelapsedtime());
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: updating a widget's display

    Thanks. I tried having api's .hpp inherit QObject and declare the signal in it, so that I could connect the api's signal to the QLabel slot.
    Qt Code:
    1. myClass::myClass()
    2. {
    3. myapi = new API;
    4. mylabel = new QLabel;
    5. connect (myapi, SIGNAL ( timeChaged(double) ), mylabel, SLOT (setNum(double) ) );
    6. }
    To copy to clipboard, switch view to plain text mode 

    but am getting segfault on startup of app, which has to do with "connect", I think. Is there something more required in api's .hpp for signal declaration?

    Qt Code:
    1. signals:
    2. void timeChanged(double newnum);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: updating a widget's display

    Do you have a chance to show us declaration and definition of API class? What's line led to segfault?
    Last edited by pastor; 10th November 2008 at 09:31.

  5. #5
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: updating a widget's display

    Did dist-clean and rebuild proj (Kdev), and moc error is 'undefined ref to vtable API.hpp', when defining signal in external API.hpp. I'm including API.hpp and linking to the external apps libraries from Kdev in my Qt4 proj. Would guess then in order to define signal in API.hpp, I need to also rebuild the API's libraries including a link back to QtCore, and QtGui?

    External API.hpp is list of virtual functions for included older c code API.h, and API.h is only a list of the publicly exposed functions, the implementation of which is scattered throughout various .c, and .cpp files.

    But the external API was already doing the job correctly of returning floats (timeVal, viewable in console) by including API.hpp:

    Qt Code:
    1. timeVal->myapi->mymethodelapsedtime();
    2. std::cout << timeVal << std::endl;
    To copy to clipboard, switch view to plain text mode 

    I need to find a better way of updating a Qt widget's display using those floats. In current code above, defining a custom signal, and implementing slot in myClass works but is very slow for h:m:s:ms display. Also tried QLCDNumber and QLineEdit widgets with same result. Is there no solution using MyClass?

  6. #6
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: updating a widget's display

    I suggested 2 ways (please see my post for 6th November 2008, 19:48). You tried the first way (add signal to API class). It doesn't work for you, because API doesn't inherits QObject. Right? Now, you can try the second way, with QTimer.

Similar Threads

  1. QLogText & QLogTable : 2 widgets to display text log
    By fcoiffie in forum Qt-based Software
    Replies: 7
    Last Post: 28th April 2019, 07:52
  2. Mac OS X Top-Level Transparent Widgets
    By tinsuke in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 16:01
  3. Replies: 0
    Last Post: 1st October 2008, 16:21
  4. Upper limit on number of widgets?
    By jdiewald in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2008, 23:00
  5. Replies: 4
    Last Post: 10th December 2006, 09:04

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.