Results 1 to 3 of 3

Thread: QGLWidget renders black

  1. #1
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGLWidget renders black

    I'm fairly new to Qt. I'm trying to make a custom VJ application. For my video-output I want to use OpenGL, which I control by another mainwindow. The problem is, It just renders black. I copypasted what the NeHe tutorial says for drawing a simple white triangle and rectangle, but it's still black. I don't get what's wrong with it. Is there anything I missed?
    this is my glscreen.h
    Qt Code:
    1. #ifndef GLSCREEN_H
    2. #define GLSCREEN_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGLWidget>
    6.  
    7. class VJOutput : public QGLWidget{
    8. Q_OBJECT
    9. public:
    10. VJOutput();
    11. protected:
    12. void initializeGL();
    13. void paintGL();
    14. void resizeGL(int w, int h);
    15. public slots:
    16. void Herteken();
    17.  
    18. };
    19.  
    20.  
    21. #endif // GLSCREEN_H
    To copy to clipboard, switch view to plain text mode 

    here's the glscreen.cpp code

    Qt Code:
    1. #include "glscreen.h"
    2. #include "ui_glscreen.h"
    3. #include <iostream>
    4. #include <QTimer>
    5. #include <math.h>
    6.  
    7. void VJOutput::initializeGL(){
    8. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    9. glLoadIdentity(); // Reset The View
    10. }
    11. VJOutput::VJOutput(){
    12. initializeGL();
    13. QTimer *RedrawTimer = new QTimer(this);
    14. connect(RedrawTimer,SIGNAL(timeout()),this,SLOT(Herteken()));
    15. RedrawTimer->start(20);
    16.  
    17. }
    18. void VJOutput::resizeGL(int w, int h){
    19.  
    20. }
    21.  
    22. void VJOutput::paintGL(){
    23. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    24. glLoadIdentity();
    25.  
    26. glTranslatef(-1.5f,0.0f,-6.0f);
    27.  
    28. glBegin(GL_TRIANGLES);
    29. glVertex3f( 0.0f, 1.0f, 0.0f);
    30. glVertex3f(-1.0f,-1.0f, 0.0f);
    31. glVertex3f( 1.0f,-1.0f, 0.0f);
    32. glEnd();
    33.  
    34. glTranslatef(3.0f,0.0f,0.0f);
    35.  
    36. glBegin(GL_QUADS);
    37. glVertex3f(-1.0f, 1.0f, 0.0f);
    38. glVertex3f( 1.0f, 1.0f, 0.0f);
    39. glVertex3f( 1.0f,-1.0f, 0.0f);
    40. glVertex3f(-1.0f,-1.0f, 0.0f);
    41. glEnd();
    42. }
    43. void VJOutput::Herteken(){
    44. updateGL();
    45. }
    To copy to clipboard, switch view to plain text mode 
    my VJ.pro file has:
    Qt Code:
    1. # -------------------------------------------------
    2. # Project created by QtCreator 2010-01-15T13:07:06
    3. # -------------------------------------------------
    4. QT += network \
    5. opengl \
    6. sql \
    7. xml \
    8. xmlpatterns \
    9. phonon \
    10. dbus
    11. CONFIG += thread
    12. TARGET = VJ
    13. TEMPLATE = app
    14. SOURCES += main.cpp \
    15. mainwindow.cpp \
    16. lib/Rhythm.cpp \
    17. lib/MidiDevices.cpp \
    18. lib/RtMidi.cpp \
    19. visualLib/glscreen.cpp
    20. HEADERS += mainwindow.h \
    21. lib/Rhythm.h \
    22. lib/MidiDevices.h \
    23. lib/RtMidi.h \
    24. lib/RtError.h \
    25. visualLib/glscreen.h
    26. LIBS += -L/alsa/ \
    27. -lasound \
    28. -lpthread
    29. FORMS += mainwindow.ui
    To copy to clipboard, switch view to plain text mode 

    I'm using ubuntu and Qt Creator

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

    Default Re: QGLWidget renders black

    Are you sure you shouldn't put anything inside resizeGL() or one of other GL methods to setup the projection (or some other, I'm not a GL expert) matrix?
    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.


  3. #3
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGLWidget renders black

    you're right.
    With the addition of this code it works:
    Qt Code:
    1. void VJOutput::resizeGL(int w, int h){
    2. if (h==0) // Prevent A Divide By Zero By
    3. {
    4. h=1; // Making Height Equal One
    5. }
    6.  
    7. glViewport(0, 0, w, h); // Reset The Current Viewport
    8. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    9. glLoadIdentity(); // Reset The Projection Matrix
    10.  
    11. // Calculate The Aspect Ratio Of The Window
    12. gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
    13.  
    14. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    15. glLoadIdentity(); // Reset The Modelview Matrix
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

Similar Threads

  1. Displaying Text on Black and White Screen
    By Stobie in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 8th December 2009, 01:03
  2. Printing plot black & white
    By maybe78 in forum Qwt
    Replies: 4
    Last Post: 9th October 2009, 07:39
  3. QPainter::drawPixmap always produces black on windows?
    By spbots in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2009, 16:42
  4. Widget become black in Release mode
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 21st October 2008, 08:07
  5. Drag and drop and black background
    By zerobala in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2008, 20:34

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.