Qt App在GLWidget.show()上导致分段错误;

时间:2012-01-19 22:21:44

标签: c++ qt opengl ubuntu segmentation-fault

当我单步执行调试器时,会收到以下弹出消息:

The inferior stopped because it received a signal from the Operating System.

Signal name : 
SIGSEGV
Signal meaning : 
Segmentation fault

这依赖于(我认为)这些代码行:

#ifndef Q_WS_WINCE
    inline void show() { setVisible(true); }
#else
    void show();
#endif
    inline void hide() { setVisible(false); }
    inline QT_MOC_COMPAT void setShown(bool shown) { setVisible(shown); }

据我所知,Q_WS_WINCE未定义,但我无法通过Google找到其背后的定义; this是我能找到的最接近的,虽然它没有CE定义。

根据我的调试器,这是被调用的内容,因为#ifndef Q_WS_WINCE函数调用时箭头跟inline void show() { setVisible(true); }之后。

我不知道如何发生这种情况;这里发生了什么?我正在使用Ubuntu 11.10,以及Qt Creator作为我的IDE。我还应该注意到,这只是在几分钟前开始发生的;在没有任何问题之前,我能够逐步完成我的大部分代码。

GLWidget标题文件

#pragma once

#include <QGLWidget>
#include <QInputEvent>
#include "Cube.h"

class GLWidget : public QGLWidget
{
    Q_OBJECT

public:
    explicit GLWidget( QWidget *parent = 0 );

    ~GLWidget( void );

    QSize minimumSizeHint( void ) const;

    QSize sizeHint( void ) const;

public slots:
    void SetXRotation( int angle );

    void SetYRotation( int angle );

    void SetZRotation( int angle );

signals:
    void xRotationChanged( int angle );

    void yRotationChanged( int angle );

    void zRotationChanged( int angle );

protected:
    void initializeGL( void );

    void paintGL( void );

    void resizeGL( int width, int height );

    void mousePressEvent( QMouseEvent *event );

    void mouseMoveEvent( QMouseEvent *event );

private:
    Cube *mCube;

    int mXRot;

    int mYRot;

    int mZRot;

    QPoint mLastPos;

    QColor mQtGreen;

    QColor mQtPurple;

};

GLWidget源文件

#include <GL/freeglut.h>

#include <QtGui>
#include <QtOpenGL>

#include <qmath.h>
#include <QMatrix4x4>

#include "GLWidget.h"

#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif

static void qNormalizeAngle( int &angle )
{
    const int ANGLE_MULTIPLIER = 360 * 16;

    while( angle < 0 )
        angle += ANGLE_MULTIPLIER;
    while ( angle > ANGLE_MULTIPLIER )
        angle -= ANGLE_MULTIPLIER;
}

GLWidget::GLWidget( QWidget *parent ) :
    QGLWidget( parent ),
    mXRot( 0 ),
    mYRot( 0 ),
    mZRot( 0 )
{    
}

GLWidget::~GLWidget( void )
{
    delete mCube;
}

void GLWidget::initializeGL( void )
{
    mQtGreen = QColor::fromCmyk( 0.40, 0.0, 1.0, 0.0 );
    mQtPurple = QColor::fromCmyk( 0.39, 0.39, 0.0, 0.0 );

    //glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );

    qglClearColor( mQtPurple );

    mCube = new Cube( this );
    mCube->Init();
    mCube->SetColor( mQtGreen.dark() );

    glEnable( GL_DEPTH_TEST );
    glEnable( GL_CULL_FACE );
    glShadeModel( GL_SMOOTH );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glEnable( GL_MULTISAMPLE );
    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
}

QSize GLWidget::minimumSizeHint( void ) const
{
    return QSize( 50, 50 );
}

void GLWidget::mouseMoveEvent( QMouseEvent *event )
{
    mLastPos = event->pos();
}

void GLWidget::mousePressEvent( QMouseEvent *event )
{
    static const int ANGLE_ADDIFIER = 8;

    int dx = event->x() - mLastPos.x();
    int dy = event->y() - mLastPos.y();

    if ( event->buttons() & Qt::LeftButton )
    {
        SetXRotation( ( mXRot + ANGLE_ADDIFIER ) * dy );
        SetYRotation( ( mYRot + ANGLE_ADDIFIER ) * dx );

        qDebug() << "You pressed the left mouse button!" << '\n';
    }
    else if ( event->buttons() & Qt::RightButton )
    {
        SetXRotation( ( mXRot + ANGLE_ADDIFIER ) * dy );
        SetZRotation( ( mZRot + ANGLE_ADDIFIER ) * dx );

        qDebug() << "You pressed the right mouse button!" << '\n';
    }

    mLastPos = event->pos();
}

void GLWidget::paintGL( void )
{
    static const GLfloat ANGLE_DIVISOR = 16.0;

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    glTranslatef( 0.0, 0.0, -10.0 );

    glRotatef( mXRot / ANGLE_DIVISOR, 1.0, 0.0, 0.0 );
    glRotatef( mYRot / ANGLE_DIVISOR, 0.0, 1.0, 0.0 );
    glRotatef( mZRot / ANGLE_DIVISOR, 0.0, 0.0, 1.0 );

    mCube->Draw();
}

void GLWidget::resizeGL( int width, int height )
{
    int side = qMin( width, height );

    //glutInitWindowPosition( 0, 0 );
    //glutInitWindowSize( width, height );

    glViewport( ( width - side ) / VIEWPORT_DIVISOR, ( height - side ) / VIEWPORT_DIVISOR, side, side );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

#ifdef QT_OPENGL_ES_1
    glOrthof( -0.5, +0.5, -0.5, +0.5, 4.0, 15.0 );
#else
    glOrtho( -0.5, +0.5, -0.5, +0.5, 4.0, 15.0 );
#endif*/

    glMatrixMode( GL_MODELVIEW );
}

void GLWidget::SetXRotation( int angle )
{
    qNormalizeAngle( angle );

    if ( angle != mYRot )
    {
        mYRot = angle;
        emit yRotationChanged( angle );
        updateGL();
    }
}

void GLWidget::SetYRotation( int angle )
{
    qNormalizeAngle( angle );

    if ( angle != mXRot )
    {
        mXRot = angle;
        emit xRotationChanged( angle );
        updateGL();
    }
}

void GLWidget::SetZRotation( int angle )
{
    qNormalizeAngle( angle );

    if ( angle != mZRot )
    {
        mZRot = angle;
        emit zRotationChanged( angle );
        updateGL();
    }
}

QSize GLWidget::sizeHint() const
{
    return QSize( 400, 400 );
}

1 个答案:

答案 0 :(得分:0)

找到了罪魁祸首:我有一个mCube->Init();函数调用,它在代码中的某处放错了位置。现在应该考虑解决这个问题。