[Kde-games-devel] simple QGLWidget program

Jan Bernatik bernatik at kn.vutbr.cz
Mon Jan 26 19:25:38 CET 2004


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



More information about the kde-games-devel mailing list