views:

39

answers:

2

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.

A: 

Try taking out the call to center() in the Communication constructor. I believe this might be causing your errors.

JimDaniel
I have done so but there is no solution.//center(this, WIDTH, HEIGHT); gives the same result.The other examples do not work and it seems to me that the thing is in the settings for qt project in code::blocks.
nik_02
+2  A: 

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.

Dusty Campbell
No and I dont now how to do that!
nik_02
I hope that the Qt plugin does this thing but I am not sure.
nik_02
Look in the settings for the QtWorkbench. I think it has a setting for the moc directory; Something like "mocs". Then look in your project folder to see if there is a directory there and if there are any files in it.
Dusty Campbell
Further reading of the Qt documentation leaves me to believe that you cannot moc a cpp file, only a header file. Also I believe that the QtWorkbench uses qmake which does not require explicit moc'ing. I updated my answer.
Dusty Campbell
I just looked.There is no settings for plugin QtWorkbench. When I open MANAGE PLUGINS, it just gives me options : enable plugin , disable , install new and uninstall. It seems to me that i can not do further adjustment.
nik_02
Hmmm. I was looking at http://code.google.com/p/qtworkbench/wiki/BuildingQtProjects and that's why I thought there was a setting.
Dusty Campbell
Yes, you're right. I opened the options in the project and there is QtWorkbench settings. SORRY , will try now experiment with that
nik_02
First, try moving the class definition to a separate header file.
Dusty Campbell
OK I will do that.
nik_02