0

I'm trying to program a Qt GUI in C++. Here is the code:

sample.h:

#ifndef SAMPLE_H
#define SAMPLE_H

#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>
#include <QtGui/QGridLayout>
#include <QtGui/QLineEdit>
#include <qobject.h>

class Sample : public QObject
{
    Q_OBJECT
public:
    Sample();
public slots:
    void buttonPressed();
private:
    QWidget *widget;
    QGridLayout *layout;
    QLineEdit *le;
    QPushButton *button;
};

#endif // SAMPLE_H

sample.cpp:

#include "sample.h"

Sample::Sample()
{
    widget = new QWidget();
    widget->setWindowTitle("Sample");
    layout = new QGridLayout();

    le = new QLineEdit();

    button = new QPushButton();
    button->setText("Button");

    connect(button, SIGNAL(clicked()), this, SLOT(buttonPressed()));
    layout->addWidget(le, 0, 0);
    layout->addWidget(button, 1 , 0);

    widget->resize(300, 300);
    widget->setLayout(layout);
    widget->show();
}

void Sample::buttonPressed(){
    le->setText("pressed");
}

I obtain this error when building:

error: undefined reference to `vtable for Sample'

I'm using QtCreator from official Qt webpage.

Does anybody know what to do to make it work? Thank you very much for your responses :)

NG_
  • 6,895
  • 7
  • 45
  • 67
Reshi
  • 799
  • 4
  • 15
  • 32

1 Answers1

3

This kind of error usually occurs if you add the Q_OBJECT macro after having written and compiling your class. Rerunning qmake will usually fix it.

Chris
  • 17,119
  • 5
  • 57
  • 60
  • the problem is solved :) i cleaned the project rerunned qmake and it works:) thanks very much:) – Reshi Oct 16 '11 at 19:19