PDA

View Full Version : signals and slots, test example doesn't compile



htroyo
16th April 2011, 05:10
hello, i'm new using Qt, i tried to run an example using custom slots, here is the code:


#include <QObject>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLabel>

class Class1 : public QWidget {
Q_OBJECT
public:
Class1(QWidget *p = 0) : QWidget(p) {
b = new QPushButton("Click",this);
l = new QLabel("Label",this);
b->resize(100,30);
b->move(10,60);
l->resize(100,30);
l->move(10,10);
QObject::connect(b,SIGNAL(clicked()),this,SLOT(set l()));
}
public slots:
void setl() {
l->setText("TEXT123");
}

private:
QPushButton *b;
QLabel *l;
};

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

Class1 *c = new Class1();
c->move(300,100);
c->resize(120,100);
c->show();

return app.exec();
}

and if i try to build the app i get this error:

:: error: collect2: ld returned 1 exit status
what is wrong?

Added after 19 minutes:

well, i found the solution, it's because i was doing that in a bad way, i use a Class1.h, Class1.cpp and main.cpp as i should do...
Sometimes i'm too lazy and i just write down all my code in a single file because i dont want to use .h's hahahahha, well i know how to do it later, thanks anyway

dbzhang800
16th April 2011, 13:39
add such a line:

#include "main.moc"