PDA

View Full Version : Problems with Q_OBJECT and subclassing



renaissanz
21st February 2006, 22:51
Hi all, using Qt 3.3.4 free X11 on Fedora Core 4 Linux (i386).

Trying to create a custom widget like so:

Class declaration as follows


// declaration (CGLFrame.h)
#ifndef CGLFRAME_H
#define CGLFRAME_H

#include "GLee.h"
#include <qgl.h>
#include <qframe.h>
#include <qpaintdevice.h>
#include <qapplication.h>
#include <qevent.h>
#include <qmessagebox.h>
#include <qstring.h>
#include <qfileinfo.h>
#include <qtimer.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

class CGLFrame : public QFrame
{
Q_OBJECT

public:
CGLFrame(QWidget * parent = 0, const char * name = 0);
virtual ~CGLFrame();
virtual void InitGLScene();
virtual void ResizeGLScene();
virtual void RenderGLScene(int pick);
virtual void KillGLScene();
virtual void InitGLSL();
virtual void CheckGLError();

private:
// enum PickMode { PickNone, PickWidget };
int m_iPickMode;
QGLContext * m_cContext;
QTimer timerGL;
char * readShaderFile(const char * filename);
virtual void mousePressEvent(QMouseEvent * event);
virtual void mouseReleaseEvent(QMouseEvent * event);
virtual void mouseMoveEvent(QMouseEvent * event);
virtual void resizeEvent(QResizeEvent * event);
virtual void paintEvent(QPaintEvent * event);
virtual void showEvent(QShowEvent * event);
virtual void info(const char * title, const char * msg, ...);

public slots:
void timerGLFire();
};
#endif


and class implementation as follows:



#include "CGLFrame.h"

CGLFrame::CGLFrame(QWidget * parent, const char * name)
: QFrame(parent, name),
m_iPickMode(0),
m_cContext(0)
{
QGLFormat format;
m_cContext = new QGLContext(format, (QPaintDevice *)this);
m_cContext->create();

InitGLScene();
}

void CGLFrame::CheckGLError()
{
int err;
if ((err = glGetError()) != GL_NO_ERROR)
{
QString s = QString((char *)gluErrorString(err));
info("GL error!", "%s\n", s.data());
}
}

void CGLFrame::info(const char * title, const char * msg, ...)
{
... format message
QMessageBox::information(0, title, text);
}

void CGLFrame::InitGLScene()
{
m_cContext->makeCurrent();

// initialize GLSL
InitGLSL();

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
ResizeGLScene();

connect(&timerGL, SIGNAL(timeout()), this, SLOT(timerGLFire()));
timerGL.start(0, FALSE);
}

void CGLFrame::InitGLSL()
{
... init GLSL
}

char * CGLFrame::readShaderFile(const char * filename)
{
... read shader file
}

void CGLFrame::ResizeGLScene()
{
m_cContext->makeCurrent();
glViewport(0, 0, width(), height());

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(-10.0, 10.0, 0.0, 10.0, -10.0, 10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void CGLFrame::RenderGLScene(int pick)
{
m_cContext->makeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();

if(m_cContext->format().doubleBuffer())
m_cContext->swapBuffers();
}

void CGLFrame::KillGLScene()
{
timerGL.stop();
delete m_cContext;
return;
}


void CGLFrame::mousePressEvent(QMouseEvent * event)
{
int mx = event->x();
int my = event->y();
}

void CGLFrame::mouseReleaseEvent(QMouseEvent * event)
{
int mx = event->x();
int my = event->y();
}

void CGLFrame::mouseMoveEvent(QMouseEvent * event)
{
int mx = event->x();
int my = event->y();
}

void CGLFrame::resizeEvent(QResizeEvent * event)
{
ResizeGLScene();
}

void CGLFrame::paintEvent(QPaintEvent * event)
{
}

void CGLFrame::timerGLFire()
{
RenderGLScene(m_iPickMode);
}

void CGLFrame::showEvent(QShowEvent * event)
{

}

CGLFrame::~CGLFrame()
{
KillGLScene();
}


and in my ui.h:



#include "CGLFrame.h"

CGLFrame * cGLFrame2DLUT = 0;

void CFormControls::init()
{
cGLFrame2DLUT = new CGLFrame( tab2DLUT, "cGLFrame2DLUT" );
cGLFrame2DLUT->setGeometry( QRect( 20, 20, 256, 256 ) );
}


When I compile, here's my problem:

g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I/usr/X11R6/include -I/usr/X11R6/include -I.ui/ -I. -I.moc/ -o .obj/CGLFrame.o CGLFrame.cpp
/usr/lib/qt-3.3/include/qnamespace.h:835: error: expected identifier before numeric constant
/usr/lib/qt-3.3/include/qnamespace.h:835: error: expected unqualified-id before numeric constant
/usr/lib/qt-3.3/include/qevent.h:61: error: expected identifier before numeric constant
/usr/lib/qt-3.3/include/qevent.h:61: error: expected `}' before numeric constant
/usr/lib/qt-3.3/include/qevent.h:61: error: expected unqualified-id before numeric constant
/usr/lib/qt-3.3/include/qevent.h:142: error: expected `)' before ‘type’
/usr/lib/qt-3.3/include/qevent.h:143: error: declaration of ‘~QEvent’ as non-member
/usr/lib/qt-3.3/include/qevent.h:144: error: ‘Type’ does not name a type
/usr/lib/qt-3.3/include/qevent.h:145: error: non-member function ‘bool spontaneous()’ cannot have cv-qualifier
/usr/lib/qt-3.3/include/qevent.h: In function ‘bool spontaneous()’:
/usr/lib/qt-3.3/include/qevent.h:145: error: ‘spont’ was not declared in this scope
/usr/lib/qt-3.3/include/qevent.h: At global scope:
/usr/lib/qt-3.3/include/qevent.h:146: error: expected unqualified-id before ‘protected’
/usr/lib/qt-3.3/include/qevent.h:148: error: expected unqualified-id before ‘private’
/usr/lib/qt-3.3/include/qevent.h:150: error: invalid function declaration
/usr/lib/qt-3.3/include/qevent.h:153: error: ‘friend’ can only be specified inside a class
/usr/lib/qt-3.3/include/qevent.h:154: error: ‘friend’ can only be specified inside a class
/usr/lib/qt-3.3/include/qevent.h:155: error: ‘friend’ can only be specified inside a class
/usr/lib/qt-3.3/include/qevent.h:156: error: ‘friend’ can only be specified inside a class
/usr/lib/qt-3.3/include/qevent.h:157: error: expected declaration before ‘}’ token
make: *** [.obj/CGLFrame.o] Error 1

Now, why the heck is my custom widget even affecting qnamespace.h & qevent.h? Why do my errors stop happening when I take the Q_OBJECT macro out of the class declaration? I'd just leave it out, but I need to have a slot for my timer!

Any ideas? Many thanks in advance! :)

Codepoet
21st February 2006, 23:09
I don't know Qt 3 but it looks like you are missing the header with the Q_OBJECT definition. The (internal?) header qobjectdefs.h is not included by any of your includes.

jacek
21st February 2006, 23:14
Try moving this:
#include "GLee.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>to .cpp file (place it after #include "CGLFrame.h").

renaissanz
21st February 2006, 23:15
Hi Codepoet, no change, I think <qobjectdefs.h> is included somewhere further up the chain anyhow.

renaissanz
21st February 2006, 23:18
Hi Jacek, that did the trick! Thanks so much!