0

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.

4
  • Can you compile a Qt application at all? Like if you just instantiated a QWidget in main and showed it? Commented Jun 22, 2010 at 20:18
  • Yes, I can compile a Qt application. There is a default example when i start new Qt4 project and all goes well. Commented Jun 22, 2010 at 20:21
  • Do you #include "Communication.h" in main.cpp? Commented Jun 22, 2010 at 20:53
  • No , I done just like i posted. Commented Jun 22, 2010 at 21:08

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

No and I dont now how to do that!
I hope that the Qt plugin does this thing but I am not sure.
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.
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.
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.
|
0

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.