PDA

View Full Version : keyboard and openGL widget



21did21
5th July 2011, 11:54
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


#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"

using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}

FenPrincipale.f


#ifndef HEADER_FENPRINCIPALE
#define HEADER_FENPRINCIPALE
#include <QtGui>
class FenPrincipale : public QMainWindow
{
public:
FenPrincipale();
~FenPrincipale();
private:
};
#endif

FenPrincipale.CPP


#include "FenPrincipale.h"
#include "myGLWidget.h"
FenPrincipale::FenPrincipale()
{
QMdiArea *zoneCentrale = new QMdiArea;
myGLWidget *plotFenetre = new myGLWidget;
QMdiSubWindow *sousFenetre1 = zoneCentrale->addSubWindow(plotFenetre);
setCentralWidget(zoneCentrale);
}
FenPrincipale::~FenPrincipale()
{
}

MAIN.CPP


#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"

using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}

myGLWidget.H


#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H
#include <QtOpenGL>
#include <QGLWidget>
#include <QKeyEvent>
class myGLWidget : public QGLWidget //this class herite from QGLWidget
{
Q_OBJECT
public:
myGLWidget();
~myGLWidget();
void initializeGL();
void resizeGL(int,int);
void paintGL();
void keyPressEvent( QKeyEvent *keyEvent );

public slots:

private:
float rotateX;
float rotateY;
float rotateZ;
};
#endif // MYGLWIDGET_H

myGLWidget.CPP


#include <iostream>
#include "myGLWidget.h"
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;
myGLWidget::myGLWidget() : QGLWidget()
{

}
myGLWidget::~myGLWidget()
{

}
void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
cout << "clavier" << endl;
if (keyEvent->key())
{
if (Qt::Key_Up)
{
rotateX=rotateX+2;
repaint();
updateGL();
cout << "UP" << endl;
}
}
else
{
}
}
void myGLWidget::initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void myGLWidget::resizeGL(int width, int height)
{
if(height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( 1.2, 1.7, 1.0, 0, 0, 0, 0, 0, 1);
glPushMatrix( );
glRotatef(rotateX,1.0f,0.0f,0.0f);
glRotatef(rotateY,0.0f,1.0f,0.0f);
glRotatef(rotateZ,0.0f,0.0f,1.0f);
glBegin(GL_LINES);
//i draw
glEnd();
}


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?

stampede
5th July 2011, 12:02
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 :

if (keyEvent->key())
{
if (Qt::Key_Up)
{
rotateX=rotateX+2;
repaint();
updateGL();
cout << "UP" << endl;
}
}
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.

mcosta
5th July 2011, 12:02
This is your code.



void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
cout << "clavier" << endl;
if (keyEvent->key())
{
if (Qt::Key_Up)
{
rotateX=rotateX+2;
repaint();
updateGL();
cout << "UP" << endl;
}
}
else
{
}
}


Try this code



void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
cout << "clavier" << endl;
if (keyEvent->key() == Qt::Key_Up) {
rotateX=rotateX+2;
updateGL();
cout << "UP" << endl;
keyEvent->accept();
}
else
keyEvent->ignore();
}

21did21
5th July 2011, 13:27
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:

void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
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

meazza
5th July 2011, 14:01
Check out these functions from QWidget class.


Qt::FocusPolicy focusPolicy () const
void setFocusPolicy ( Qt::FocusPolicy policy )

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

21did21
5th July 2011, 14:06
thank for your answer but i haven't understand because my english is not good :o

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

meazza
5th July 2011, 14:12
just try


this->setFocusPolicy(Qt::StrongFocus)

Call this in the constructor of the item.


myGLWidget::myGLWidget() : QGLWidget()
{
this->setFocusPolicy(Qt::StrongFocus)
}

like this

john_god
5th July 2011, 14:14
what happens if in your main window instead of


FenPrincipale fenetre;
fenetre.showMaximized();
you use


myGLWidget fenetre;
fenetre.showMaximized();
?

peace_comp
5th July 2011, 14:20
Hi,
The Qt::FocusPolicy property holds the way the widget accepts keyboard focus (http://doc.qt.nokia.com/latest/qwidget.html#focusPolicy-prop)
to enable keyboard focus, add a call to setFocusPolicy(Qt::StrongFocus) in the widget constructor :

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

21did21
5th July 2011, 14:23
just try

this->setFocusPolicy(Qt::StrongFocus)
Call this in the constructor of the item.

myGLWidget::myGLWidget() : QGLWidget()
{
this->setFocusPolicy(Qt::StrongFocus)
}
like this

yes it works !!! ;)

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

meazza
5th July 2011, 14:26
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

21did21
5th July 2011, 14:27
Hi,
The Qt::FocusPolicy property holds the way the widget accepts keyboard focus (http://doc.qt.nokia.com/latest/qwidget.html#focusPolicy-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 ? :confused:


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 ;)