PDA

View Full Version : signals and slots linked api



jhearon
30th October 2008, 15:47
I have a derived class of QWidget, MyClass which uses another api. That api has various methods including a counter or timer which returns a continuously updated float value.

I'm trying to connect those floats to a QWidget gui object such as QLCDnumber, QLineEdit, or QLabel to display the updated values.

Do I need a signal to connect to a gui widget slot? I'm having trouble finding a signal which would emit values from the object which returns floats from the linked api. I tried using the display method for a Qt gui object but those do not update continuously.

Am I going in the right direction with this? Should I make MyClass derive from a different parent class? thanks.

wysota
30th October 2008, 16:11
You should declare your own signal and emit it in your code.

jhearon
31st October 2008, 16:26
Thank You. I gave this a shot, but get "no such signal error".


class myClass : public QWidget

public:
QLabel *myLabel;
double timeVal;
int runapi();

public slots:
void setNum (double num);
signals:
void mySignal(double newnum);
private:
API *myapi;
-----
myClass::myClass()
{
myapi = new API;
mylabel = new QLabel;
connect (mylabel, SIGNAL ( mySignal(double) ),
mylabel, SLOT ( setNum(double) ) );
}
int myClass::runapi()
{
timeVal->myapi->someMethod();
mylabel->setNum(timeVal);

}
void myClass::setNum (double num)
{
emit mySignal(num);
}

wysota
31st October 2008, 18:50
You forgot the Q_OBJECT macro.

jhearon
1st November 2008, 19:18
I have the macro, but omitted it in pseudo-code ex., sorry.
Exact error is: Object::connect: No such signal QLabel::mySignal(double).
Is there something else I'm missing to create my own signal to connect to a QT slot?


//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 (mylabel, SIGNAL ( mySignal(double) ),
mylabel, SLOT ( setNum(double) ) );.
}
int myClass::runapi()
{
timeVal->myapi->someMethod(); //returns double
mylabel->setNum(timeVal);
}
void myClass::setNum (double num)
{
emit mySignal(num);
}

jhearon
1st November 2008, 21:10
I noticed one problem which was "this" or myclass is where mySignal was declared and I'm trying to connect it to mylabel or QLabel. But back to original problem which is cannot seem to get QLCDNumber, QLabel, or QLineEdit to display running update in realtime from linked api method which returns double h:m:s:ms, now that I have mySignal working.


connect (this, SIGNAL ( mySignal(double) ), mylabel, SLOT ( setNum(double) ) );.

jhearon
3rd November 2008, 16:41
I discovered I need to repaint() to update the widget. In my code (not shown) the call to setNum is within a while loop, thus the widget gets continuously updated; but it is very slow at the moment and need to figure out what is causing the slow down. Will end this thread here. Thanks.



void myClass::setNum (double num)
{
emit mySignal(num);
mylabel->repaint();
}

wysota
3rd November 2008, 19:59
I think you need to redesign your class. While loops in the gui thread are a bad thing to do. If you are a commercial Qt user, I suggest taking a look at the latest Qt Quarterly issue (#27).

jacek
3rd November 2008, 20:00
I discovered I need to repaint() to update the widget. In my code (not shown) the call to setNum is within a while loop
Rather than calling repaint() here, you should allow event loop to process events in that while loop.

See QCoreApplication::processEvents() and consider dropping that while loop in favor of a timer.