PDA

View Full Version : [Signals & Slots] Custom class' signal not detected



Mr_Cloud
26th July 2012, 05:41
Hello All,

I've looked into how to write a class with QObject's inheritance so I can use signals as I need feedback from said class. The trouble is, I'm not sure if I defined the signal correctly, as it differs from the Signals & Slots example.

My code is as follows:



class worker : public QObject{
Q_OBJECT
public:
worker();

void execute (bool isExecuting){
if (isExecuting==true){
//do stuff
emit scanUpdate(); //differs from the example's ValueChanged(int newValue)
}
else return;
}

signals:
void scanUpdate();
}

class mainClass : public QMainWindow, Ui::MainWindow
{
Q_OBJECT

public:
mainClass() : QMainWindow()
{
setupUi(this);
lib=new worker;
connect(lib,SIGNAL(scanUpdate()),this,SLOT(pbScanU pdate()));

while(1){ lib->execute(true); }

}
worker *lib;
public slots:
void pbScanUpdate(){
//do stuff which can only occur after lib->execute has been called
}
}


This compiles ok with the Q_OBJECT declarations, but I never see pbScanUpdate() running. What could I be missing?


Thanks in advance,
Mr_Cloud

Santosh Reddy
26th July 2012, 06:09
You are missing "slot:" keyword before "void pbScanUpdate()" declaration/definition

ChrisW67
26th July 2012, 06:34
You are also ignoring the warning messages in your program output that will be telling you that there is no slot named pbScanUpdate().

Mr_Cloud
26th July 2012, 07:59
Sorry you're right. I copy-pasta'd wrong, there is actually public slots: declaration before void pbScanUpdate(). Top post edited.

FelixB
26th July 2012, 10:57
Are you sure the "emit"-statement gets called?

Santosh Reddy
26th July 2012, 11:35
It will not compile!

You better get the code compiling and then organize the code such that you can at-least insert break-points on individual line. Your code will work perfectly if you fix the "worker" constructor (implementation is missing, how come it will compile), and class terminating ";" (It will for sure not compile).

Also note that you have while(1) loop in ctor, you will never be able to see the window pop up. Breakpoint is the only way you can see it work.