Results 1 to 12 of 12

Thread: keyboard and openGL widget

  1. #1
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default keyboard and openGL widget

    Hello world,

    i use openGL widget and i want to use keybord to rotate my object but it don't works...

    i don't know why, because i think that my programm is good...

    can you see this please? :


    MAIN.CPP
    Qt Code:
    1. #include <iostream>
    2. #include <QApplication>
    3. #include "myGLWidget.h"
    4. #include <QGridLayout>
    5. #include "FenPrincipale.h"
    6.  
    7. using namespace std;
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11. FenPrincipale fenetre;
    12. fenetre.showMaximized();
    13. fenetre.setWindowTitle("blabla");
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    FenPrincipale.f
    Qt Code:
    1. #ifndef HEADER_FENPRINCIPALE
    2. #define HEADER_FENPRINCIPALE
    3. #include <QtGui>
    4. class FenPrincipale : public QMainWindow
    5. {
    6. public:
    7. FenPrincipale();
    8. ~FenPrincipale();
    9. private:
    10. };
    11. #endif
    To copy to clipboard, switch view to plain text mode 
    FenPrincipale.CPP
    Qt Code:
    1. #include "FenPrincipale.h"
    2. #include "myGLWidget.h"
    3. FenPrincipale::FenPrincipale()
    4. {
    5. QMdiArea *zoneCentrale = new QMdiArea;
    6. myGLWidget *plotFenetre = new myGLWidget;
    7. QMdiSubWindow *sousFenetre1 = zoneCentrale->addSubWindow(plotFenetre);
    8. setCentralWidget(zoneCentrale);
    9. }
    10. FenPrincipale::~FenPrincipale()
    11. {
    12. }
    To copy to clipboard, switch view to plain text mode 
    MAIN.CPP
    Qt Code:
    1. #include <iostream>
    2. #include <QApplication>
    3. #include "myGLWidget.h"
    4. #include <QGridLayout>
    5. #include "FenPrincipale.h"
    6.  
    7. using namespace std;
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11. FenPrincipale fenetre;
    12. fenetre.showMaximized();
    13. fenetre.setWindowTitle("blabla");
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    myGLWidget.H
    Qt Code:
    1. #ifndef MYGLWIDGET_H
    2. #define MYGLWIDGET_H
    3. #include <QtOpenGL>
    4. #include <QGLWidget>
    5. #include <QKeyEvent>
    6. class myGLWidget : public QGLWidget //this class herite from QGLWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. myGLWidget();
    11. ~myGLWidget();
    12. void initializeGL();
    13. void resizeGL(int,int);
    14. void paintGL();
    15. void keyPressEvent( QKeyEvent *keyEvent );
    16.  
    17. public slots:
    18.  
    19. private:
    20. float rotateX;
    21. float rotateY;
    22. float rotateZ;
    23. };
    24. #endif // MYGLWIDGET_H
    To copy to clipboard, switch view to plain text mode 
    myGLWidget.CPP
    Qt Code:
    1. #include <iostream>
    2. #include "myGLWidget.h"
    3. #include <GL/gl.h>
    4. #include <GL/glu.h>
    5. using namespace std;
    6. myGLWidget::myGLWidget() : QGLWidget()
    7. {
    8.  
    9. }
    10. myGLWidget::~myGLWidget()
    11. {
    12.  
    13. }
    14. void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
    15. {
    16. cout << "clavier" << endl;
    17. if (keyEvent->key())
    18. {
    19. if (Qt::Key_Up)
    20. {
    21. rotateX=rotateX+2;
    22. repaint();
    23. updateGL();
    24. cout << "UP" << endl;
    25. }
    26. }
    27. else
    28. {
    29. }
    30. }
    31. void myGLWidget::initializeGL()
    32. {
    33. glShadeModel(GL_SMOOTH);
    34. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    35. glClearDepth(1.0f);
    36. glEnable(GL_DEPTH_TEST);
    37. glDepthFunc(GL_LEQUAL);
    38. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    39. }
    40. void myGLWidget::resizeGL(int width, int height)
    41. {
    42. if(height == 0)
    43. height = 1;
    44. glViewport(0, 0, width, height);
    45. glMatrixMode(GL_PROJECTION);
    46. glLoadIdentity();
    47. gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
    48. glMatrixMode(GL_MODELVIEW);
    49. glLoadIdentity();
    50. }
    51. void myGLWidget::paintGL()
    52. {
    53. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    54. glClearColor(1.0f,1.0f,1.0f,1.0f);
    55. glMatrixMode( GL_MODELVIEW );
    56. glLoadIdentity( );
    57. gluLookAt( 1.2, 1.7, 1.0, 0, 0, 0, 0, 0, 1);
    58. glPushMatrix( );
    59. glRotatef(rotateX,1.0f,0.0f,0.0f);
    60. glRotatef(rotateY,0.0f,1.0f,0.0f);
    61. glRotatef(rotateZ,0.0f,0.0f,1.0f);
    62. glBegin(GL_LINES);
    63. //i draw
    64. glEnd();
    65. }
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    2°) i have a second question:


    you can see in this programm that i never tell the methode : "paintGL()"

    but openGL nevertheless draw when i compile my project, why?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: keyboard and openGL widget

    First of all, you should initialize member variables rotateX, rotateY and rotateZ in constructor (set them to 0), because without it they will contain random values. Print them to see it yourself.
    Second thing, this code :
    Qt Code:
    1. if (keyEvent->key())
    2. {
    3. if (Qt::Key_Up)
    4. {
    5. rotateX=rotateX+2;
    6. repaint();
    7. updateGL();
    8. cout << "UP" << endl;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    is wrong. You should check if keyEvent->key() == Qt::Key_Up, not the two things separately.
    but openGL nevertheless draw when i compile my project, why?
    Because paintGL is called automatically when gl widget receives paintEvent.

  3. The following user says thank you to stampede for this useful post:

    21did21 (5th July 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keyboard and openGL widget

    This is your code.

    Qt Code:
    1. void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
    2. {
    3. cout << "clavier" << endl;
    4. if (keyEvent->key())
    5. {
    6. if (Qt::Key_Up)
    7. {
    8. rotateX=rotateX+2;
    9. repaint();
    10. updateGL();
    11. cout << "UP" << endl;
    12. }
    13. }
    14. else
    15. {
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Try this code

    Qt Code:
    1. void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
    2. {
    3. cout << "clavier" << endl;
    4. if (keyEvent->key() == Qt::Key_Up) {
    5. rotateX=rotateX+2;
    6. updateGL();
    7. cout << "UP" << endl;
    8. keyEvent->accept();
    9. }
    10. else
    11. keyEvent->ignore();
    12. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  5. #4
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    thank for you help but it still don't works:

    => my problem it's not here, my problem is due to my programm never go in the function:
    Qt Code:
    1. void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
    To copy to clipboard, switch view to plain text mode 
    i see this because i have put a "cout" in this function and this cout never show the keyword: "clavier"

    => if think i have a mistake a the management of keybord but i don't find it...
    perhaps i have forgotten différents "#include " ???

    => can you try my code on your computer and say if it show the keyword "clavier" in your console?

    thanks a lot

  6. #5
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    Check out these functions from QWidget class.

    Qt Code:
    1. Qt::FocusPolicy focusPolicy () const
    2. void setFocusPolicy ( Qt::FocusPolicy policy )
    To copy to clipboard, switch view to plain text mode 

    Because by default a widget has Qt::NoFocus flag set.

  7. #6
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    thank for your answer but i haven't understand because my english is not good

    can you reformulate that i have to do this this function ?

  8. #7
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    just try

    Qt Code:
    1. this->setFocusPolicy(Qt::StrongFocus)
    To copy to clipboard, switch view to plain text mode 

    Call this in the constructor of the item.

    Qt Code:
    1. myGLWidget::myGLWidget() : QGLWidget()
    2. {
    3. this->setFocusPolicy(Qt::StrongFocus)
    4. }
    To copy to clipboard, switch view to plain text mode 

    like this

  9. The following user says thank you to meazza for this useful post:

    21did21 (5th July 2011)

  10. #8
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: keyboard and openGL widget

    what happens if in your main window instead of

    Qt Code:
    1. FenPrincipale fenetre;
    2. fenetre.showMaximized();
    To copy to clipboard, switch view to plain text mode 
    you use

    Qt Code:
    1. myGLWidget fenetre;
    2. fenetre.showMaximized();
    To copy to clipboard, switch view to plain text mode 
    ?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  11. #9
    Join Date
    Mar 2008
    Location
    Morocco
    Posts
    47
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: keyboard and openGL widget

    Hi,
    The Qt::FocusPolicy property holds the way the widget accepts keyboard focus (http://doc.qt.nokia.com/latest/qwidg...cusPolicy-prop)
    to enable keyboard focus, add a call to setFocusPolicy(Qt::StrongFocus) in the widget constructor :

    myGLWidget::myGLWidget() : QGLWidget()
    {
    setFocusPolicy(Qt::StrongFocus);
    }

  12. The following user says thank you to peace_comp for this useful post:

    21did21 (5th July 2011)

  13. #10
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    Quote Originally Posted by meazza View Post
    just try
    Qt Code:
    1. this->setFocusPolicy(Qt::StrongFocus)
    To copy to clipboard, switch view to plain text mode 
    Call this in the constructor of the item.
    Qt Code:
    1. myGLWidget::myGLWidget() : QGLWidget()
    2. {
    3. this->setFocusPolicy(Qt::StrongFocus)
    4. }
    To copy to clipboard, switch view to plain text mode 
    like this
    yes it works !!!

    ==> but i don't know why...

  14. #11
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    yes it works !!!

    ==> but i don't know why...
    Well for the widget to be able to handle key-press event the widget has to have focus. Otherwise it is the MainWindow class which has focus and when you press your keyboard the MainWindow class will handle them and not the widget. Glad it works for you and also read what peace_comp posted and that link for more info

  15. #12
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: keyboard and openGL widget

    Quote Originally Posted by peace_comp View Post
    Hi,
    The Qt::FocusPolicy property holds the way the widget accepts keyboard focus (http://doc.qt.nokia.com/latest/qwidg...cusPolicy-prop)
    to enable keyboard focus, add a call to setFocusPolicy(Qt::StrongFocus) in the widget constructor :

    myGLWidget::myGLWidget() : QGLWidget()
    {
    setFocusPolicy(Qt::StrongFocus);
    }
    if i understand this line is to active keyboard ?

    Quote Originally Posted by meazza View Post
    Well for the widget to be able to handle key-press event the widget has to have focus. Otherwise it is the MainWindow class which has focus and when you press your keyboard the MainWindow class will handle them and not the widget. Glad it works for you and also read what peace_comp posted and that link for more info
    ok thanks a lot

Similar Threads

  1. Widget Virtual Keyboard
    By embitel in forum Qt-based Software
    Replies: 14
    Last Post: 12th July 2012, 10:22
  2. opengl widget not able to embed in QGraphicsView
    By wagmare in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2010, 07:22
  3. Putting an opengl widget in to a mainwindow
    By bod in forum Qt Programming
    Replies: 3
    Last Post: 1st July 2008, 07:59
  4. Crash when minimizing OpenGL widget
    By MistaPain in forum Qt Programming
    Replies: 5
    Last Post: 7th October 2006, 16:58

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.