I am new to Qt programming. I would like to create a simple application that can show the OpenGL model and provide some simple user interface elements for managing it.
So far I have created my own GlWidget, which subclasses QGLWidget and uses Qt Designer to create a simple form. In my main form, I added a few user interface elements and an empty QWidget, which defines the area in which I want my GlWidget to fill (I come from the Java background, and so everything is done in Java). I also created an instance of my GlWidged and added it to this empty widget. GlWidget even does it right! The only problem is that this is the size of the postage stamp, and I cannot figure out how to make it fill the entire parent widget.
How to fix it?
MainForm.h
#ifndef _MAINFORM_H #define _MAINFORM_H #include "ui_MainForm.h" #include "GlWidget.h" class MainForm : public QMainWindow { Q_OBJECT public: MainForm(); virtual ~MainForm(); private: Ui::MainForm widget; GlWidget* glWidget; }; #endif
MainForm.cpp
#include "MainForm.h" MainForm::MainForm() { widget.setupUi(this); glWidget = new GlWidget(widget.widget_gl); } MainForm::~MainForm() { }
glwidget.h
#ifndef GLWIDGET_H #define GLWIDGET_H #include <qtimer.h> #include <Qt/qgl.h> class GlWidget : QGLWidget { Q_OBJECT public: GlWidget(QWidget* parent = 0); protected: virtual void initializeGL(); virtual void resizeGL(int width, int height); virtual void paintGL(); virtual void keyPressEvent(QKeyEvent *e); virtual void timeOut(); protected slots: virtual void timeOutSlot(); private: QTimer *m_timer; }; #endif /* GLWIDGET_H */
glwidget.cpp
#include "GlWidget.h" #include <qapplication.h> #include <qtimer.h> #include <qevent.h> GlWidget::GlWidget(QWidget* parent) : QGLWidget(parent) { //setLayout(); //showMaximized(); int timerInterval = 1; if (timerInterval == 0) { m_timer = 0; } else { m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(timeOutSlot())); m_timer->start(timerInterval); } } void GlWidget::keyPressEvent(QKeyEvent *e) { switch (e->key()) { case Qt::Key_Escape: close(); } } void GlWidget::timeOut() { } void GlWidget::timeOutSlot() { timeOut(); } void GlWidget::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 GlWidget::resizeGL(int width, int height) { height = height ? height : 1; glViewport(0, 0, (GLint) width, (GLint) height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void GlWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5f, 0.0f, -6.0f); glBegin(GL_TRIANGLES); glVertex3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glEnd(); glTranslatef(3.0f, 0.0f, 0.0f); glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glEnd(); }