Reputation: 131
OS : windows xp SP2 , compiler : Code::Blocks ver. 10.05 , Qt 4.6
I recently started to learn Qt. At first all went well with simple tut examples. I soon came across an example that can not compile and and realized that something is wrong.
Here is the code :
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QDesktopWidget>
class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);
private slots:
void OnPlus();
void OnMinus();
private:
QLabel *label;
};
void center(QWidget *widget, int w, int h)
{
int x, y;
int screenWidth;
int screenHeight;
QDesktopWidget *desktop = QApplication::desktop();
screenWidth = desktop->width();
screenHeight = desktop->height();
x = (screenWidth - w) / 2;
y = (screenHeight - h) / 2;
widget->move( x, y );
}
Communicate::Communicate(QWidget *parent)
: QWidget(parent)
{
int WIDTH = 350;
int HEIGHT = 190;
resize(WIDTH, HEIGHT);
QPushButton *plus = new QPushButton("+", this);
plus->setGeometry(50, 40, 75, 30);
QPushButton *minus = new QPushButton("-", this);
minus->setGeometry(50, 100, 75, 30);
label = new QLabel("0", this);
label->setGeometry(190, 80, 20, 30);
connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
center(this, WIDTH, HEIGHT);
}
void Communicate::OnPlus()
{
int val = label->text().toInt();
val++;
label->setText(QString::number(val));
}
void Communicate::OnMinus()
{
int val = label->text().toInt();
val--;
label->setText(QString::number(val));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Communicate window;
window.setWindowTitle("Communicate");
window.show();
return app.exec();
}
When I try to open it, I get this message:
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| more undefined references to `vtable for Communicate' follow|
||=== Build finished: 6 errors, 0 warnings ===|
I was looking for a solution to the code:: blocks forum and learned that there should be Qt plugin installed.
So , I install QtWorkbench 0.6.0 alpha -> qt plugin but nothing has changed.
Any suggestion is welcome.
Upvotes: 0
Views: 1031
Reputation: 3156
Did you moc this file and include the moc output to get compiled?
Whenever you use the Q_OBJECT macro, you must use Qt's moc command on that file to generate a new cpp file which should also be included in the files to be compiled with your project. In addition, I believe that you can only moc a header file, so you will have to move your class definition to a separate file and moc that file.
I don't know how it works for your IDE, but on the command line you would call something like
<QT4 directory>\bin\moc.exe myfile.h -o moc_myfile.cpp
Then include the file moc_myfile.cpp in the project as well.
In some IDEs that is what the Qt plugin does for you; It automates all those steps or uses qmake which does not require explicit moc'ing. In Visual Studio I just use Custom Build Steps.
Upvotes: 2
Reputation: 12713
Try taking out the call to center() in the Communication constructor. I believe this might be causing your errors.
Upvotes: 0