2009年4月1日 星期三

Qt Opengl QMainWindow

glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>

class GLWidget : public QGLWidget
{
public:
GLWidget(QWidget *parent);

~GLWidget();
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
};

#endif // GLWIDGET_H

glwidget.cpp
#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{}

GLWidget::~GLWidget()
{}

void GLWidget::initializeGL()
{
qglClearColor( Qt::black );
}

void GLWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT );

glLoadIdentity();
qglColor( Qt::white );
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();
}

void GLWidget::resizeGL(int w, int h)
{
h = h < 1 ? 1 : h;
glViewport( 0, 0, (GLint)w, (GLint)h );
}


mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QMenuBar>
#include <QAction>
#include <QApplication>
#include "glwidget.h"
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
GLWidget *w;
};

#endif // MAINWINDOW_H

mainwindow.cpp
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
w=new GLWidget(this);
setCentralWidget(w);

QMenu *fileMenu = new QMenu("&File");
fileMenu->addAction("&Close", qApp, SLOT(quit()), Qt::CTRL + Qt::Key_Q);
menuBar()->addMenu(fileMenu);
}

MainWindow::~MainWindow()
{

}

main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

opengl.pro
QT += opengl
TARGET = opengl
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h

沒有留言: