Results 1 to 4 of 4

Thread: Qt Widget Bug.

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Platforms
    Unix/X11

    Default Qt Widget Bug.

    I've been having a strange bug happen in my program, lately. It's been this way for about an hour now, and I'm not quite sure what to do about it.

    First off, I'll post the code which is relevant to what may (or may not) be causing this:

    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3. #include "GLWidget.h"
    4.  
    5. MainWindow::MainWindow( QWidget *parent ) :
    6. QMainWindow( parent ),
    7. ui( new Ui::MainWindow ),
    8. display( new GLWidget )
    9. {
    10. ui->setupUi( this );
    11. //ui->gridLayout->addWidget( display );
    12. //resize( 640, 480 );
    13. installEventFilter( display );
    14. display->setWindowTitle( "OpenGL Cube Rendering Test" );
    15. display->glDraw();
    16. setCentralWidget( display );
    17. }
    18.  
    19. MainWindow::~MainWindow( void )
    20. {
    21. delete display;
    22. delete ui;
    23. }
    To copy to clipboard, switch view to plain text mode 

    The bug is strange, for it resembles the following image:



    Uploaded with ImageShack.us

    I have no idea what could be causing this; I've tried restarting my pc to see if that would help, but unfortunately it doesn't. Does anyone have any idea as to what the problem might be?

    I can post more code too, if necessary.

    Edit:

    Realized I posted the wrong image. Sorry.

    Description

    The bug basically, for some reason, captures whatever lies behind the window and projects it onto the window itself. Is this common for any reason?
    Last edited by litedrive; 21st January 2012 at 02:31.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt Widget Bug.

    I cannot see what you mean from the screen shot. However, calling glDraw() inside the parent window constructor, before the parent window is shown, may explain odd behaviour.

  3. #3
    Join Date
    Aug 2011
    Posts
    5
    Platforms
    Unix/X11

    Default Re: Qt Widget Bug.

    That makes sense. Should I just call it in main after w.show()?


    Added after 8 minutes:


    Update: just tried that. I'm thinking that I may have mis configured my openGL, so I post the functions which use it incase anyone has any idea:

    Qt Code:
    1. void GLWidget::initializeGL( void )
    2. {
    3. mQtGreen = QColor::fromCmyk( 0.40, 0.0, 1.0, 0.0 );
    4. mQtPurple = QColor::fromCmyk( 0.39, 0.39, 0.0, 0.0 );
    5.  
    6. qglClearColor( mQtPurple );
    7. glClearDepth( 1.0f );
    8.  
    9. mCube->SetColor( mQtGreen.dark() );
    10.  
    11. glEnable( GL_VERTEX_PROGRAM_ARB );
    12. glEnable( GL_DEPTH_TEST );
    13. glEnable( GL_CULL_FACE );
    14. glShadeModel( GL_SMOOTH );
    15. glEnable( GL_LIGHTING );
    16. glEnable( GL_LIGHT0 );
    17. glEnable( GL_MULTISAMPLE );
    18. static GLfloat lightPosition[ 4 ] = { 0.5, 5.0, 7.0, 1.0 };
    19. glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
    20.  
    21. qDebug() << "GL Initialized" << '\n';
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void GLWidget::paintGL( void )
    2. {
    3. const GLfloat ANGLE_DIVISOR = 16.0;
    4.  
    5. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    6.  
    7. glMatrixMode( GL_PROJECTION );
    8. glLoadIdentity();
    9.  
    10. glMatrixMode( GL_MODELVIEW );
    11. glTranslatef( 0.0, 0.0, -10.0 );
    12.  
    13. glRotatef( mXRot / ANGLE_DIVISOR, 1.0, 0.0, 0.0 );
    14. glRotatef( mYRot / ANGLE_DIVISOR, 0.0, 1.0, 0.0 );
    15. glRotatef( mZRot / ANGLE_DIVISOR, 0.0, 0.0, 1.0 );
    16.  
    17. glMatrixMode( GL_MODELVIEW );
    18.  
    19. mCube->Draw();
    20.  
    21. qDebug() << "GL Painted" << '\n';
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void GLWidget::resizeGL( int width, int height )
    2. {
    3. const GLdouble NEAR = 1;
    4. const GLdouble FAR = 200;
    5. const GLdouble FOVY = 45;
    6.  
    7. int side = qMin( width, height );
    8.  
    9. glMatrixMode( GL_PROJECTION );
    10.  
    11. gluPerspective(
    12. FOVY,
    13. ( float ) width / ( float ) height,
    14. NEAR,
    15. FAR
    16. );
    17.  
    18. glMatrixMode( GL_MODELVIEW );
    19.  
    20. glViewport( ( width - side ) / VIEWPORT_DIVISOR, ( height - side ) / VIEWPORT_DIVISOR, side, side );
    21.  
    22. glMatrixMode( GL_PROJECTION );
    23.  
    24. glLoadIdentity();
    25.  
    26. #ifdef QT_OPENGL_ES_1
    27. glOrthof( -0.5, +0.5, -0.5, +0.5, 4.0, 15.0 );
    28. #else
    29. glOrtho( -0.5, +0.5, -0.5, +0.5, 4.0, 15.0 );
    30. #endif*/
    31.  
    32. glMatrixMode( GL_MODELVIEW );
    33.  
    34. qDebug() << "GL Resized" << '\n';
    35. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Patch::Draw( void ) const
    2. {
    3. const bool PUSH_AND_POP_MATRIX = CheckMinMatrixStack( GL_MODELVIEW );
    4.  
    5. if ( PUSH_AND_POP_MATRIX )
    6. glPushMatrix();
    7.  
    8. float *faceColor = new float[ 4 ];
    9.  
    10. faceColor[ 0 ] = 0.0f;
    11. faceColor[ 1 ] = 0.0f;
    12. faceColor[ 2 ] = 0.0f;
    13. faceColor[ 3 ] = 1.0f;
    14.  
    15. //qMultMatrix( mMatrix44 );
    16. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, static_cast< GLfloat* >( faceColor ) );
    17.  
    18. delete[] faceColor;
    19.  
    20. //GLushort *indicies = new GLushort;
    21.  
    22. //*indicies = ( *mGeometry->mFaces )[ 0 ];
    23.  
    24. GLenum element;
    25.  
    26. switch ( mFocus )
    27. {
    28. case GF_Triangles:
    29. element = GL_TRIANGLES;
    30. break;
    31. case GF_Quads:
    32. element = GL_QUADS;
    33. break;
    34. default:
    35. element = GL_LINES;
    36. break;
    37. }
    38.  
    39. //glDrawElements( element, mCount, GL_UNSIGNED_SHORT, indicies + mStart );
    40.  
    41. glBegin( element );
    42.  
    43. qreal x = 0, y = 0, z = 0;
    44.  
    45. for ( int i = 0; i < mCount; ++i )
    46. {
    47. x = ( *mGeometry->mVerticies )[ i ].x();
    48. y = ( *mGeometry->mVerticies )[ i ].y();
    49. z = ( *mGeometry->mVerticies )[ i ].z();
    50.  
    51. glVertex3f( x, y, z );
    52. }
    53.  
    54. glEnd();
    55.  
    56. if ( PUSH_AND_POP_MATRIX )
    57. glPopMatrix();
    58. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void Cube::BuildGeometry( int divisions, qreal scale )
    2. {
    3. if ( !mGeometry->mFinalized )
    4. mGeometry->Finalize();
    5. mGeometry->LoadArrays();
    6.  
    7. glEnableClientState( GL_VERTEX_ARRAY );
    8. glEnableClientState( GL_NORMAL_ARRAY );
    9.  
    10. for ( size_t i = 0; i < mParts->size(); i++ )
    11. ( *mParts )[ i ]->Draw();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Cube::RenderCube( const unsigned short type, const QPoint3 &position, const int size )
    2. {
    3. ( *mCubeage )[ "cubes" + '\0' ] += 1;
    4.  
    5. SetColor( type );
    6.  
    7. int x1 = 0, x2 = 0;
    8. int y1 = 0, y2 = 0;
    9. int z1 = 0, z2 = 0;
    10.  
    11. x1 = position.x() - size;
    12. x2 = position.x() + size;
    13.  
    14. y1 = position.y() - size;
    15. y2 = position.y() + size;
    16.  
    17. z1 = ( position.z() - size ) + 1;
    18. z2 = position.z() + size + 1;
    19.  
    20. qreal camZ = mCamera->z();
    21. qreal camY = mCamera->y();
    22. qreal camX = mCamera->x();
    23.  
    24. glMatrixMode( GL_PROJECTION );
    25.  
    26. glTranslatef( 0.0, 0.0, -10.0 );
    27.  
    28. glOrtho( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 );
    29.  
    30. glMatrixMode( GL_MODELVIEW );
    31.  
    32. /*
    33. * if axis > mCamera.axis; then normal3f -1.0 on the axis.
    34. * axis point is axis1
    35. * else if axis < mCamera.axis; then nomral3f 1.0 on the axis.
    36. * axis point is axis2
    37. */
    38.  
    39. /* Pattern: one axis is written twice, with the other variant of the same axis written twice as well, then the
    40. * order at which those axes are written is flipped, yet still following the same pattern.
    41. * Pattern2: one type of axis is still written twice, and the other group of the same axis written twice as well,
    42. * the only difference being that the first group is written once, with the next group written twice, and then that
    43. * first group written once again. The third axis type is written using one type, depending on the camera comparisons.
    44. */
    45.  
    46. const bool PUSH_AND_POP_MATRIX = ( CheckMinMatrixStack( GL_MODELVIEW ) );
    47.  
    48. if ( PUSH_AND_POP_MATRIX )
    49. glPushMatrix;
    50.  
    51. glBegin( GL_QUADS );
    52.  
    53. if ( z1 > camZ )
    54. {
    55. glNormal3f( 0.0, 0.0, -1.0 );
    56.  
    57. glVertex3i( x1, y1, z1 ); //1
    58. glVertex3i( x1, y2, z1 ); //1
    59. glVertex3i( x2, y2, z1 ); //1
    60. glVertex3i( x2, y1, z1 ); //1
    61. }
    62.  
    63. if ( z2 < camZ )
    64. {
    65. glNormal3f( 0.0, 0.0, 1.0 );
    66.  
    67. glVertex3i( x2, y1, z2 ); //2
    68. glVertex3i( x2, y2, z2 ); //2
    69. glVertex3i( x1, y2, z2 ); //2
    70. glVertex3i( x1, y1, z2 ); //2
    71. }
    72.  
    73. if ( y1 > camY )
    74. {
    75. glNormal3f( 0.0, -1.0, 0.0 );
    76.  
    77. glVertex3i( x2, y1, z1 );
    78. glVertex3i( x2, y1, z2 );
    79. glVertex3i( x1, y1, z2 );
    80. glVertex3i( x1, y1, z1 );
    81. }
    82.  
    83. if ( y2 < camY )
    84. {
    85. glNormal3f( 0.0, 1.0, 0.0 );
    86.  
    87. glVertex3i( x1, y2, z1 );
    88. glVertex3i( x1, y2, z2 );
    89. glVertex3i( x2, y2, z2 );
    90. glVertex3i( x2, y2, z1 );
    91. }
    92.  
    93. if ( x1 > camX )
    94. {
    95. glNormal3f( -1.0, 0.0, 0.0 );
    96.  
    97. glVertex3i( x1, y1, z1 );
    98. glVertex3i( x1, y1, z2 );
    99. glVertex3i( x1, y2, z2 );
    100. glVertex3i( x1, y2, z1 );
    101. }
    102.  
    103. if ( x2 < camX )
    104. {
    105. glNormal3f( 1.0, 0.0, 0.0 );
    106.  
    107. glVertex3i( x2, y2, z1 );
    108. glVertex3i( x2, y2, z2 );
    109. glVertex3i( x2, y1, z2 );
    110. glVertex3i( x2, y1, z1 );
    111. }
    112.  
    113. glEnd();
    114.  
    115. if ( PUSH_AND_POP_MATRIX )
    116. glPopMatrix();
    117.  
    118. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Cube::RenderVB( unsigned short type, const QPoint3F &position, int size )
    2. {
    3. static const GLuint NUM_COORDS_PER_VERTEX = 3;
    4. static const GLuint NUM_ARR_TO_DRAW = 24;
    5. static const GLuint FIRST_INDEX_TO_DRAW = 0;
    6.  
    7. ( *mCubeage )[ "cubes" ] += 1;
    8.  
    9. SetColor( type );
    10.  
    11. glMatrixMode( GL_PROJECTION );
    12.  
    13. const bool pushAndPop = CheckMinMatrixStack( GL_PROJECTION );
    14.  
    15. if ( pushAndPop )
    16. glPushMatrix();
    17.  
    18. glTranslatef( position.x(), position.y(), position.z() );
    19. glScalef( size, size, size );
    20.  
    21. glVertexPointer( NUM_COORDS_PER_VERTEX, GL_FLOAT, sizeof( QPoint3F ), ( void* ) ( sizeof( QPoint3F ) * 24 ) );
    22. glNormalPointer( GL_FLOAT, sizeof( QPoint3F ), 0 );
    23.  
    24. glEnableClientState( GL_VERTEX_ARRAY );
    25. glEnableClientState( GL_NORMAL_ARRAY );
    26.  
    27. glDrawArrays( GL_QUADS, FIRST_INDEX_TO_DRAW, NUM_ARR_TO_DRAW);
    28.  
    29. glDisableClientState( GL_VERTEX_ARRAY );
    30. glDisableClientState( GL_NORMAL_ARRAY );
    31.  
    32. if ( pushAndPop )
    33. glPopMatrix();
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    And...that's that. Please, if anyone has any idea what the reason for this is, I'd appreciate an answer. Thanks.
    Last edited by litedrive; 21st January 2012 at 03:18.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Widget Bug.

    Quote Originally Posted by litedrive View Post
    Should I just call it in main after w.show()?
    You shouldn't need to call it at all. How are you calling it from an external widget, by the way, as it is a protected method?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 8
    Last Post: 28th June 2011, 14:57
  2. Replies: 1
    Last Post: 23rd June 2011, 23:09
  3. Replies: 10
    Last Post: 29th May 2010, 18:42
  4. Replies: 7
    Last Post: 14th January 2010, 08:47
  5. Replies: 4
    Last Post: 3rd March 2008, 22:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.