Results 1 to 2 of 2

Thread: [SOLVED] QMainWindow, QGLWidget - problems with QKeyEvents

  1. #1
    Join Date
    Nov 2008
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post [SOLVED] QMainWindow, QGLWidget - problems with QKeyEvents

    Hey everyone,
    I am new to Qt and especially OpenGL...So here is my question.
    If i sublass QMainWindow to create main window with menus and status bar that also contains one and only centralwidget (a QGLWidget to be more specific) i cant grab of the events from that QGLWidget.
    If i just show the widget and dont use QMainWindow everything is ok !
    Any help ?

    mainwindow.h
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QWidget>
    6.  
    7. class GLWidget;
    8. class QAction;
    9. class QMenu;
    10.  
    11. class Window : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Window();
    17.  
    18. private slots:
    19. void about();
    20.  
    21. private:
    22. void createActions();
    23. void createMenus();
    24. void createStatusBar();
    25.  
    26. QMenu *fileMenu;
    27. QMenu *helpMenu;
    28.  
    29. QAction *exitAction;
    30. QAction *aboutAction;
    31. QAction *aboutQtAction;
    32.  
    33. GLWidget *glWidget;
    34.  
    35. };
    36.  
    37. #endif
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3. #include "glwidget.h"
    4.  
    5. Window::Window()
    6. {
    7. glWidget = new GLWidget;
    8. setCentralWidget(glWidget);
    9.  
    10. createActions();
    11. createMenus();
    12. createStatusBar();
    13.  
    14. setWindowTitle(tr("Blank OpenGL Environment"));
    15. }
    16.  
    17. void Window::createActions()
    18. {
    19. exitAction = new QAction(tr("E&xit"), this);
    20. exitAction->setShortcut(tr("Ctrl+Q"));
    21. exitAction->setStatusTip(tr("Exit the application"));
    22. connect(exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
    23.  
    24. aboutAction = new QAction(tr("&About"), this);
    25. aboutAction->setStatusTip(tr("Show the application's About box"));
    26. connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
    27.  
    28. aboutQtAction = new QAction(tr("About &Qt"), this);
    29. aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
    30. connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    31. }
    32.  
    33. void Window::createMenus()
    34. {
    35. fileMenu = menuBar()->addMenu(tr("&File"));
    36. fileMenu->addAction(exitAction);
    37.  
    38. helpMenu = menuBar()->addMenu(tr("&Help"));
    39. helpMenu->addAction(aboutAction);
    40. helpMenu->addAction(aboutQtAction);
    41. }
    42.  
    43. void Window::createStatusBar()
    44. {
    45. statusBar()->showMessage(tr("Ready"));
    46. }
    47.  
    48. void Window::about()
    49. {
    50. QMessageBox::about(this, tr("About OpenGL"),
    51. tr("<h2>OpenGL Learning</h2>"
    52. "<p>Copyright &copy;2008 xerion :D"));
    53. }
    To copy to clipboard, switch view to plain text mode 

    Now lets move to glwidget.h
    Qt Code:
    1. #ifndef GLWIDGET_H
    2. #define GLWIDGET_H
    3.  
    4. #include <QGLWidget>
    5.  
    6. class QTimer;
    7.  
    8. class GLWidget : public QGLWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. GLWidget(QWidget *parent=0);
    14. ~GLWidget();
    15.  
    16. public slots:
    17. void timeOutSlot();
    18.  
    19. protected:
    20. void initializeGL();
    21. void paintGL();
    22. void resizeGL(int width, int height);
    23. void timeOut();
    24. void keyPressEvent(QKeyEvent *e);
    25.  
    26. private:
    27. void makeObject();
    28. void loadGLTextures();
    29.  
    30. QTimer *m_timer;
    31.  
    32. GLfloat rotx, roty;
    33. GLfloat z;
    34. GLfloat drotx, droty;
    35.  
    36. GLfloat LightAmbient[4];
    37. GLfloat LightDiffuse[4];
    38. GLfloat LightPosition[4];
    39.  
    40. bool light;
    41. GLuint filter;
    42. GLuint texture[3];
    43. };
    44.  
    45. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include <QtOpenGL>
    3. #include <math.h>
    4. #include "glwidget.h"
    5. #include <qapplication.h>
    6. #include <qtimer.h>
    7.  
    8. //constructor
    9. GLWidget::GLWidget(QWidget *parent)
    10. : QGLWidget(parent)
    11. {
    12. m_timer = new QTimer( this );
    13. connect( m_timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );
    14. m_timer->start( 50 );
    15.  
    16. LightAmbient[0]= 0.5f;
    17. LightAmbient[1]= 0.5f;
    18. LightAmbient[2]= 0.5f;
    19. LightAmbient[3]= 1.0f;
    20.  
    21. LightDiffuse[0]= 1.0f;
    22. LightDiffuse[1]= 1.0f;
    23. LightDiffuse[2]= 1.0f;
    24. LightDiffuse[3]= 1.0f;
    25.  
    26. LightPosition[0]= 0.0f;
    27. LightPosition[1]=0.0f;
    28. LightPosition[2]=2.0f;
    29. LightPosition[3]=1.0f;
    30.  
    31. rotx = roty = 0.0f;
    32. z = -5.0f;
    33. drotx = droty = 0.0f;
    34. filter = 0;
    35. light = true;
    36. }
    37.  
    38. //destructor
    39. GLWidget::~GLWidget()
    40. {
    41. makeCurrent();
    42. }
    43.  
    44. void GLWidget::initializeGL()
    45. {
    46. loadGLTextures();
    47.  
    48. glEnable(GL_TEXTURE_2D);
    49. glShadeModel(GL_SMOOTH);
    50.  
    51. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    52. glClearDepth(1.0f);
    53.  
    54. glEnable(GL_DEPTH_TEST);
    55. glDepthFunc(GL_LEQUAL);
    56.  
    57. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    58.  
    59. glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
    60. glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
    61. glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
    62. glEnable(GL_LIGHT1);
    63.  
    64. }
    65.  
    66. void GLWidget::resizeGL( int width, int height )
    67. {
    68. height = height?height:1;
    69.  
    70. glViewport( 0, 0, (GLint)width, (GLint)height );
    71.  
    72. glMatrixMode(GL_PROJECTION);
    73. glLoadIdentity();
    74. gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    75.  
    76. glMatrixMode(GL_MODELVIEW);
    77. glLoadIdentity();
    78. }
    79.  
    80. void GLWidget::paintGL()
    81. {
    82. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    83. glLoadIdentity();
    84.  
    85. makeObject(); //create our object
    86. }
    87.  
    88. void GLWidget::makeObject()
    89. {
    90. glTranslatef(0.0f,0.0f,z);
    91.  
    92. glRotatef(rotx,1.0f,0.0f,0.0f);
    93. glRotatef(roty,0.0f,1.0f,0.0f);
    94.  
    95. glBindTexture(GL_TEXTURE_2D, texture[filter]);
    96.  
    97. glBegin(GL_QUADS);
    98. // Front Face
    99. glNormal3f( 0.0f, 0.0f, 1.0f);
    100. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    101. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    102. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    103. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    104. // Back Face
    105. glNormal3f( 0.0f, 0.0f,-1.0f);
    106. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    107. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    108. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    109. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    110. // Top Face
    111. glNormal3f( 0.0f, 1.0f, 0.0f);
    112. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    113. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    114. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    115. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    116. // Bottom Face
    117. glNormal3f( 0.0f,-1.0f, 0.0f);
    118. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    119. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    120. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    121. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    122. // Right face
    123. glNormal3f( 1.0f, 0.0f, 0.0f);
    124. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    125. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    126. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    127. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    128. // Left Face
    129. glNormal3f(-1.0f, 0.0f, 0.0f);
    130. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    131. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    132. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    133. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    134. glEnd();
    135. }
    136.  
    137. void GLWidget::timeOutSlot()
    138. {
    139. timeOut();
    140. }
    141.  
    142. void GLWidget::timeOut()
    143. {
    144. rotx+=drotx;
    145. roty+=droty;
    146.  
    147. updateGL();
    148. }
    149.  
    150. void GLWidget::loadGLTextures()
    151. {
    152. QImage t;
    153. QImage b;
    154.  
    155. if ( !b.load( "../images/crate.bmp" ) )
    156. {
    157. b = QImage( 16, 16, QImage::Format_ARGB32 );
    158. b.fill( Qt::green);
    159. }
    160.  
    161. t = QGLWidget::convertToGLFormat( b );
    162. glGenTextures( 3, &texture[0] );
    163.  
    164. glBindTexture( GL_TEXTURE_2D, texture[0] );
    165. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    166. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    167. glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() );
    168.  
    169. glBindTexture(GL_TEXTURE_2D, texture[1]);
    170. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    171. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    172. glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() );
    173.  
    174. glBindTexture(GL_TEXTURE_2D, texture[2]);
    175. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    176. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    177. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, t.width(), t.height(), GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
    178. }
    179.  
    180. void GLWidget::keyPressEvent( QKeyEvent *e )
    181. {
    182. switch( e->key() )
    183. {
    184. case Qt::Key_L:
    185. light = !light;
    186.  
    187. if( light )
    188. glEnable( GL_LIGHTING );
    189. else
    190. glDisable( GL_LIGHTING );
    191.  
    192. break;
    193.  
    194. case Qt::Key_F:
    195. filter++;
    196. if( filter > 2 )
    197. filter = 0;
    198.  
    199. break;
    200.  
    201. case Qt::Key_Left:
    202. droty -= 0.01f;
    203.  
    204. break;
    205.  
    206. case Qt::Key_Right:
    207. droty += 0.01f;
    208.  
    209. break;
    210.  
    211. case Qt::Key_Up:
    212. drotx -= 0.01f;
    213.  
    214. break;
    215.  
    216. case Qt::Key_Down:
    217. drotx += 0.01f;
    218.  
    219. break;
    220.  
    221. case Qt::Key_PageDown:
    222. z -= 0.02f;
    223.  
    224. break;
    225.  
    226. case Qt::Key_PageUp:
    227. z += 0.02f;
    228.  
    229. case Qt::Key_Escape:
    230. close();
    231. }
    232. }
    To copy to clipboard, switch view to plain text mode 

    Last our main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. Window window;
    10. window.resize( 400, 350 );
    11. window.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    If i instead use this and dont use at all the mainwindow.h and mainwindow.cpp the QKey Evnts work

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QGLWidget>
    4. #include "glwidget.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9. QGLWidget *glWidget = new GLWidget;
    10. glWidget->show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    So my question is why they dont work when i use QMainWindow
    Last edited by xerionn; 21st November 2008 at 07:03.

  2. #2
    Join Date
    Nov 2008
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow, QGLWidget - problems with QKeyEvents

    Duh Silly me, i had the setFocus to a wrong position !

Similar Threads

  1. QGLWidget Problems
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2006, 22:06

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.