Results 1 to 5 of 5

Thread: Problems with Q_OBJECT and subclassing

  1. #1
    Join Date
    Feb 2006
    Location
    Norwalk, CT, USA
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Problems with Q_OBJECT and subclassing

    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
    Qt Code:
    1. // declaration (CGLFrame.h)
    2. #ifndef CGLFRAME_H
    3. #define CGLFRAME_H
    4.  
    5. #include "GLee.h"
    6. #include <qgl.h>
    7. #include <qframe.h>
    8. #include <qpaintdevice.h>
    9. #include <qapplication.h>
    10. #include <qevent.h>
    11. #include <qmessagebox.h>
    12. #include <qstring.h>
    13. #include <qfileinfo.h>
    14. #include <qtimer.h>
    15.  
    16. #include <stdlib.h>
    17. #include <stdio.h>
    18. #include <stdarg.h>
    19.  
    20. class CGLFrame : public QFrame
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. CGLFrame(QWidget * parent = 0, const char * name = 0);
    26. virtual ~CGLFrame();
    27. virtual void InitGLScene();
    28. virtual void ResizeGLScene();
    29. virtual void RenderGLScene(int pick);
    30. virtual void KillGLScene();
    31. virtual void InitGLSL();
    32. virtual void CheckGLError();
    33.  
    34. private:
    35. // enum PickMode { PickNone, PickWidget };
    36. int m_iPickMode;
    37. QGLContext * m_cContext;
    38. QTimer timerGL;
    39. char * readShaderFile(const char * filename);
    40. virtual void mousePressEvent(QMouseEvent * event);
    41. virtual void mouseReleaseEvent(QMouseEvent * event);
    42. virtual void mouseMoveEvent(QMouseEvent * event);
    43. virtual void resizeEvent(QResizeEvent * event);
    44. virtual void paintEvent(QPaintEvent * event);
    45. virtual void showEvent(QShowEvent * event);
    46. virtual void info(const char * title, const char * msg, ...);
    47.  
    48. public slots:
    49. void timerGLFire();
    50. };
    51. #endif
    To copy to clipboard, switch view to plain text mode 

    and class implementation as follows:

    Qt Code:
    1. #include "CGLFrame.h"
    2.  
    3. CGLFrame::CGLFrame(QWidget * parent, const char * name)
    4. : QFrame(parent, name),
    5. m_iPickMode(0),
    6. m_cContext(0)
    7. {
    8. QGLFormat format;
    9. m_cContext = new QGLContext(format, (QPaintDevice *)this);
    10. m_cContext->create();
    11.  
    12. InitGLScene();
    13. }
    14.  
    15. void CGLFrame::CheckGLError()
    16. {
    17. int err;
    18. if ((err = glGetError()) != GL_NO_ERROR)
    19. {
    20. QString s = QString((char *)gluErrorString(err));
    21. info("GL error!", "%s\n", s.data());
    22. }
    23. }
    24.  
    25. void CGLFrame::info(const char * title, const char * msg, ...)
    26. {
    27. ... format message
    28. QMessageBox::information(0, title, text);
    29. }
    30.  
    31. void CGLFrame::InitGLScene()
    32. {
    33. m_cContext->makeCurrent();
    34.  
    35. // initialize GLSL
    36. InitGLSL();
    37.  
    38. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    39. glClearDepth(1.0f);
    40. ResizeGLScene();
    41.  
    42. connect(&timerGL, SIGNAL(timeout()), this, SLOT(timerGLFire()));
    43. timerGL.start(0, FALSE);
    44. }
    45.  
    46. void CGLFrame::InitGLSL()
    47. {
    48. ... init GLSL
    49. }
    50.  
    51. char * CGLFrame::readShaderFile(const char * filename)
    52. {
    53. ... read shader file
    54. }
    55.  
    56. void CGLFrame::ResizeGLScene()
    57. {
    58. m_cContext->makeCurrent();
    59. glViewport(0, 0, width(), height());
    60.  
    61. glMatrixMode(GL_PROJECTION);
    62. glLoadIdentity();
    63.  
    64. glOrtho(-10.0, 10.0, 0.0, 10.0, -10.0, 10.0);
    65.  
    66. glMatrixMode(GL_MODELVIEW);
    67. glLoadIdentity();
    68.  
    69. }
    70.  
    71. void CGLFrame::RenderGLScene(int pick)
    72. {
    73. m_cContext->makeCurrent();
    74. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    75. glFlush();
    76.  
    77. if(m_cContext->format().doubleBuffer())
    78. m_cContext->swapBuffers();
    79. }
    80.  
    81. void CGLFrame::KillGLScene()
    82. {
    83. timerGL.stop();
    84. delete m_cContext;
    85. return;
    86. }
    87.  
    88.  
    89. void CGLFrame::mousePressEvent(QMouseEvent * event)
    90. {
    91. int mx = event->x();
    92. int my = event->y();
    93. }
    94.  
    95. void CGLFrame::mouseReleaseEvent(QMouseEvent * event)
    96. {
    97. int mx = event->x();
    98. int my = event->y();
    99. }
    100.  
    101. void CGLFrame::mouseMoveEvent(QMouseEvent * event)
    102. {
    103. int mx = event->x();
    104. int my = event->y();
    105. }
    106.  
    107. void CGLFrame::resizeEvent(QResizeEvent * event)
    108. {
    109. ResizeGLScene();
    110. }
    111.  
    112. void CGLFrame::paintEvent(QPaintEvent * event)
    113. {
    114. }
    115.  
    116. void CGLFrame::timerGLFire()
    117. {
    118. RenderGLScene(m_iPickMode);
    119. }
    120.  
    121. void CGLFrame::showEvent(QShowEvent * event)
    122. {
    123.  
    124. }
    125.  
    126. CGLFrame::~CGLFrame()
    127. {
    128. KillGLScene();
    129. }
    To copy to clipboard, switch view to plain text mode 

    and in my ui.h:

    Qt Code:
    1. #include "CGLFrame.h"
    2.  
    3. CGLFrame * cGLFrame2DLUT = 0;
    4.  
    5. void CFormControls::init()
    6. {
    7. cGLFrame2DLUT = new CGLFrame( tab2DLUT, "cGLFrame2DLUT" );
    8. cGLFrame2DLUT->setGeometry( QRect( 20, 20, 256, 256 ) );
    9. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with Q_OBJECT and subclassing

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with Q_OBJECT and subclassing

    Try moving this:
    Qt Code:
    1. #include "GLee.h"
    2. #include <stdlib.h>
    3. #include <stdio.h>
    4. #include <stdarg.h>
    To copy to clipboard, switch view to plain text mode 
    to .cpp file (place it after #include "CGLFrame.h").

  4. #4
    Join Date
    Feb 2006
    Location
    Norwalk, CT, USA
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Re: Problems with Q_OBJECT and subclassing

    Hi Codepoet, no change, I think <qobjectdefs.h> is included somewhere further up the chain anyhow.

  5. #5
    Join Date
    Feb 2006
    Location
    Norwalk, CT, USA
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Talking Re: Problems with Q_OBJECT and subclassing

    Hi Jacek, that did the trick! Thanks so much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.