码迷,mamicode.com
首页 > 其他好文 > 详细

qt在tabwidget中使用opengl

时间:2015-04-14 19:52:58      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

    1.创建一个Qt gui application工程,使用设计器调出一个tabwidget控件,如下图

技术分享

 2.pro文件如下

QT += opengl

# LIBS+=-IGLU
LIBS +=   -lglut      -lGLU

TARGET = .
TEMPLATE = app
SOURCES += main.cpp     mainwindow.cpp     nehewidget.cpp
HEADERS += mainwindow.h     nehewidget.h
FORMS += mainwindow.ui

3.输入命令产生makefile文件,记得一定先查看qmake版本是否正确

技术分享

4.输入$make ,产生ui_ mainwindow.h文件,然后修改ui_ mainwindow.h文件如下(红色为修改的地方)

/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 4.8.6
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QTabWidget>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>

<span style="color:#CC0000;">include "nehewidget.h"</span>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralWidget;
    QTabWidget *tabWidget;
    QWidget *tab;
    QPushButton *pushButton;
   <span style="color:#FF0000;"> NeHeWidget </span>*tab_2;
    QPushButton *pushButton_2;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(600, 400);
        centralWidget = new QWidget(MainWindow);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        tabWidget = new QTabWidget(centralWidget);
        tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
        tabWidget->setGeometry(QRect(10, 10, 381, 221));
        tab = new QWidget();
        tab->setObjectName(QString::fromUtf8("tab"));
        pushButton = new QPushButton(tab);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(40, 50, 87, 27));
        tabWidget->addTab(tab, QString());
        tab_2 = new <span style="color:#FF0000;">NeHeWidget</span> ();
        tab_2->setObjectName(QString::fromUtf8("tab_2"));
        pushButton_2 = new QPushButton(tab_2);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setGeometry(QRect(20, 40, 87, 27));
        tabWidget->addTab(tab_2, QString());
        MainWindow->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(MainWindow);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 600, 22));
        MainWindow->setMenuBar(menuBar);
        mainToolBar = new QToolBar(MainWindow);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(MainWindow);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        MainWindow->setStatusBar(statusBar);

        retranslateUi(MainWindow);

        tabWidget->setCurrentIndex(1);


        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
        pushButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));
        tabWidget->setTabText(tabWidget->indexOf(tab), QApplication::translate("MainWindow", "Tab 1", 0, QApplication::UnicodeUTF8));
        pushButton_2->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));
        tabWidget->setTabText(tabWidget->indexOf(tab_2), QApplication::translate("MainWindow", "Tab 2", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H

5.修改Makefile文件,防止编译重新生成ui_mainwindow.h文件覆盖掉修改后的ui_mainwindow.h文件

屏蔽掉如下语句

#compiler_uic_make_all: _mainwindow.h文件ui_mainwindow.h
#compiler_uic_clean:
#       -$(DEL_FILE) ui_mainwindow.h
#ui_mainwindow.h: mainwindow.ui
#       /usr/lib/i386-linux-gnu/qt4/bin/uic mainwindow.ui -o ui_mainwindow.h
还有修改语句

compiler_clean: compiler_moc_header_clean compiler_uic_clean为

compiler_clean: compiler_moc_header_clean 


6,经过以上处理,重新$make一下,生成opengltest可执行文件

输入$./opengltest后如下

  技术分享

opengl设计的UI已经嵌入到了tabwidget空间中去了


附上程序的相关代码如下:

opengltest.pro

######################################################################
# Automatically generated by qmake (2.01a) ?? 4? 14 15:43:00 2015
######################################################################


QT += opengl

# LIBS+=-IGLU
LIBS +=   -lglut      -lGLU

#INCPATH +=/opt/EmbedSky/qt-4.5/build/build_arm/include/QtOpenGL

TARGET = opengltest
TEMPLATE = app
SOURCES += main.cpp     mainwindow.cpp     nehewidget.cpp
HEADERS += mainwindow.h     nehewidget.h
FORMS += mainwindow.ui

mainwindow.h如下

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
     ui->tab_2-> resizeGL(10, 10);
    ui->tab_2->initializeGL();
    ui->tab_2->paintGL();
   // ui->tab_2-> resizeGL(10, 10);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

mainwindow.cpp文件如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
     ui->tab_2-> resizeGL(10, 10);
    ui->tab_2->initializeGL();
    ui->tab_2->paintGL();
   // ui->tab_2-> resizeGL(10, 10);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

nehewidget.h文件如下:

#ifndef NEHEWLDGET_H
#define NEHEWLDGET_H

#include <QGLWidget>
#include <QtGui>
#include <QtOpenGL>
#include <GL/glu.h>
//#include</opt/EmbedSky/qt-4.5/build/build_arm/include/QtOpenGL/QGLWidget>

class NeHeWidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit NeHeWidget(QWidget *parent = 0);
    ~NeHeWidget();
//protected:
    //设置渲染环境
    void initializeGL();
    //绘制窗口
    void paintGL();
    //响应窗口的大小变化
    void resizeGL( int width, int height );
};

#endif // NEHEWLDGET_H

nehewidget.cpp文件如下:

#include "nehewidget.h"

NeHeWidget::NeHeWidget(QWidget *parent) :
    QGLWidget(parent)
{
}
NeHeWidget::~NeHeWidget()
{}
void NeHeWidget::initializeGL()
{
     // 启用阴影平滑
    glShadeModel( GL_SMOOTH );
    // 黑色背景
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
    // 设置深度缓存
    glClearDepth( 1.0 );
     // 启用深度测试
    glEnable( GL_DEPTH_TEST );
    // 所作深度测试的类型
    glDepthFunc( GL_LEQUAL );
    // 告诉系统对透视进行修正
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
void NeHeWidget::paintGL()
{
#if 0
    // 清除屏幕和深度缓存
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    //坐标转移
    glTranslatef(-1.5f,0.0f,-6.0f);
    //设置颜色
    glColor3f( 1.0, 1.0, 1.0 );
    //绘制一个正方形
    glBegin( GL_QUADS );
    glVertex3f( -1.0,  1.0,  0.0 );
    glVertex3f(  1.0,  1.0,  0.0 );
    glVertex3f(  1.0, -1.0,  0.0 );
    glVertex3f( -1.0, -1.0,  0.0 );
    glEnd();
#endif
    int rTri = 0;
// 清除屏幕和深度缓存
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    //移到屏幕的左半部分,并且将视图推入屏幕背后足够的距离以便我们可以看见全部的场景
    glTranslatef(-1.0f,0.0f,-6.0f);
    glRotatef(rTri,1.0,0,0);
    glBegin( GL_TRIANGLE_STRIP );
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  0.0,  1.0,  0.0 );
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f(-1.0, -1.0,  1.0 );
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f(  1.0, -1.0,  1.0 );
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f(  1.0, -1.0, -1.0 );
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  0.0,  1.0,  0.0 );
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f(-1.0, -1.0,  1.0 );
    glEnd();
    rTri+=2;

    glLoadIdentity();
    //移到屏幕的右半部分,并且将视图推入屏幕背后足够的距离以便我们可以看见全部的场景
    glTranslatef(1.0f,0.0f,-6.0f);
    glRotatef(rTri,1.0,0,0);
    glBegin(GL_QUAD_STRIP);
    //第一面
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  0.0,  0.0,  0.0 );
    glVertex3f(  0.0,  0.0,  1.0 );
    glVertex3f(  1.0,  0.0,  0.0 );
    glVertex3f(  1.0,  0.0,  1.0 );
    //第二个 面
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f(  1.0,  1.0,  0.0 );
    glVertex3f(  1.0,  1.0,  1.0 );
    //第三个面
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f(  0.0,  1.0,  0.0 );
    glVertex3f(  0.0,  1.0,  1.0 );
    //第四个面
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  0.0,  0.0,  0.0 );
    glVertex3f(  0.0,  0.0,  1.0 );
    glEnd();
    //第五个和第六个面
    glBegin(GL_QUADS);
    glColor3f( 0.0, 0.8, 0.8 );
    glVertex3f(0.0,1.0,1.0);
    glVertex3f(0.0,0.0,1.0);
    glVertex3f(1.0,0.0,1.0);
    glVertex3f(1.0,1.0,1.0);
    glVertex3f(0.0,1.0,0.0);
    glVertex3f(0.0,0.0,0.0);
    glVertex3f(1.0,0.0,0.0);
    glVertex3f(1.0,1.0,0.0);
    glEnd();
}
 // 重置OpenGL窗口大小
void NeHeWidget::resizeGL(int width, int height)
{
        // 防止窗口大小变为0
    if ( height == 0 )
    {
        height = 1;
    }
    // 重置当前的视口
    glViewport( 0, 0, (GLint)width, (GLint)height );
    // 选择投影矩阵
    glMatrixMode( GL_PROJECTION );
        // 重置投影矩阵
    glLoadIdentity();
    // 设置视口的大小
    gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
    // 选择模型观察矩阵
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

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();
}

以上代码可能包含较多调试信息,大家可以忽略即可.




qt在tabwidget中使用opengl

标签:

原文地址:http://blog.csdn.net/xiao_ping_ping/article/details/45044559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!