#ifndef GLWIDGET_H#define GLWIDGET_H#include <QGLWidget>#include "glthread.h"class GLWidget : public QGLWidget{public: GLWidget(QWidget *parent); void startRendering(); void stopRendering();protected: void resizeEvent(QResizeEvent *evt); void paintEvent(QPaintEvent *); void closeEvent(QCloseEvent *evt); GLThread glt;};
#include "glwidget.h"#include <QResizeEvent>GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent), glt(this){ setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer)); qglClearColor(Qt::white); setAutoBufferSwap(false); resize(320, 240);}void GLWidget::startRendering(){ glt.start();}void GLWidget::stopRendering(){ glt.stop(); glt.wait();}void GLWidget::resizeEvent(QResizeEvent *evt){ glt.resizeViewport(evt->size());}void GLWidget::paintEvent(QPaintEvent *){ // Handled by the GLThread.}void GLWidget::closeEvent(QCloseEvent *evt){ stopRendering(); QGLWidget::closeEvent(evt);}
#ifndef GLTHREAD_H#define GLTHREAD_H#include <QThread>#include <QSize>class GLWidget;class GLThread : public QThread{public: GLThread(GLWidget *glWidget); void resizeViewport(const QSize &size); void run(); void stop();private: bool doRendering; bool doResize; int w; int h; int rotAngle;public: GLWidget *glw;};#endif // GLTHREAD_H
#include "glthread.h"#include <QtGui>#include <QtOpenGL>#include "glwidget.h"GLThread::GLThread(GLWidget *gl) : QThread(), glw(gl){ doRendering = true; doResize = false; // glw->qglClearColor(Qt::white);}void GLThread::stop(){ doRendering = false;}void GLThread::resizeViewport(const QSize &size){ glMatrixMode(GL_PROJECTION); w = size.width(); h = size.height(); doResize = true;}void GLThread::run(){ srand(QTime::currentTime().msec()); rotAngle = rand() % 360; glw->makeCurrent(); // glw->qglClearColor(Qt::white); // qglClearColor(Qt::white); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-5.0, 5.0, -5.0, 5.0, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glViewport(0, 0, 200, 200); glClearColor(1.0, 1.0, 1.0, 1.0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST);// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // test of painting// glBegin(GL_LINES);// glVertex2f(-0.6,-0.4);// glVertex2f(0.6,-0.4);// glVertex2f(-0.6,-0.6);// glVertex2f(0.6,-0.6);// glVertex2f(-0.6,-0.8);// glVertex2f(0.6,-0.8);// glEnd(); while (doRendering) { if (doResize) { glViewport(0, 0, w, h); doResize = false; } glw->swapBuffers(); msleep(40); }}
#ifndef APPWINDOW_H#define APPWINDOW_H#include <QMainWindow>#include <QWorkspace>class AppWindow: public QMainWindow{ Q_OBJECTpublic: AppWindow();protected: void closeEvent(QCloseEvent *evt);private slots: void newThread(); // void killThread();private: QWorkspace *ws;};#endif // APPWINDOW_H
#include "appwindow.h"#include "glwidget.h"#include "glthread.h"#include<QtGui>#include <QMenu>#include<QMenuBar>#include <QAction>AppWindow::AppWindow() : QMainWindow(0){ QMenu *menu = new QMenu(this); menuBar()->addMenu(menu); menu=menuBar()->addMenu("&Thread"); menu->addAction("&New thread", this, SLOT(newThread()), Qt::CTRL+Qt::Key_N); // menu->addAction("&End current thread", this, SLOT(killThread()), Qt::CTRL+Qt::Key_K); menu->addSeparator(); menu->addAction("&Exit", qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q); menuBar()->show(); ws = new QWorkspace(this); setCentralWidget(ws);}void AppWindow::closeEvent(QCloseEvent *evt){ QWidgetList windows = ws->windowList(); for (int i = 0; i < int(windows.count()); ++i) { GLWidget *window = (GLWidget *)windows.at(i); window->stopRendering(); } QMainWindow::closeEvent(evt);}void AppWindow::newThread(){ QWidgetList windows = ws->windowList(); GLWidget *widget = new GLWidget(ws); widget->setWindowTitle("Thread #" + QString::number(windows.count() + 1)); // widget->resize(200,200); widget->show(); widget->startRendering();}//void AppWindow::killThread()//{// GLWidget *widget = (GLWidget *)ws->activeWindow();// if (widget) {// widget->stopRendering();// delete widget;// }//}
#include <QApplication>#include "appwindow.h"#include "glwidget.h"#include "glthread.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); AppWindow aw; // app.setMainWidget(&aw); aw.show(); return app.exec();}