Results 1 to 13 of 13

Thread: Serious problem with QGLWidget

  1. #1
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Serious problem with QGLWidget

    Hello

    I'm looking to write an opengl based editor using QT but I'm experiencing a problem with QGLWidget. I have implemented a camera class and it moves smoothly when I move it 'programmatically' and from input using the mouseMoveEvent, but when I update the camera using input from keyPressEvent it is erratic and shows what looks like a double image of the previous frame. I've subclassed the QGLWidget and placed it on my main window and tried adding the keyPressEvent function to the opengl widget class and as a function of main window but the same problem remains.

    Has anybody else experienced this problem, which I belive is caused by the high number of keyboard events being sent, and know of a way to work around it for smooth keyboard input?

    I am currently using version 4.3.0beta/Mingw with QDevelop and have an example program that allows you to switch from one input method to another as a test. It contains all the source code and project file too. The download is available here at http://www.alsprogrammingresource.com/Test.zip

    Regards
    Alan
    Last edited by al101; 26th April 2007 at 21:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serious problem with QGLWidget

    I suggest you start by adding a timer to check for key presses instead of handling them directly in keyPressEvent to get a steady movement. In keyPress/keyRelease events only flag that a key has been pressed or released and then handle the actual change in the timer timeout. Maybe this will be enough to make it work correctly.

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

    al101 (27th April 2007)

  4. #3
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serious problem with QGLWidget

    Thanks wysota

    I'll certainly give that a try. Polling the key press events periodically may be the only work around but I was wondering if it was a common problem with a standard solution. I'll see what happens and post the results if successful.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serious problem with QGLWidget

    "Standard solution" is to use a timer. You're currently using a non-standard one

  6. #5
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serious problem with QGLWidget

    ok, I've added an array of flags to record which keys were pressed:

    bool keypress[256];

    and changed the keyPressEvent and keyReleaseEvent to:

    Qt Code:
    1. void MainWindowImpl::keyReleaseEvent( QKeyEvent *e )
    2.  
    3. {
    4.  
    5. switch( e->key() )
    6.  
    7. {
    8.  
    9. if (keyboardControl)
    10.  
    11. {
    12.  
    13. case Qt::Key_W:
    14.  
    15. case Qt::Key_S:
    16.  
    17. case Qt::Key_A:
    18.  
    19. case Qt::Key_D:
    20.  
    21. case Qt::Key_Q:
    22.  
    23. case Qt::Key_E:
    24.  
    25. keypress[e->key()] = false;
    26.  
    27. break;
    28.  
    29. }
    30.  
    31. }
    32.  
    33. }
    34.  
    35.  
    36.  
    37. void MainWindowImpl::keyPressEvent( QKeyEvent *e )
    38.  
    39. {
    40.  
    41. switch( e->key() )
    42.  
    43. {
    44.  
    45. if (keyboardControl)
    46.  
    47. {
    48.  
    49. case Qt::Key_W:
    50.  
    51. case Qt::Key_S:
    52.  
    53. case Qt::Key_A:
    54.  
    55. case Qt::Key_D:
    56.  
    57. case Qt::Key_Q:
    58.  
    59. case Qt::Key_E:
    60.  
    61. keypress[e->key()] = true;
    62.  
    63. break;
    64.  
    65. }
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 
    and changed the time out routine to:

    Qt Code:
    1. void OGLWidget::timeOutSlot()
    2.  
    3. {
    4.  
    5. if (keypress[Qt::Key_W])
    6.  
    7. camera[currentCamera].Movement_z -= 0.01;
    8.  
    9.  
    10.  
    11. if (keypress[Qt::Key_S])
    12.  
    13. camera[currentCamera].Movement_z += 0.01;
    14.  
    15.  
    16.  
    17. if (keypress[Qt::Key_A])
    18.  
    19. camera[currentCamera].Movement_x -= 0.01;
    20.  
    21.  
    22.  
    23. if (keypress[Qt::Key_D])
    24.  
    25. camera[currentCamera].Movement_x += 0.01;
    26.  
    27.  
    28.  
    29. if (keypress[Qt::Key_Q])
    30.  
    31. camera[currentCamera].Movement_y -= 0.01;
    32.  
    33.  
    34.  
    35. if (keypress[Qt::Key_E])
    36.  
    37. camera[currentCamera].Movement_y += 0.01;
    38.  
    39.  
    40.  
    41. updateGL();
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 

    but the results are still erratic, how would I go about polling key press events using a timer or can you suggest an example or tutorial that I could refer to?
    Last edited by al101; 26th April 2007 at 21:34. Reason: reformatted to look better

  7. #6
    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: Serious problem with QGLWidget

    Quote Originally Posted by al101 View Post
    Qt Code:
    1. switch( e->key() )
    2. {
    3. if (keyboardControl)
    4. {
    5. case Qt::Key_W:
    6. ...
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    In this case "if(keyboardControl)" has no effect, as switch simply jumps to appropriate case clause.

  8. #7
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serious problem with QGLWidget

    thanks, I noticed this after I had released the test program. It didn't play a part in the problem so I just left it.
    Last edited by al101; 26th April 2007 at 22:13.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serious problem with QGLWidget

    Instead of the array you may want to try QSet<Qt::Key> and possibly also QSet<Qt::KeyboardModifier>. It should give you more flexibility. If not, then I'd like to notice that you're wasting 7 out of 8 bits of each cell in the array which gives a total waste of 224 bytes

  10. #9
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serious problem with QGLWidget

    ic, good to know

    On the keyboard input subject I might add that I have been downloading every QT program that displays an QGLWidget that I could find. A prime example of the problem can be seen clearly when viewing the examples of libQGL Viewer available here http://artis.imag.fr/~Gilles.Debunne/QGLViewer/

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serious problem with QGLWidget

    Could you be more specific?

  12. #11
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serious problem with QGLWidget

    Sure, if you download any of the examples on that site and use the arrow keys to move the camera left and right you will see that the problem is very obvious. I simply can't find any application with a QGLWidget with smooth scrolling based on keyboard input.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Serious problem with QGLWidget

    Is this smooth enough for you (it rotates instead of scrolling, but the idea remains...)?
    Attached Files Attached Files

  14. #13
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serious problem with QGLWidget

    Back from sleep, work...

    Thanks for the example wysota. I just found that the changes in my third post worked to solve the problem, but I also had to remove the timing routine that I introduced as a fix.

    So again, thanks for working with me on this. I'll post the link of a working example or tutorial soon.
    Last edited by al101; 27th April 2007 at 14:14.

Similar Threads

  1. why linking problem with QGLWidget???
    By Shuchi Agrawal in forum Newbie
    Replies: 17
    Last Post: 16th March 2007, 10:45
  2. Problem combining QWorkspace & QGLWidget
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2006, 21:45
  3. QGLWidget renderText problem
    By mbjerkne in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2006, 21:35
  4. Replies: 16
    Last Post: 7th March 2006, 15:57

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.