PDA

View Full Version : Q_OBJECT error



Toshikazu
27th May 2008, 12:09
Hey, my code compiles fine but I cannot connect my own functions to a signal. Is the reason because i have not put the Q_OBJECT macro in the class header file for the window?

if so when i do put this macro in i get 2 errors.

make: *** [debug] Error 2
make[1]: *** [debug\buttonTest.exe] Error 1

I was hoping if i fixed this then i would be able to connect signals to my own functions.

-----------------


class Window : public QWidget
{
Q_OBJECT

public:
Window(QWidget *parent = 0)
{
button = new QPushButton("Increment", this);
lcd = new QLCDNumber(2, this);
slider = new QSlider(Qt::Horizontal);
slider->setRange(0,99);
slider->setValue(0);

QObject::connect(slider, SIGNAL(valueChanged(int)),
this, SLOT(Update(int)));
//lcd, SLOT(display(int)));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(slider);
layout->addWidget(lcd);
layout->addWidget(button);
this->setLayout(layout);

}

private slots:
void Update(int i)
{
lcd->display(i);
}


private:
QPushButton *button;
QLCDNumber *lcd;
QSlider *slider;
};




//.pro file
TEMPLATE = app
TARGET = buttonTest
QT += core \
gui
HEADERS += buttontest.h
SOURCES += main.cpp \
buttontest.cpp
FORMS += buttontest.ui
RESOURCES +=

lyuts
27th May 2008, 13:19
Q_OBJECT macro should be in your class definition and your function should be declared in "slots" section. In this case you'll be able to do "your connect".

Toshikazu
27th May 2008, 13:46
Ok thanks.

But when i try to include the Q_OBJECT macro i get those errors :(?

mazurekwrc
27th May 2008, 14:12
do you place Q_OBJECT in private section of your class ?
and remeber that your class should inherit someway from QObject class

ChristianEhrlicher
27th May 2008, 14:13
There must be an earlier problem - the lines you post don't tell us anything. Also you did not tell us how you create the makefile. If you're using qmake then also show us your pro-File.

vycke
27th May 2008, 14:42
Did you also make sure that your class derives from QObject (either directly or indirectly)?
Vycke

Toshikazu
27th May 2008, 15:10
I have edited my original post to show the code for my class and for my .pro file. Although i am not that clear yet on the purpose of the .pro file.

ChristianEhrlicher
27th May 2008, 15:16
Did you rerun qmake after you added Q_OBJECT to your header?
And what's the actual error - as I said the error message you gave us isn't the first one you see and we can't say anything about the real problem.
The pro-file looks fine.

OT: But why did you put the implementation into the header? This isn't a good coding style.

Toshikazu
27th May 2008, 15:48
Hi, thnanks for your time.

I put the implementation in the header, becauase when I do not i get an additional error message. Even if it is all in the same file.

undefined reference to `vtable for Window'

Also I am new to eclipse and qt, This might sound dumb but am I not rerunning qmake when i save the source and build all and run? or do i need to do something else.

Thanks

ChristianEhrlicher
27th May 2008, 15:50
You have to rerun qmake every time you add/remove Q_OBJECT in an header - otherwise qmake can't create the moc calls for the headers and you get a vtable linker error.

Toshikazu
28th May 2008, 03:27
How do I rerun qmake and what files do i need to pass.
Or does qmake get invoked by eclipse everytime I do a build project?

I tried this on cmd but I still get the errors.

qmake -makefile -win32 C:/Eclipse/buttonTest/buttonTest.pro
qmake -project -win32 C:/Eclipse/buttonTest/buttonTest.pro

and they both didn't output any errors when i run them. But still errors when i try to build the project.

Sorry my understanding is low.