[Kde-games-devel] simple QGLWidget program
Albert Astals Cid
tsdgeos at terra.es
Mon Jan 26 20:28:27 CET 2004
Have a closer look at the qt qglwidget documentation as you seen to not have
fully undestood it.
In short, you need to reimplement initializeGL() (maybe not completely
necessary), resizeGL( int w, int h ) (you already do that but in a incorerct
way as resizeGL is protected) AND the most important paintGL() that is the
routine that gets called every time the widgets wants to redisplay itself. So
basically you'll have to change your code to something like
-------------------------- main.cpp -----------------------------
#include <qapplication.h>
#include "../headers/window.h"
int main( int argc, char **argv )
{
QApplication::setColorSpec( QApplication::CustomColor );
QApplication app( argc, argv );
if ( !QGLFormat::hasOpenGL() )
qFatal( "This system has no OpenGL support" );
base_window w;
app.setMainWidget( &w );
w.show();
return app.exec();
}
-------------------------- window.h -----------------------------
#include <qgl.h>
class base_window : public QGLWidget
{
Q_OBJECT
public:
base_window( QWidget *parent = 0, const char *name = "main_GL_widget" );
~base_window();
protected: // THAT IS IMPORTANT
void initializeGL();
void resizeGL(int w, int h) { glViewport( 0, 0, (GLint)w, (GLint)h ); };
void paintGL();
};
-------------------------- window.cpp -----------------------------
#include "../headers/window.h"
base_window :: base_window( QWidget *parent, const char *name)
: QGLWidget( parent, name )
{
}
base_window :: ~base_window()
{
}
base_window ::initializeGL()
{
glClearColor(0.0, 1.0, 0.0, 0.0);
}
void base_window :: paintGL()
{
cout<<"draw"<<endl;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2i(200, 50);
glVertex2i( 50, 250);
glVertex2i(350, 250);
glEnd();
glFlush();
}
I haev not checked that works or evern works, but it is similar has how it
should be. If you still have problems tell me and i'll send you a example
code i have for QT and OpenGL.
Albert.
A Dilluns 26 Gener 2004 19:25, Jan Bernatik va escriure:
> Hi all
>
> I'm trying to learn QT. I started by reading Tutorials and examples from
> trolltech assistant which came with QT. I'm not using any KDE libs. at
> the moment, QT is just enough at the time :)
>
> I tried to do a simple think, paint triangle with QGLWidget. After like
> 3 days I'm not succesfull.
>
> -------------------------- main.cpp -----------------------------
>
> #include <qapplication.h>
> #include "../headers/window.h"
>
> int main( int argc, char **argv )
> {
>
>
> QApplication::setColorSpec( QApplication::CustomColor );
> QApplication app( argc, argv );
> if ( !QGLFormat::hasOpenGL() )
> qFatal( "This system has no OpenGL support" );
>
> base_window w;
> app.setMainWidget( &w );
>
> w.show();
> w.Draw();
>
> return app.exec();
> }
>
>
> -------------------------- window.h -----------------------------
>
> #include <qgl.h>
>
> class base_window : public QGLWidget
> {
> Q_OBJECT
> public:
> base_window( QWidget *parent = 0, const char *name = "main_GL_widget" );
> ~base_window();
>
> void resizeGL(int w, int h) { glViewport( 0, 0, (GLint)w, (GLint)h ); };
> void Draw();
> public slots:
> protected:
> private:
>
>
> };
>
> -------------------------- window.cpp -----------------------------
> #include "../headers/window.h"
>
> base_window :: base_window( QWidget *parent, const char *name)
>
> : QGLWidget( parent, name )
>
> {
> }
>
> base_window :: ~base_window()
> {
> }
>
> void base_window :: Draw() {
> cout<<"draw"<<endl;
> glClearColor(0.0, 1.0, 0.0, 0.0);
> glClear(GL_COLOR_BUFFER_BIT);
> glColor3f(0.0f, 1.0f, 0.0f);
> glBegin(GL_TRIANGLES);
> glVertex2i(200, 50);
> glVertex2i( 50, 250);
> glVertex2i(350, 250);
> glEnd();
> glFlush();
> }
>
>
> I know I'm not using any layout, frame or whatever, but I'm trying to
> display simple window with GLWidget and a triangle. Window opens,
> resizeGL works as supposed, but nothing is painted. I really don't know
> WHAT is wrong, if QGLWidget must be paced in some layout, cause If it's
> like this it should be the only => MAIN widget. If I understand.
> I did some programs using GLUT, and I want to switch to QT.
> (using glutswapbuffers() for drawing scene)
>
> Thanks for help.
>
> John
>
> _______________________________________________
> kde-games-devel mailing list
> kde-games-devel at kde.org
> https://mail.kde.org/mailman/listinfo/kde-games-devel
More information about the kde-games-devel
mailing list