《Qt》part 11 Qt5.5 VowelBox

来源:互联网 发布:qq企业邮箱 smtp 端口 编辑:程序博客网 时间:2024/06/08 14:33

《Qt》part 11 Qt5.5 VowelBox

第二十章中的OpenGL的代码,在Qt5.5中的调试,有的需要注意,不然能通过调试,但是无法得到线框矩形。


代码:

main.cpp

#include <QtWidgets/QApplication>#include <iostream>#include "vowelcube.h"int main(int argc, char *argv[]){QApplication a(argc, argv);if (!QGLFormat::hasOpenGL()) {std::cerr << "This system has no OpenGL support" << std::endl;return 1;}VowelCube cube;cube.setWindowTitle(QObject::tr("Vowel Cube"));cube.setMinimumSize(200, 200);cube.resize(450, 450);cube.show();//VowelCube w;//w.show();return a.exec();}

vowelcube.h

#ifndef VOWELCUBE_H#define VOWELCUBE_H#include <QGLWidget>#include <QRadialGradient>class VowelCube : public QGLWidget{    Q_OBJECTpublic:    VowelCube(QWidget *parent = 0);    ~VowelCube();protected:    void paintEvent(QPaintEvent *event);    void mousePressEvent(QMouseEvent *event);    void mouseMoveEvent(QMouseEvent *event);    void wheelEvent(QWheelEvent *event);private:    void createGradient();    void createGLObject();    void drawBackground(QPainter *painter);    void drawCube();    void drawLegend(QPainter *painter);    GLuint glObject;    QRadialGradient gradient;    GLfloat rotationX;    GLfloat rotationY;    GLfloat rotationZ;    GLfloat scaling;    QPoint lastPos;};#endif

vowelcube.cpp

