• 14594阅读
  • 7回复

关于C++ Gui Programming with Qt4第20章Vowel Cube的bug! [复制链接]

上一主题 下一主题
离线comfanter
 
只看楼主 倒序阅读 楼主  发表于: 2011-03-18
[pre][pre][pre]运行时出现如下错误
Starting E:\Qt\OpenGL\QtOpenGL\VowelCube-build-desktop\debug\VowelCube.exe...[/pre][/pre][/pre]
ASSERT: "d" in file ..\..\include/QtCore/../../src/corelib/tools/qscopedpointer.h, line 112
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
QWidget::repaint: Recursive repaint detected
ASSERT: "d" in file ..\..\include/QtCore/../../src/corelib/tools/qscopedpointer.h, line 112
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
不知道是什么原因,恳请各位高人相助,感激不尽!

[pre][pre][pre][pre][pre]下面是源代码[/pre][/pre][/pre][/pre][/pre]// vowelcube.h
#ifndef
VOWELCUBE_H[pre]#define VOWELCUBE_H[/pre][pre]#include <QtCore>[/pre][pre]#include <QtGui>[/pre][pre]#include <QtOpenGL>[/pre][pre]class VowelCube : public QGLWidget[/pre][pre]{[/pre][pre]    Q_OBJECT[/pre][pre]public:[/pre][pre]    VowelCube(QWidget *parent = 0);[/pre][pre]    ~VowelCube();[/pre][pre]protected:[/pre][pre]    void paintEvent(QPaintEvent *e);[/pre][pre]    void mousePressEvent(QMouseEvent *e);[/pre][pre]    void mouseMoveEvent(QMouseEvent *e);[/pre][pre]    void wheelEvent(QWheelEvent *e);[/pre][pre]private:[/pre][pre]    void createGradient();[/pre][pre]    void createGLObject();[/pre][pre]    void drawBackground(QPainter *painter);[/pre][pre]    void drawCube();[/pre][pre]    void drawLegend(QPainter *painter);[/pre][pre]    GLuint glObject;[/pre][pre]    QRadialGradient gradient;[/pre][pre]    GLfloat xrot, yrot, zrot;[/pre][pre]    GLfloat scaling;[/pre][pre]    QPoint lastPos;[/pre][pre]};[/pre][pre]#endif // VOWELCUBE_H[/pre][pre]
// vowelcube.cpp
#include <cmath>#include "vowelcube.h"#ifndef GL_MULTISAMPLE#define GL_MULTISAMPLE 0x809D#endifVowelCube::VowelCube(QWidget *parent)    : QGLWidget(parent){    setFormat(QGLFormat(QGL::SampleBuffers));    xrot = -38.0;    yrot = -58.0;    zrot = 0.0;    scaling = 1.0;    createGradient();    createGLObject();}VowelCube::~VowelCube(){    makeCurrent();    glDeleteLists(glObject, 1);}void VowelCube::paintEvent(QPaintEvent *e){    QPainter painter(this);    drawBackground(&painter);    drawCube();    drawLegend(&painter);}void VowelCube::mousePressEvent(QMouseEvent *e){    lastPos = e->pos();}void VowelCube::mouseMoveEvent(QMouseEvent *e){    GLfloat dx = GLfloat(e->x() - lastPos.x()) / width();    GLfloat dy = GLfloat(e->y() - lastPos.y()) / height();    if (e->buttons() & Qt::LeftButton) {        xrot += 180 * dy;        yrot += 180 * dx;        update();    } else if (e->buttons() & Qt::RightButton) {        xrot += 180 * dy;        zrot += 180 * dx;        update();    }    lastPos = e->pos();}void VowelCube::wheelEvent(QWheelEvent *e){    double numDegrees = -e->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(){    static const GLfloat vertex_list[8][3] = {{-1.0, -1.0, 1.0},                                              {1.0, -1.0, 1.0},                                              {1.0, 1.0, 1.0},                                              {-1.0, 1.0, 1.0},                                              {-1.0, -1.0, -1.0},                                              {1.0, -1.0, -1.0},                                              {1.0, 1.0, -1.0},                                              {-1.0, 1.0, -1.0}};    static const int vertex_index_list1[6] = {6, 7, 5, 4, 1, 0};    static const int vertex_index_list2[10] = {2, 6, 5, 1, 2, 3, 7, 4, 0, 3};    makeCurrent();    glShadeModel(GL_FLAT);    glObject = glGenLists(1);    glNewList(glObject, GL_COMPILE); {        qglColor(QColor(255, 239, 191));        glLineWidth(1.0);        glBegin(GL_LINES); {            for (int i = 0; i < 6; i++)                glVertex3fv(vertex_list[vertex_index_list1[i]]);        } glEnd();        glBegin(GL_LINE_LOOP); {            for (int i = 0; i < 10; i++)                glVertex3fv(vertex_list[vertex_index_list2[i]]);        } glEnd();    } glEndList();}void VowelCube::drawBackground(QPainter *painter){    painter->setPen(Qt::NoPen);    painter->setBrush(gradient);    painter->drawRect(rect());}void VowelCube::drawCube(){    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(xrot, 1.0, 0.0, 0.0);            glRotatef(yrot, 0.0, 1.0, 0.0);            glRotatef(zrot, 0.0, 0.0, 1.0);            glEnable(GL_MULTISAMPLE);            glCallList(glObject);            setFont(QFont("Times", 24));            qglColor(QColor(255, 223, 127));            renderText(1.0, 1.0, 1.0, QChar('a'));            renderText(-1.0, 1.0, 1.0, QChar('e'));            renderText(1.0, 1.0, -1.0, QChar('o'));            renderText(-1.0, 1.0, -1.0, QChar(0x00F6));            renderText(1.0, -1.0, 1.0, QChar(0x0131));            renderText(-1.0, -1.0, 1.0, QChar('i'));            renderText(1.0, -1.0, -1.0, QChar('u'));            renderText(-1.0, -1.0, -1.0, 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>&uuml"                         "<tr><td>Front:<td>e<td>i<td>ö<td>&uuml"                         "<tr><td>Back:<td>a<td>ı<td>o<td>u"                         "<tr><td>Round:<td>o<td>ö<td>u<td>&uuml"                         "<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);}[pre]
// main.cpp
#include <QtGui/QApplication>#include "vowelcube.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    if (!QGLFormat::hasOpenGL()) {        QMessageBox::warning(0, "Fatal Error!", "This system has no OpenGL support!");        return -1;    }    VowelCube w;    w.setWindowTitle(QObject::tr("Vowel Cube"));    w.setMinimumSize(200, 200);    w.resize(500, 500);    w.show();    return a.exec();}[pre]
[/pre][/pre][/pre]
离线comfanter
只看该作者 1楼 发表于: 2011-03-18
额?上面代码格式乱了? vowelcube.h (1 K) 下载次数:3 vowelcube.cpp (6 K) 下载次数:2 main.cpp (1 K) 下载次数:2   
离线comfanter
只看该作者 2楼 发表于: 2011-03-18
我把构造函数第一句
setFormat(QGLFormat(QGL::SampleBuffers)); 注释了,程序能运行了,但是立方体无法显示
出现这样的提示:
QGLWidget::renderText(): Calling renderText() while a GL 2 paint engine is active on the same device is not allowed.
我上网查了setFormat()这个函数存在bug
离线wxj120bw

