Cant understand Signal Slot Syntax
-
Hi
i have created a little code for understanding the Signal / Slot Syntax but i always get "../blubber.cpp:-1: Error:undefined reference to `vtable for blubber'
blubber.cpp
@#include "blubber.h"blubber::blubber()
{
}void blubber::test(){
emit this->testsignal(10);
}
@blubber.h
@#ifndef BLUBBER_H
#define BLUBBER_H#include <QObject>
class blubber:public QObject
{
Q_OBJECTpublic:
blubber();
void test();signals:
void testsignal(int);
};#endif // BLUBBER_H
@main.cpp
@#include <QtGui/QApplication>
#include "mainwindow.h"
#include "blubber.h"int main(int argc, char *argv[])
{
blubber *blub=new blubber;QApplication a(argc, argv); MainWindow w; w.show(); QObject::connect(blub,SIGNAL(testsignal(int)),&w,SLOT(abc(int))); return a.exec();
}
@What i am doing wrong?
-
Did you try running qmake?
-
thx
qmake solved this why does this happen?
-
Do you have the abc(int) slot declared in your MainWindow class? Where do you call the blubber.test() method so the signal is actually emitted??
Anyway, there is nothing all that complex about the syntax - declaration of signals and slots is straightforward, signals are just declarations, slots are like regular functions.
When you connect you specify which QObject derived class and which signal you want to connect to which QObject derived class and slot.
In Qt5 there is additional syntax for signals and slots, allowing to connect signals to either regular functions (not declared as slots) and even inline lambda expressions. It even does compile time checking, which the old syntax does not.
http://qt-project.org/wiki/New_Signal_Slot_Syntax -
[quote author="hexenbrennen" date="1356901927"]thx
qmake solved this why does this happen?[/quote]
Sometimes it 'misses' to run qmake.
-
[quote author="hexenbrennen" date="1356901927"]qmake solved this why does this happen?[/quote]
A vtable is a compiler generated internal table used to handle virtual functions in a class. If the class has no declared virtuals (not inherited) then a vtable is not generated.
Blubber without the Q_OBJECT macro has no virtuals. Adding Q_OBJECT gives blubber some virtual function declarations that tie to code generated by moc. Until the moc code is generated and linked the now-required vtable is missing.
Using the Q_OBJECT macro as a marker, qmake arranges Makefile entries to run moc. The Makefile does not have moc commands for the file if the marker is added after qmake is run. Re-running qmake regenerates the Makefile, and a subsequent make runs moc on the file, generates the code, and satisfies the missing linker names.