PDA

View Full Version : doubts in signal and slots



sar_van81
2nd April 2007, 06:30
hi everyone,

i had written an application wherein there will be single pushbutton and when i click the button and set of comments will appear. i had written as the following:



class demo: public QWidget
{
public:
demo(QWidget *parent=0,const char *name=0);
~demo();
public slots:
void change();

};

demo::demo(QWidget *parent,const char *name):
QWidget(parent,name)
{
setMinimumSize(640,480);

QPushButton *clear= new QPushButton("clear",this,"clear");
clear->setGeometry(10,10,50,20);
clear->setFont(QFont("Times",12,QFont::Bold));
connect(clear,SIGNAL(clicked()),this,SLOT(change() ));
}

demo::~demo()
{
}

void demo::change()
{
printf("\n change \n");
}


this is all the code. but when i execute the program i get the error as

"QObject::connect: No such slot QWidget::change()
QObject::connect: (sender name: 'clear')
QObject::connect: (receiver name: 'unnamed')
".
but the same code works if i had declared the class in an header file and defined it in a cpp file and the main program in a main.cpp file.
can anyone say me why is this not working ? is my coding correct ?

thanks in advance,

saravanan

marcel
2nd April 2007, 06:36
The Qt Meta Object Compiler needs the class definition in a header. It generates a source file from this header that will map the signals in your class to the slots you connect them to.

Also, you have to add the Q_OBJECT macro to your class definition.

I hope this helps.

Regards,
Marcel.

sar_van81
2nd April 2007, 07:59
hi,

i tried that too. but it said
"
In function `ConnectWidget::~ConnectWidget()':
undefined reference to `vtable for ConnectWidget'
undefined reference to `vtable for ConnectWidget'
.obj/release-shared-mt-emb-x86/connect.o: In function `ConnectWidget::~ConnectWidget()':
undefined reference to `vtable for ConnectWidget'
undefined reference to `vtable for ConnectWidget'
.obj/release-shared-mt-emb-x86/connect.o: In function `ConnectWidget::~ConnectWidget()':
collect2: ld returned 1 exit status
make: *** [drawlines] Error 1".

as you said the moc file is required i think. but i gave as follows also:
"#include "drawlines.moc" "

before main function. then also it said

"error: drawlines.moc: No such file or directory".

vermarajeev
2nd April 2007, 08:11
Use this

class demo: public QWidget
{
Q_OBJECT
public: demo(QWidget *parent=0,const char *name=0);
~demo();
public slots:
void change();
};

Then rerun qmake again.
qmake *.pro
make

sar_van81
2nd April 2007, 09:16
hi,

i did that but still the same error :(.

marcel
2nd April 2007, 09:29
You must not include the generated moc in your source file.
Implement everything normally( h + cpp ), and let qmake to its job.

Also, take a look at your project file to see if everything is ok.

vermarajeev
2nd April 2007, 09:40
hi,

i did that but still the same error :(.

Follow this link

http://www.qtcentre.org/forum/f-qt-programming-2/t-trying-to-get-basic-signals-example-working-5855.html/?highlight=moc

sar_van81
2nd April 2007, 12:31
hi,

thank you for that link. its working fine. actually i added the moc file before the main after the class definition.so thats why it did not work. now i added the moc file at the end of the cpp file.

Also thank you to all them who gave suggestion.