PDA

View Full Version : Updating Window QTOpenGL



xleniz
22nd February 2012, 20:09
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 )



#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtOpenGL/QGLWidget>
#include <QtGui/QMouseEvent>
#include <GL/glu.h>

int cameraX, cameraY, cameraZ;

class GLWidget : public QGLWidget{
public:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
};

void GLWidget::initializeGL() {
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(3, 3, 3, 0);
cameraX = 0;
cameraY = 0;
cameraZ = 0;
}

void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(5,0,0);
glTranslatef(cameraX, cameraY, cameraZ);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(0,500);
glVertex2f(500,0);
glEnd();
}
void GLWidget::mousePressEvent(QMouseEvent *event) {

}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
printf("%d, %d\n", event->x(), event->y());
}

void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
close();
break;
case Qt::Key_F10:
cameraX += 0.5f;
break;
default:
event->ignore();
break;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GLWidget window;
window.resizeGL(800, 600);
window.show();
a.exec();
}


Thanks!

AlexSudnik
23rd February 2012, 08:03
Seems like you have to make the widget update ( re-draw ) itself. Try to call void QGLWidget::updateGL() after "cameraX += 0.5f;"

xleniz
23rd February 2012, 13:36
cameraX += 0.5f;
QGLWidget::updateGL();


Doesn't work

Talei
23rd February 2012, 14:15
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 (http://www.opengl.org/resources/faq/technical/viewing.htm)

Basically this has nothing to do with Qt.

AlexSudnik
23rd February 2012, 14:29
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 :




void <GLWidget'sParentClass>::keyPressEvent(QKeyEvent *event)
{

QCoreApplication::sendEvent( glWidget , event );

}



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.

xleniz
23rd February 2012, 14:57
I got some error while declaring <void MainWindow>::keyPressEvent(QKeyEvent * event), I tried it in both main.cpp and mainwindow.cpp.

error:


/home/marcus/2-build-desktop/../2/mainwindow.cpp:16: error: ‘void’ is not a template
/home/marcus/2-build-desktop/../2/mainwindow.cpp:16: error: explicit qualification in declaration of ‘void keyPressEvent(QKeyEvent*)’
/home/marcus/2-build-desktop/../2/mainwindow.cpp:-1: In function ‘void keyPressEvent(QKeyEvent*)’:
/home/marcus/2-build-desktop/../2/mainwindow.cpp:19: error: ‘glWidget’ was not declared in this scope


Here's main window if it's needed.


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

AlexSudnik
23rd February 2012, 15:26
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.

xleniz
23rd February 2012, 15:29
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)

AlexSudnik
23rd February 2012, 15:35
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.

xleniz
23rd February 2012, 18:01
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.