PDA

View Full Version : class not compiling because of Q_OBJECT



seux
5th June 2011, 11:30
Hello,
when I inherit from a Qt class I generally struct the class like this:


class MyClass : public QDialog //or another Qt class
{
Q_OBJECT
public:
MyClass();
private:
QPushButton *okBtn;
//....
};

But on some classes I get the error:
undefined reference to `vtable for MyClass'

I don't know why Q_OBJECT sometimes works for my classes and sometimes not.

Zlatomir
5th June 2011, 11:42
Use two files per class: the class declaration in .h file and definition in .cpp file and it should work or else (if you write both definition and declaration in a cpp file) you might need to include the moc generated file, and if some particular piece of code doesn't work - post it here and we can see what is wrong with it.

seux
5th June 2011, 12:02
For excample this class called CreateFile.h:


#ifndef CREATEFILEDIALOG_H
#define CREATEFILEDIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QRadioButton>
#include <QLabel>
#include <QSpinBox>
#include <QComboBox>
#include <QLineEdit>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

class C_CreateFileDialog : public QDialog
{
//Q_OBJECT
public:
C_CreateFileDialog();

public slots:
void createFile();

private:
QLabel *describtion;
QLabel *sizeLabel;
QSpinBox *sizeSpin;
QComboBox *sizeCombo;
QGroupBox *filledWithGroup;
QRadioButton *RB_Letters;
QLineEdit *letters;
QRadioButton *RB_AutoText;

QPushButton *createFileBtn;
QPushButton *cancelBtn;

//Layouts
QHBoxLayout *sizeLayout;
QHBoxLayout *letterLayout;
QHBoxLayout *buttonLayout;
QVBoxLayout *radioLayout;
QVBoxLayout *layout;
};

#endif // CREATEFILEDIALOG_H

in the CreateFile.cpp is only the constructor in which the several items are placed and aligned to the layouts. Also the connections to the corresponding buttons are made.The createFile slot is also there but does nothing yet.

If I comment out the Q_OBJECT the class compiles fine, but when I remove the // I get the errors.

SixDegrees
5th June 2011, 12:23
Do a 'make clean' on your project. Run qmake again. Run make again. See what happens.

seux
5th June 2011, 12:36
ah, thanks. Now its working. :D