只看该作者 3楼 发表于: 2011-03-18
你的程序我编译运行后并没有弹出断言出错的问题 你的qt版本是多少?
离线comfanter
只看该作者 4楼 发表于: 2011-03-18
我用的是qt4.7.0的,也有人说用linux就没问题,我等会儿再试试
离线wxj120bw

只看该作者 5楼 发表于: 2011-03-18
我用的是qt4.6版本 qt4.7运行程序的确有些问题
离线comfanter
只看该作者 6楼 发表于: 2011-03-19
现在问题解决了,不是版本的问题,以下是改动部分
[pre]VowelCube::VowelCube(QWidget *parent)    : QGLWidget(parent){    //setFormat(QGLFormat(QGL::SampleBuffers));    xrot = -38.0;    yrot = -58.0;    zrot = 0.0;    scaling = 1.0;    setAutoBufferSwap(false);    setAutoFillBackground(false);    createGradient();    createGLObject();}

void VowelCube::paintEvent(QPaintEvent *e){    QPainter painter(this);    //painter.begin(this);    drawBackground(&painter);    painter.end();    //painter.beginNativePainting();    drawCube();    //painter.endNativePainting();    painter.begin(this);    drawLegend(&painter);    painter.end();    swapBuffers();}
[/pre]
离线wxj120bw

只看该作者 7楼 发表于: 2011-03-19
是什么问题能说明下吗
快速回复
限100 字节
 
上一个 下一个