PDA

View Full Version : problem with slots



babu198649
31st March 2008, 09:01
hi
in the following code the signal is not connected to slot.


#include <QApplication>
#include <QtGui>

class push : public QPushButton
{

public slots:
void hi();

public:
push(QWidget *parent=0);
};


push::push(QWidget *parent) :
QPushButton(parent)
{
connect(this, SIGNAL(clicked()), this, SLOT(hi()));
}

void push::hi()
{
qDebug()<<"hioo";
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

push W;
W.show();

return app.exec();
}


this code compiles and executes but gives the warning
Object::connect: No such slot QPushButton::hi()

where did the code wnt wrong

Krish
31st March 2008, 09:14
Hey babu198649,
You are using the clicked() signal to connect to the slot hi(), but where is the button which after clicking will emit the signal ????

mattia
31st March 2008, 09:15
How did u declared hi() slot?

wysota
31st March 2008, 09:35
The Q_OBJECT macro needs to be present in every class declaring a signal or a slot and moc has to be run on the header file.

Krish
31st March 2008, 09:43
Ohh ya I overlooked the fault which Wysota has said.

So Yeap as Wysota has mentioned you have to declare the Q_OBJECT macro in your class to use the signal !!!!

babu198649
31st March 2008, 10:37
thanks to all for u r reply


You are using the clicked() signal to connect to the slot hi(), but where is the button which after clicking will emit the signal ????

i have subclassed the QPushButton and in the connect function i am using this pointer .ie. the same pushbutton signal is connected to the slot.


The Q_OBJECT macro needs to be present in every class declaring a signal or a slot and moc has to be run on the header file.

it worked after declaring the class in the header file(with Q_OBJECT declaration in it) and defining the class in .cpp file.



but if i do both class declaration and definition in same file ,and even after including the Q_OBJECT macro i am getting the following error.


release/main.o(.text+0x1a): In function `push::push(QWidget*)':
make[1]: Leaving directory `/home/bala/workspace/gg'
: undefined reference to `vtable for push'
release/main.o(.text+0x22): In function `push::push(QWidget*)':
: undefined reference to `vtable for push'
release/main.o(.text+0x8a): In function `push::push(QWidget*)':
: undefined reference to `vtable for push'
release/main.o(.text+0x92): In function `push::push(QWidget*)':
: undefined reference to `vtable for push'
release/main.o(.text+0x3dc): In function `main':
: undefined reference to `vtable for push'
release/main.o(.text+0x3e5): more undefined references to `vtable for push' follow
collect2: ld returned 1 exit status
make[1]: *** [gg] Error 1
make: *** [release] Error 2


i have defined the class only after the declaration , even then why do i need two files(header and source file, when there is a single class).

jpn
31st March 2008, 10:51
Add

#include "main.moc"
at the end of main.cpp or put the class declaration in a separate header file. Then, re-run qmake.

babu198649
31st March 2008, 11:10
hi
its working after including #include "main.moc" at the end.

if i use both header and source file for a class ,then do i dont have to bother about the moc issues.

jpn
31st March 2008, 11:50
if i use both header and source file for a class ,then do i dont have to bother about the moc issues.
You will still have to re-run qmake after adding/removing Q_OBJECT macro to update necessary rules in the makefile(s). But you're right, you don't have to include the moc file by hand provided that the header is properly listed in the .pro file.