Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Issues with QGLWidget as QMainWindow central widget

  1. #1
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Issues with QGLWidget as QMainWindow central widget

    Hello,

    I am setting a QGLWidget as QMainWindow central widget.
    I want to use it to render simple 2D GUI elements quickly.
    I set it up this way:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QGLWidget* client = new MyQGLWidget(this);
    5. this->setCentralWidget(client);
    6. }
    To copy to clipboard, switch view to plain text mode 

    It does work, but the resulting Window has horrible resizing performance.
    The content won't redraw at all unless I hold the mouse cursor still for ~100ms.

    MyQGLWidget:
    Qt Code:
    1. #ifndef MYQGLWIDGET_H
    2. #define MYQGLWIDGET_H
    3.  
    4. #include <QGLWidget>
    5. #include <QMouseEvent>
    6. #include <QDebug>
    7. #include <QPaintEvent>
    8. #include <QSize>
    9.  
    10. class MyQGLWidget : public QGLWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit MyQGLWidget(QWidget *parent = NULL);
    15.  
    16. QSizePolicy sizeHint() {
    17. return QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    18. }
    19.  
    20. QSize minimumSizeHint() {
    21. return QSize(800, 600);
    22. }
    23.  
    24. QSize maximumSize() {
    25. return QSize(999999, 999999);
    26. }
    27.  
    28. int minimumWidth() {
    29. return 800;
    30. }
    31.  
    32. int minimumHeight() {
    33. return 600;
    34. }
    35.  
    36. int maximumWidth() {
    37. return 999999;
    38. }
    39.  
    40. int maximumHeight() {
    41. return 999999;
    42. }
    43.  
    44. protected:
    45. int count;
    46.  
    47. void initializeGL() {
    48. glClearColor(0, 1, 0, 1);
    49. glPushMatrix();
    50. glOrtho(0, size().width(), size().height(), 0, -1, 1);
    51. glViewport(0, 0, (GLint)size().width(), (GLint)size().height());
    52. glDisable(GL_DEPTH_TEST);
    53. glShadeModel(GL_FLAT);
    54. }
    55.  
    56. void resizeGL(int w, int h)
    57. {
    58. glPopMatrix();
    59. glPushMatrix();
    60. glOrtho(0, w, h, 0, -1, 1);
    61. glViewport(0, 0, w, h);
    62. }
    63.  
    64. void paintGL() {
    65. qDebug() << "paintGL() " << count;
    66. count++;
    67. // Draw some squares
    68. int r = ((int) (rand())) % 255;
    69. for (int i = 0; i < 10; i++) {
    70. for (int j = 0; j < 10; j++) {
    71. glColor3f((((i * 51 + 3 + r) * (j * 127 + 17 + r)) % 173) / 173.0f,
    72. (((i * 73 + 19 + r) * (j * 1337 + 5 + r)) % 143) / 143.0f,
    73. (((i * 97 + 13 + r) * (j * 27 + 77 + r)) % 931) / 931.0f);
    74. glBegin(GL_QUADS);
    75. glVertex2i(10 + (20 * i), 10 + (20 * j));
    76. glVertex2i(20 + (20 * i), 10 + (20 * j));
    77. glVertex2i(20 + (20 * i), 20 + (20 * j));
    78. glVertex2i(10 + (20 * i), 20 + (20 * j));
    79. glEnd();
    80. }
    81. }
    82. }
    83.  
    84. signals:
    85.  
    86. public slots:
    87.  
    88. };
    89.  
    90. #endif // MYQGLWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    It does rerender properly and instantly if I do not set it as the central window and just setGeometry to large width and height,
    but it won't resize according to the QMainWindow then. Is it possible to address this issue? Using Qt 4.7.4.

    Thanks in advance and best regards,
    Zyl

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    I'm guessing that using QSizePolicy::Ignored is the reason resizing is not as desired?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    Here is the whole project in its rawest form: Link
    There may be meaningless leftover code from previous tryhardness.

  4. #4
    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: Issues with QGLWidget as QMainWindow central widget

    This code doesn't make sense:
    Qt Code:
    1. QSizePolicy sizeHint() {
    2. return QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    3. }
    To copy to clipboard, switch view to plain text mode 

    I don't know what you are trying to obtain with this, but sizeHint() returns QSize and not QSizePolicy and it's a const method too.

    See if it helps if you call update() at the end of resizeGL().
    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.


  5. #5
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    Quote Originally Posted by wysota View Post
    I don't know what you are trying to obtain with this, but sizeHint() returns QSize and not QSizePolicy and it's a const method too.
    Oops! Thanks. That explains a few things. I changed the public methods to this:
    Qt Code:
    1. public:
    2. explicit ShardIRCClient(QWidget *parent = NULL);
    3.  
    4. QSize sizeHint() const {
    5. return QSize(800, 600);
    6. }
    7.  
    8. QSize minimumSizeHint() const {
    9. return QSize(640, 480);
    10. }
    To copy to clipboard, switch view to plain text mode 
    The window can now not be made smaller than a size to allow a 640*480 widget to fit, so that works properly. (QSizeHint::Preferred)


    Quote Originally Posted by wysota View Post
    See if it helps if you call update() at the end of resizeGL().
    Assuming you meant updateGL(): Yes and no. The widget now redraws when resizing it larger. It won't react if resized smaller and its overall performance is still oddly poor.

  6. #6
    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: Issues with QGLWidget as QMainWindow central widget

    Quote Originally Posted by Zyl View Post
    Assuming you meant updateGL(): Yes and no.
    update() calls updateGL() so, no, I meant update()

    The widget now redraws when resizing it larger. It won't react if resized smaller and its overall performance is still oddly poor.
    Please provide a minimal compilable example reproducing the problem.
    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.


  7. #7
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    This is the most minimal example I could make: Link
    Hope that shows what I mean.

  8. #8
    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: Issues with QGLWidget as QMainWindow central widget

    I tested your app on Linux and I can't see any unexpected behaviour. When I resize the window (up or down), paintGL gets called.

    As for your note regarding updateGL()... Here is what paintEvent for QGLWidget looks like (which is what gets called upon calling update()):
    Qt Code:
    1. void QGLWidget::paintEvent(QPaintEvent *)
    2. {
    3. if (updatesEnabled()) {
    4. glDraw();
    5. updateOverlayGL();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    and this is updateGL():

    Qt Code:
    1. void QGLWidget::updateGL()
    2. {
    3. if (updatesEnabled())
    4. glDraw();
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.


  9. #9
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    Strange. Using update() I get not additional paintGL()-calls, while with using updateGL() I do.
    Also I already do get automatic calls to paintGL() when resizing the window, without needing to set a call to update() nor updateGL(). However, although paintGL() is definitely called, there is no change visible unless I stop moving the mouse. Only an additional updateGL() call in resizeGL() causes a visible change, BUT ONLY when resizing the window larger. It really feels as if something's running on the wrong thread, and I don't see where I'd ever have to do with that. (Win7 x64 btw)

  10. #10
    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: Issues with QGLWidget as QMainWindow central widget

    Unless you are using threads yourself, there is no threading involved. What you observe might be somehow caused by your system settings.
    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.


  11. #11
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    I know of no system setting regarding the rendering mechanism of OpenGL in a window.

    If I remove this line from MainWindow::MainWindow
    Qt Code:
    1. this->setCentralWidget(widget);
    To copy to clipboard, switch view to plain text mode 
    the problem no longer occurs. However, no layouting is performed and the widget keeps its initial size.

    Doing layouting results in the whole problem returning however:
    Qt Code:
    1. #include <QHBoxLayout>
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. QWidget* center = new QWidget(this);
    7. QGLWidget* widget = new MyGLWidget(center);
    8. QHBoxLayout* layout = new QHBoxLayout(center);
    9. layout->setMargin(0);
    10. center->setLayout(layout);
    11. center->layout()->addWidget(widget);
    12. this->setCentralWidget(center);
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zyl; 12th June 2012 at 00:39.

  12. #12
    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: Issues with QGLWidget as QMainWindow central widget

    I didn't say the system setting has to be related to rendering of OpenGL in a window. You might have some system tweak installed that causes such behaviour. If what you observe was a common problem, it would have been identified and resolved a long time ago since it's quite common to use OpenGL child windows. Does it change anything if you call center->winId() (you can ignore the result) as part of your last code snippet?
    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.


  13. #13
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    I don't think I have any outrageous system tweaks installed. The hackiest thing on this machine is probably Dropbox with its random folder scans.
    I tried adding center->winId() at various lines (and even multiple times), but there was no change.

    It might be worth working with the hint that just doing
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QGLWidget* widget = new MyGLWidget(this);
    5. widget->setGeometry(0, 0, 800, 600);
    6. }
    To copy to clipboard, switch view to plain text mode 
    presents me the widget with proper performance. I can scale the window larger and as long as new parts of the 800*600 rectangle area of the widget are revealed paintGL() gets called and I see the redrawn content immediately, at what appears to be 60Hz - my screen's refresh rate; exactly what I want and one should expect. I can't help but think the problem to lie somewhere in the Qt layout classes.

  14. #14
    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: Issues with QGLWidget as QMainWindow central widget

    Quote Originally Posted by Zyl View Post
    I don't think I have any outrageous system tweaks installed. The hackiest thing on this machine is probably Dropbox with its random folder scans.
    You'd be suprised how things tend to influence stuff logically unrelated to what the things are meant to do, especially when it comes to Windows.

    I tried adding center->winId() at various lines (and even multiple times), but there was no change.
    It was only needed once and in theory it shouldn't be needed at all

    I can't help but think the problem to lie somewhere in the Qt layout classes.
    Unlikely, especially since I've been using Qt+OpenGL+layouts and didn't experience any slowdowns.

    As an experiment, try this:

    Qt Code:
    1. class Widget : public QWidget {
    2. public:
    3. Widget(QWidget *parent = 0) : QWidget(parent) {
    4. w = new MyGLWidget(this);
    5. }
    6. protected:
    7. void resizeEvent(QResizeEvent *re) {
    8. w->setGeometry(0,0, width(), height());
    9. }
    10. MyGLWidget *w;
    11. };
    12.  
    13. int main(...) {
    14. ...
    15. Widget w;
    16. w.show();
    17. ...
    18. }
    To copy to clipboard, switch view to plain text mode 

    No layouts, same GL widget code. Is there a slowdown? If yes, what if you replace MyGLWidget with QGLWidget?
    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.


  15. #15
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    Same slow behaviour with MyGLWidget as well as with QGLWidget.
    If this goes on I'll try making a QThread spamming sizeInfo signals about the window size and catch them in a MyGLWidget slot... >_>
    EDIT: That actually works, but it acts equally crappy. (Yes I did just try it)
    Last edited by Zyl; 11th June 2012 at 23:35.

  16. #16
    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: Issues with QGLWidget as QMainWindow central widget

    Quote Originally Posted by Zyl View Post
    If this goes on I'll try making a QThread spamming sizeInfo signals about the window size and catch them in a MyGLWidget slot...
    I don't see how this would help anything.

    I'm still suspecting the problem is related to your system configuration. Try your program on a different machine, with a different graphics driver. It would be best if it was a vanilla installation of Windows.
    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.


  17. #17
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    UGH! Upon you insisting it was a system problem, I tried something... and found the cause, at least.

    I'm on a laptop with an "NVidia mobile GPU-equipped" graphics card GeForce GT540M and "NVidia Optimus" technology, which basically refers to some wicked mechanism which allows switching between GPU and Intel HD graphics (which is in this thing as well, apparantly). So I went to the NVidia Control Panel and set "Preferred graphics processor" from "High-performance NVIDIA processor" to "Integrated graphics", finding that this solves the original problem entirely. (Which is weird, because Integrated graphics are supposed to suck balls as opposed to GPU)

    However, I still do get other strange effects, which include flickering while resizing (I was able to get a screen capture of it: Link) and broad white stripes when resizing the window larger quickly, as if the widget wouldn't resize before redrawing. (Interestingly it appears to be impossible to get a screen capture of the later effect) I guess this is just one major NVidia (or Intel) (or Microsoft) (or all three together) fuckup. I'm not sure where the catastrophe happens, but OGL never caused bad problems on this machine.

    Thanks for your help on this. If there is additional wisdom to share, please do.

  18. #18
    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: Issues with QGLWidget as QMainWindow central widget

    Intel HD OpenGL drivers for Windows are totally broken. You should focus on getting your NVidia drivers to work properly. I'm sure the code of the program itself is correct, it works fine on my Linux+NVidia combo.

    01:00.0 VGA compatible controller: NVIDIA Corporation GF104 [GeForce GTX 460] (rev a1)
    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.


  19. The following user says thank you to wysota for this useful post:

    Zyl (13th June 2012)

  20. #19
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Issues with QGLWidget as QMainWindow central widget

    If I may interject . . .

    @Zyl - I see similar artifacts using your code above on my Debian box. Try calling glClear() in your
    paintGL() function:
    Qt Code:
    1. void paintGL() {
    2. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3. . . .
    To copy to clipboard, switch view to plain text mode 

  21. The following user says thank you to norobro for this useful post:

    Zyl (13th June 2012)

  22. #20
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issues with QGLWidget as QMainWindow central widget

    Quote Originally Posted by wysota View Post
    Intel HD OpenGL drivers for Windows are totally broken. You should focus on getting your NVidia drivers to work properly. I'm sure the code of the program itself is correct, it works fine on my Linux+NVidia combo.

    01:00.0 VGA compatible controller: NVIDIA Corporation GF104 [GeForce GTX 460] (rev a1)
    I'd be thankful if you have any instructions/hints regarding this. I remember trying to install the NVidia driver before any Intel HD driver resulting in some message like "You must install Intel HD first [...]" or something along those lines. I also remember uninstalling Intel HD leaving me with a black screen once, requiring me to reinstall Windows - so I'm paranoid about ever touching it again. I still need to test if there are painting problems when doing updateGL() outside the content of a resize event. If yes, I won't be using OGL for anything, because there are too many people with systems like mine. If no, crappy resizing performance is an endurable pain.


    Quote Originally Posted by norobro View Post
    If I may interject . . .

    @Zyl - I see similar artifacts using your code above on my Debian box. Try calling glClear() in your
    paintGL() function:
    Qt Code:
    1. void paintGL() {
    2. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3. . . .
    To copy to clipboard, switch view to plain text mode 
    Good idea. glClear does the job. Actually... If I don't do glClear, everything still gets cleared with black. At least when I move my squares like so:
    Qt Code:
    1. glBegin(GL_QUADS);
    2. glVertex2i(10 + (20 * i), 10 + (20 * j) + 3 * (countPaint % 150));
    3. glVertex2i(20 + (20 * i), 10 + (20 * j) + 3 * (countPaint % 150));
    4. glVertex2i(20 + (20 * i), 20 + (20 * j) + 3 * (countPaint % 150));
    5. glVertex2i(10 + (20 * i), 20 + (20 * j) + 3 * (countPaint % 150));
    6. glEnd();
    To copy to clipboard, switch view to plain text mode 
    ; the previously drawn ones won't stay visible in the next frame. I'm no OGL expert, but isn't this supposed to not happen unless there actually is a glClear()-call?


    Another thing I noticed: I seem to be able to avoid unneccesary drawing by doing this:

    Qt Code:
    1. void initializeGL() {
    2. // gl-calls here
    3.  
    4. resized = true;
    5. }
    6.  
    7. void resizeGL(int w, int h) {
    8. // gl-calls here
    9.  
    10. resized = true;
    11. }
    12.  
    13. void paintGL() {
    14. if (!resized)
    15. return;
    16.  
    17. // gl-calls to draw here...
    18.  
    19. resized = false;
    20. }
    21.  
    22. private:
    23. bool resized;
    To copy to clipboard, switch view to plain text mode 
    ; this seemingly effectively increases performance and responsiveness.
    EDIT: Doing this running the program using NVidia GPU however causes a black window. The squares would only flash up while resizing it.
    Last edited by Zyl; 12th June 2012 at 15:23.

Similar Threads

  1. How can size of QMainWindow's central widget be managed?
    By yogeshm02 in forum Qt Programming
    Replies: 9
    Last Post: 28th March 2014, 21:12
  2. Replies: 2
    Last Post: 29th June 2011, 15:45
  3. Replies: 2
    Last Post: 7th June 2008, 13:12
  4. paint central widget of a QMainWindow???
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 08:02
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32

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.