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
Code:
#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}
FenPrincipale.f
Code:
#ifndef HEADER_FENPRINCIPALE
#define HEADER_FENPRINCIPALE
#include <QtGui>
{
public:
FenPrincipale();
~FenPrincipale();
private:
};
#endif
FenPrincipale.CPP
Code:
#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
Code:
#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}
myGLWidget.H
Code:
#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();
public slots:
private:
float rotateX;
float rotateY;
float rotateZ;
};
#endif // MYGLWIDGET_H
myGLWidget.CPP
Code:
#include <iostream>
#include "myGLWidget.h"
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;
{
}
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?
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 :
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.
Quote:
but openGL nevertheless draw when i compile my project, why?
Because paintGL is called automatically when gl widget receives paintEvent.
Re: keyboard and openGL widget
This is your code.
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
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();
}
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:
Code:
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
Re: keyboard and openGL widget
Check out these functions from QWidget class.
Code:
Qt::FocusPolicy focusPolicy () const
void setFocusPolicy ( Qt::FocusPolicy policy )
Because by default a widget has Qt::NoFocus flag set.
Re: keyboard and openGL widget
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 ?
Re: keyboard and openGL widget
just try
Code:
this->setFocusPolicy(Qt::StrongFocus)
Call this in the constructor of the item.
Code:
{
this->setFocusPolicy(Qt::StrongFocus)
}
like this
Re: keyboard and openGL widget
what happens if in your main window instead of
Code:
FenPrincipale fenetre;
fenetre.showMaximized();
you use
Code:
myGLWidget fenetre;
fenetre.showMaximized();
?
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);
}
Re: keyboard and openGL widget
Quote:
Originally Posted by
meazza
just try
Code:
this->setFocusPolicy(Qt::StrongFocus)
Call this in the constructor of the item.
Code:
{
this->setFocusPolicy(Qt::StrongFocus)
}
like this
yes it works !!! ;)
==> but i don't know why...
Re: keyboard and openGL widget
Quote:
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
Re: keyboard and openGL widget
Quote:
Originally Posted by
peace_comp
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 ? :confused:
Quote:
Originally Posted by
meazza
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 ;)