PDA

View Full Version : updating a widget's display



jhearon
6th November 2008, 15:33
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.



//myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QWidget>

#include "API.hpp"
class QLabel;

class myClass : public QWidget
Q_OBJECT
public:
QLabel *mylabel;
double timeVal;
int runapi();
public slots:
void setNum (double num);
signals:
void mySignal(double newnum);
private:
API *myapi;
-----
//myclass.cpp

#include "myclass.h"
myClass::myClass()
{
myapi = new API;
mylabel = new QLabel;
connect (this, SIGNAL ( mySignal(double) ), mylabel, SLOT ( setNum(double) ) );
}
int myClass::runapi()
{
while ( myapi->myintreturn() ==0 )
{
timeVal->myapi->mymethodelapsedtime(); //returns double
mylabel->setNum(timeVal);
mylabel->repaint();
}
}
void myClass::setNum (double num)
{
emit mySignal(num);
}

pastor
6th November 2008, 17:48
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:


myClass::myClass()
{
myapi = new API;
mylabel = new QLabel;
connect (myapi, SIGNAL ( timeCahnged(double) ), mylabel, SLOT (setNum(double) ) );
}

2) Set value by QTimer


myClass::myClass()
{
myapi = new API;
mylabel = new QLabel;

timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
}

int myClass::runapi()
{
// init API

timer->start(1000);
}

void myClass::update ()
{
mylabel->setNum(myapi->mymethodelapsedtime());
}

jhearon
9th November 2008, 19:21
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.


myClass::myClass()
{
myapi = new API;
mylabel = new QLabel;
connect (myapi, SIGNAL ( timeChaged(double) ), mylabel, SLOT (setNum(double) ) );
}


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?



signals:
void timeChanged(double newnum);

pastor
10th November 2008, 09:26
Do you have a chance to show us declaration and definition of API class? What's line led to segfault?

jhearon
10th November 2008, 16:52
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:



timeVal->myapi->mymethodelapsedtime();
std::cout << timeVal << std::endl;


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?

pastor
10th November 2008, 16:59
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.