#include <QtGui>#include <QtOpenGL>#include <cmath>#ifndef GL_MULTISAMPLE#define GL_MULTISAMPLE 0x809D#endif#include "vowelcube.h"VowelCube::VowelCube(QWidget *parent)    : QGLWidget(parent){    // 必须注释掉    //setFormat(QGLFormat(QGL::SampleBuffers));    rotationX = -38.0;    rotationY = -58.0;    rotationZ = 0.0;    scaling = 1.0;    // 后面添加的    setAutoBufferSwap( false );    setAutoFillBackground( false );    createGradient();    createGLObject();}VowelCube::~VowelCube(){    makeCurrent();    glDeleteLists(glObject, 1);}void VowelCube::paintEvent(QPaintEvent * /* event */){    /* 这是原有的代码    QPainter painter(this);    drawBackground(&painter);    drawCube();    drawLegend(&painter);    */    // 这是后面添加的代码    QPainter painter(this);    drawBackground(&painter);    painter.end();    drawCube();    painter.begin(this);    drawLegend(&painter);    painter.end();    swapBuffers();}void VowelCube::mousePressEvent(QMouseEvent *event){    lastPos = event->pos();}void VowelCube::mouseMoveEvent(QMouseEvent *event){    GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();    GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();    if (event->buttons() & Qt::LeftButton) {        rotationX += 180 * dy;        rotationY += 180 * dx;        update();    } else if (event->buttons() & Qt::RightButton) {        rotationX += 180 * dy;        rotationZ += 180 * dx;        update();    }    lastPos = event->pos();}void VowelCube::wheelEvent(QWheelEvent *event){    double numDegrees = -event->delta() / 8.0;    double numSteps = numDegrees / 15.0;    scaling *= std::pow(1.125, numSteps);    update();}void VowelCube::createGradient(){    gradient.setCoordinateMode(QGradient::ObjectBoundingMode);    gradient.setCenter(0.45, 0.50);    gradient.setFocalPoint(0.40, 0.45);    gradient.setColorAt(0.0, QColor(105, 146, 182));    gradient.setColorAt(0.4, QColor(81, 113, 150));    gradient.setColorAt(0.8, QColor(16, 56, 121));}void VowelCube::createGLObject(){    makeCurrent();    glShadeModel(GL_FLAT);    glObject = glGenLists(1);    glNewList(glObject, GL_COMPILE);    qglColor(QColor(255, 239, 191));    glLineWidth(1.0);    glBegin(GL_LINES);    glVertex3f(+1.0, +1.0, -1.0);    glVertex3f(-1.0, +1.0, -1.0);    glVertex3f(+1.0, -1.0, -1.0);    glVertex3f(-1.0, -1.0, -1.0);    glVertex3f(+1.0, -1.0, +1.0);    glVertex3f(-1.0, -1.0, +1.0);    glEnd();    glBegin(GL_LINE_LOOP);    glVertex3f(+1.0, +1.0, +1.0);    glVertex3f(+1.0, +1.0, -1.0);    glVertex3f(+1.0, -1.0, -1.0);    glVertex3f(+1.0, -1.0, +1.0);    glVertex3f(+1.0, +1.0, +1.0);    glVertex3f(-1.0, +1.0, +1.0);    glVertex3f(-1.0, +1.0, -1.0);    glVertex3f(-1.0, -1.0, -1.0);    glVertex3f(-1.0, -1.0, +1.0);    glVertex3f(-1.0, +1.0, +1.0);    glEnd();    glEndList();}void VowelCube::drawBackground(QPainter *painter){    painter->setPen(Qt::NoPen);    painter->setBrush(gradient);    painter->drawRect(rect());}void VowelCube::drawCube(){    makeCurrent();    glPushAttrib(GL_ALL_ATTRIB_BITS);    glMatrixMode(GL_PROJECTION);    glPushMatrix();    glLoadIdentity();    GLfloat x = 3.0 * GLfloat(width()) / height();    glOrtho(-x, +x, -3.0, +3.0, 4.0, 15.0);    glMatrixMode(GL_MODELVIEW);    glPushMatrix();    glLoadIdentity();    glTranslatef(0.0, 0.0, -10.0);    glScalef(scaling, scaling, scaling);    glRotatef(rotationX, 1.0, 0.0, 0.0);    glRotatef(rotationY, 0.0, 1.0, 0.0);    glRotatef(rotationZ, 0.0, 0.0, 1.0);    glEnable(GL_MULTISAMPLE);    glCallList(glObject);    setFont(QFont("Times", 24));    qglColor(QColor(255, 223, 127));    renderText(+1.1, +1.1, +1.1, QChar('a'));    renderText(-1.1, +1.1, +1.1, QChar('e'));    renderText(+1.1, +1.1, -1.1, QChar('o'));    renderText(-1.1, +1.1, -1.1, QChar(0x00F6));    renderText(+1.1, -1.1, +1.1, QChar(0x0131));    renderText(-1.1, -1.1, +1.1, QChar('i'));    renderText(+1.1, -1.1, -1.1, QChar('u'));    renderText(-1.1, -1.1, -1.1, QChar(0x00FC));    glMatrixMode(GL_MODELVIEW);    glPopMatrix();    glMatrixMode(GL_PROJECTION);    glPopMatrix();    glPopAttrib();}void VowelCube::drawLegend(QPainter *painter){    const int Margin = 11;    const int Padding = 6;    QTextDocument textDocument;    textDocument.setDefaultStyleSheet("* { color: #FFEFEF }");    textDocument.setHtml("<h4 align=\"center\">Vowel Categories</h4>"                         "<p align=\"center\"><table width=\"100%\">"                         "<tr><td>Open:<td>a<td>e<td>o<td>ö"                         "<tr><td>Close:<td>ı<td>i<td>u<td>ü"                         "<tr><td>Front:<td>e<td>i<td>ö<td>ü"                         "<tr><td>Back:<td>a<td>ı<td>o<td>u"                         "<tr><td>Round:<td>o<td>ö<td>u<td>ü"                         "<tr><td>Unround:<td>a<td>e<td>ı<td>i"                         "</table>");    textDocument.setTextWidth(textDocument.size().width());    QRect rect(QPoint(0, 0), textDocument.size().toSize()               + QSize(2 * Padding, 2 * Padding));    painter->translate(width() - rect.width() - Margin,                       height() - rect.height() - Margin);    painter->setPen(QColor(255, 239, 239));    painter->setBrush(QColor(255, 0, 0, 31));    painter->drawRect(rect);    painter->translate(Padding, Padding);    textDocument.drawContents(painter);}

注意:

1、本程序在VS中调试时,需要在Qt Project Settings中的Qt Modules选中OpenGL模块。

2、构造函数中修改为:

VowelCube::VowelCube(QWidget *parent): QGLWidget(parent){// 必须注释掉//setFormat(QGLFormat(QGL::SampleBuffers));rotationX = -38.0;rotationY = -58.0;rotationZ = 0.0;scaling = 1.0;// 后面添加的setAutoBufferSwap(false);setAutoFillBackground(false);createGradient();createGLObject();}

3、paintEvent修改为:

void VowelCube::paintEvent(QPaintEvent * /* event */){/* 这是原有的代码QPainter painter(this);drawBackground(&painter);drawCube();drawLegend(&painter);*/// 这是后面添加的代码QPainter painter(this);drawBackground(&painter);painter.end();drawCube();painter.begin(this);drawLegend(&painter);painter.end();swapBuffers();}


0 0
原创粉丝点击