Results 1 to 10 of 10

Thread: Updating Window QTOpenGL

  1. #1
    Join Date
    Feb 2012
    Posts
    13
    Qt products
    Qt4

    Default Updating Window QTOpenGL

    Can someone help me, I need to update window in QTOpenGL ( if I press F10 I want to zoom, or change X or Y, but it doesnt update )

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QtOpenGL/QGLWidget>
    4. #include <QtGui/QMouseEvent>
    5. #include <GL/glu.h>
    6.  
    7. int cameraX, cameraY, cameraZ;
    8.  
    9. class GLWidget : public QGLWidget{
    10. public:
    11. void initializeGL();
    12. void resizeGL(int w, int h);
    13. void paintGL();
    14. void mousePressEvent(QMouseEvent *event);
    15. void mouseMoveEvent(QMouseEvent *event);
    16. void keyPressEvent(QKeyEvent *event);
    17. };
    18.  
    19. void GLWidget::initializeGL() {
    20. glDisable(GL_TEXTURE_2D);
    21. glDisable(GL_DEPTH_TEST);
    22. glDisable(GL_COLOR_MATERIAL);
    23. glEnable(GL_BLEND);
    24. glEnable(GL_POLYGON_SMOOTH);
    25. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    26. glClearColor(3, 3, 3, 0);
    27. cameraX = 0;
    28. cameraY = 0;
    29. cameraZ = 0;
    30. }
    31.  
    32. void GLWidget::resizeGL(int w, int h) {
    33. glViewport(0, 0, w, h);
    34. glMatrixMode(GL_PROJECTION);
    35. glLoadIdentity();
    36. gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
    37. glMatrixMode(GL_MODELVIEW);
    38. glLoadIdentity();
    39. }
    40.  
    41. void GLWidget::paintGL() {
    42. glClear(GL_COLOR_BUFFER_BIT);
    43. glColor3f(5,0,0);
    44. glTranslatef(cameraX, cameraY, cameraZ);
    45. glBegin(GL_POLYGON);
    46. glVertex2f(0,0);
    47. glVertex2f(0,500);
    48. glVertex2f(500,0);
    49. glEnd();
    50. }
    51. void GLWidget::mousePressEvent(QMouseEvent *event) {
    52.  
    53. }
    54. void GLWidget::mouseMoveEvent(QMouseEvent *event) {
    55. printf("%d, %d\n", event->x(), event->y());
    56. }
    57.  
    58. void GLWidget::keyPressEvent(QKeyEvent* event) {
    59. switch(event->key()) {
    60. case Qt::Key_Escape:
    61. close();
    62. break;
    63. case Qt::Key_F10:
    64. cameraX += 0.5f;
    65. break;
    66. default:
    67. event->ignore();
    68. break;
    69. }
    70. }
    71. int main(int argc, char *argv[])
    72. {
    73. QApplication a(argc, argv);
    74. GLWidget window;
    75. window.resizeGL(800, 600);
    76. window.show();
    77. a.exec();
    78. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating Window QTOpenGL

    Seems like you have to make the widget update ( re-draw ) itself. Try to call void QGLWidget::updateGL() after "cameraX += 0.5f;"

  3. #3
    Join Date
    Feb 2012
    Posts
    13
    Qt products
    Qt4

    Default Re: Updating Window QTOpenGL

    Qt Code:
    1. cameraX += 0.5f;
    2. QGLWidget::updateGL();
    To copy to clipboard, switch view to plain text mode 

    Doesn't work

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Updating Window QTOpenGL

    Because

    int cameraX, cameraY, cameraZ;
    is not a camera. You simply update some int value and refresh view.

    You need either transform projection matrix according to camerax or use gluLookAt().

    Link to RTM

    Basically this has nothing to do with Qt.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  5. #5
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating Window QTOpenGL

    Ok , just curious : is GLWidget is the only widget in your application's GUI hierachy ? If so...well i'm more then sure that it's not , i'd suggest that you use...Grabber example^^. The thing is that if GLWidget has a parent , then keyPressEvent will be handled by it's parent , so in order to make your (child) widget to handle it you can either use event filters or...just to see if it works , use QCoreApplication::sendEvent method ( in your parent's keyPressEvent implementation ) , look at the docs or http://doc.qt.nokia.com/qq/qq11-events.html for more details on Qt's event system...

    Eventually ,*not the best* solution may be :

    Qt Code:
    1. void <GLWidget'sParentClass>::keyPressEvent(QKeyEvent *event)
    2. {
    3.  
    4. QCoreApplication::sendEvent( glWidget , event );
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    Your GLWidget::keyPressEvent(QKeyEvent* event) may be the same , but still you DO need to update it manually , otherwise it will only updated explicitly : when window will be resized etc.

  6. #6
    Join Date
    Feb 2012
    Posts
    13
    Qt products
    Qt4

    Default Re: Updating Window QTOpenGL

    I got some error while declaring <void MainWindow>::keyPressEvent(QKeyEvent * event), I tried it in both main.cpp and mainwindow.cpp.

    error:
    Qt Code:
    1. /home/marcus/2-build-desktop/../2/mainwindow.cpp:16: error: ‘void’ is not a template
    2. /home/marcus/2-build-desktop/../2/mainwindow.cpp:16: error: explicit qualification in declaration of ‘void keyPressEvent(QKeyEvent*)’
    3. /home/marcus/2-build-desktop/../2/mainwindow.cpp:-1: In function ‘void keyPressEvent(QKeyEvent*)’:
    4. /home/marcus/2-build-desktop/../2/mainwindow.cpp:19: error: ‘glWidget’ was not declared in this scope
    To copy to clipboard, switch view to plain text mode 

    Here's main window if it's needed.
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by xleniz; 23rd February 2012 at 15:20.

  7. #7
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating Window QTOpenGL

    Did you just...copy-paste it ? ^^ Hm, i think you should step away from Qt for a while and learn native OOP / C++ programming , because you seem to be missing core moments:

    void keyPressEvent(QKeyEvent *event) is a protected method : it's supposed to be reimplemented by the child classes . Aside from reimplementation , you've declared new public method with the same signature...So that was your initial problem (according to the code you've shared in the first post) . Now , you show another piece of code (seems like copy-pasted again^^) that doesn't seem to have anything related to your first program.

  8. #8
    Join Date
    Feb 2012
    Posts
    13
    Qt products
    Qt4

    Default Re: Updating Window QTOpenGL

    Quote Originally Posted by AlexSudnik View Post
    Did you just...copy-paste it ? ^^ Hm, i think you should step away from Qt for a while and learn native OOP / C++ programming , because you seem to be missing core moments:

    void keyPressEvent(QKeyEvent *event) is a protected method : it's supposed to be reimplemented by the child classes . Aside from reimplementation , you've declared new public method with the same signature...So that was your initial problem (according to the code you've shared in the first post) . Now , you show another piece of code (seems like copy-pasted again^^) that doesn't seem to have anything related to your first program.
    Heheh, yes, I had a hard time googling a QT OpenGL code, theres so few examples that I understand how to add.
    You got a site ?? (maybe with .pro files)

  9. #9
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating Window QTOpenGL

    What do you mean "googling a QT OpenGL code" ? There is a folder called "examples" in the Qt home dir.Navigate to "examples/opengl" folder...everything you need ( headers , sources and project files ) is there.

  10. #10
    Join Date
    Feb 2012
    Posts
    13
    Qt products
    Qt4

    Default Re: Updating Window QTOpenGL

    Ok thanks, I downloaded SDK now, though I think I need a more simple openGL example.

    Btw whats difference between Native and OpenGL? Native looks better on my computer.
    Last edited by xleniz; 23rd February 2012 at 18:06.

Similar Threads

  1. QtOpenGL/qgl.h change between 4.7.3 and 4.8
    By hml in forum Qt Programming
    Replies: 6
    Last Post: 28th January 2012, 18:38
  2. Replies: 2
    Last Post: 7th January 2012, 15:42
  3. QtOpenGL example problem on wince6
    By lamosca in forum Installation and Deployment
    Replies: 1
    Last Post: 21st April 2010, 16:56
  4. Replies: 0
    Last Post: 2nd January 2010, 08:58
  5. QtOpenGL how to draw text?
    By AndreaCe in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2009, 00:50

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.