设为首页 加入收藏

TOP

深入理解Qt多线程(二)
2014-11-23 21:34:05 来源: 作者: 【 】 浏览:26
Tags:深入 理解 线程
ew QLabel(); mLabel->setText(Bitch); computePiThread = new ComputeThread; mainLayout->setSpacing(10); mainLayout->addWidget(calButton); mainLayout->addWidget(hiButton); mainLayout->addWidget(mLabel); QWidget *centreWidget=new QWidget(this); centreWidget->setLayout(mainLayout); this->setCentralWidget(centreWidget); this->connect(computePiThread,SIGNAL(computeFinish(double)),this, SLOT(slotShowResult(double))); this->connect(hiButton,SIGNAL(released()),this, SLOT(slotSayHi())); this->connect(calButton,SIGNAL(released()),this, SLOT(slotGetPi())); } MainWindow::~MainWindow() { computePiThread->terminate(); computePiThread->wait(); delete computePiThread; computePiThread = 0; } void MainWindow::slotGetPi() { computePiThread->start(); } void MainWindow::slotSayHi() { mLabel->setText(Hei,gay~); } void MainWindow::slotShowResult(double result) { mLabel->setText(QString::number(result)); }
运行结果

/

修改之后计算就在子线程中进行,主线程就没有卡死的情况了。

MultiThread in Opengl

之前有用QT作为框架来学习OpenGL,参考这里。

当是有个问题没有解决,就是当想要GLWidget中的图形不断的进行变换的话,就要在主线程中加一个死循环,这样做只是权宜之记,最好的解决方法就是用多线程。

创建一个GLThread类专门用来渲染:

glthread.h

#ifndef GLTHREAD_H
#define GLTHREAD_H

#include 
  
   
#include 
   
     #include 
    
      #include
     
       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; GLWidget *glw; }; #endif // GLTHREAD_H 
     
    
   
  

glthread.cpp

#include glthread.h
#include glwidget.h

GLThread::GLThread(GLWidget *gl)
    : QThread(), glw(gl)
{
    doRendering = true;
    doResize = false;
}

void GLThread::stop()
{
    doRendering = false;
}

void GLThread::resizeViewport(const QSize &size)
{
    w = size.width();
    h = size.height();
    doResize = true;
}

void GLThread::run()
{

    glw->makeCurrent();
    this->rotAngle = 0.0;
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		// This Will Clear The Background Color To Black
    glClearDepth(1.0);				// Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS);				// The Type Of Depth Test To Do
    glEnable(GL_DEPTH_TEST);			// Enables Depth Testing
    glShadeModel(GL_SMOOTH);			// Enables Smooth Color Shading

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();				// Reset The Projection Matrix

    gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);	// Calculate The Aspect Ratio Of The Window

    glMatrixMode(GL_MODELVIEW);

    while (doRendering)
    {
        rotAngle +=5;
        if(rotAngle>=360)
            rotAngle = 0;
        if (doResize)
        {
            glViewport(0, 0, w, h);
            doResize = false;

            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();

            gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
            glMatrixMode(GL_MODELVIEW);
        }
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
        glLoadIdentity();				// Reset The View

        glTranslatef(-1.5f,0.0f,-6.0f);		// Move Left 1.5 Units And Into The Screen 6.0

        glRotatef(rotAngle,0.0f,0.0f,1.0f);		// Rotate The Triangle On The Y axis
        // draw a triangle (in smooth coloring mode)
        glBegin(GL_POLYGON);				// start drawing a polygon
        glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
        glVertex3f( 0.0f, 1.0f, 0.0f);		// Top
        glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
        glVertex3f( 1.0f,-1.0f, 0.0f);		// Bottom R
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言字符串操作函数 下一篇2006 ACM Northwestern European ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: