Results 1 to 13 of 13

Thread: QGLWidget doesn't update

  1. #1
    Join Date
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QGLWidget doesn't update

    Hi,

    I modified the example project 2dpainting coming with QT. If I send the timeOut signal directly to the slot animate() in GLWidget class, everything works fine. However, if I send the timeOut signal to another slot calc() in main window, do something in calc(), then emit a new signal newData() to animate(), the program got crashed.

    Could you please take a look at the code? I attached it to this post.

    Thanks in advance for the help.

    QtBNL
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    Your GLWidget::animate() function assumes that sender() is a QTimer. You might consider changing that.

    On a side note, this was fairly easy to figure out using a debugger. QtCreator can use one as well.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: QGLWidget doesn't update

    Franz,

    Thanks for your reply. I am kind of newBie to Qt and appreciate your help if you explain more for me.

    Basically, there are three "connect" statements in the constructor of Window class. If I use the first one only (that's the case of emitting timeOut signal directly to animate), everything works.

    If I comment out the first statements and use the next two statements, I am emitting timeOut to calc, and from there emitting newData to animate. In this case, the program crashes.

    Could you please tell me what's wrong?

    Thanks,
    QtBNL

  4. #4
    Join Date
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    Dear All,
    I am still waiting for somebody to help me with this issue. As a newBie to Qt, I and don't know too much about the internal of Qt and completely lost my way at this point.

    I appreciate your help in advance.

    QtBNL

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

    Default Re: QGLWidget doesn't update

    What's wrong with the reply you were given?
    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.


  6. #6
    Join Date
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    wysota,

    Franz's first reply didn't solve the problem I have. Franz said "Your GLWidget::animate() function assumes that sender() is a QTimer". Actually that's what I need if I want to send timeOut signal directly to animate. For the other case that I want timeOut signal to trigger a data processing function, e.g. calc() and then emit newData signal to animate after the data processing is done, I have to comment out that statement and enable the other two as shown below. In this case, my program crashes.

    // connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
    connect(timer, SIGNAL(timeout()), this, SLOT(calc()));
    connect(this, SIGNAL(newData()), openGL, SLOT(animate()));

    For some reasons, my attachment is corrupted. I am attaching it again.

    Thanks,
    QtBNL
    Attached Files Attached Files

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

    Default Re: QGLWidget doesn't update

    I don't see your point. The answer you were given is 100% precise. If you reemit the signal from a different class than QTimer then qobject_cast will return 0 and your modified application will crash.
    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.


  8. #8
    Join Date
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    wysota,

    Let's see the following code

    case 1.

    connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
    //connect(timer, SIGNAL(timeout()), this, SLOT(calc()));
    //connect(this, SIGNAL(newData()), openGL, SLOT(animate()));

    case 2.

    // connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
    connect(timer, SIGNAL(timeout()), this, SLOT(calc()));
    connect(this, SIGNAL(newData()), openGL, SLOT(animate()));

    with calc() as

    void Window::calc()
    {
    {
    do somthing here for data processing;
    }

    emit newData();//data);
    }


    What's the difference between 1 and 2? Why does 2 cause crash?

    Thanks,
    QtBNL

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGLWidget doesn't update

    I guess more people will be able to help if you post your code for 'animate' and 'calc' methods.
    Previous answer from wysota explains everything. Look, if you do this:
    Qt Code:
    1. void Window::calc()
    2. {
    3. //...
    4. emit newData();
    5. }
    To copy to clipboard, switch view to plain text mode 
    then "sender()" in 'animate()' is not a QTimer anymore, its now an instance of Window, so qobject_cast<QTimer*>(sender()) will return 0.

  10. #10
    Join Date
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    I am attached my code.

    window.cpp
    Qt Code:
    1. Window::Window()
    2. : QWidget()
    3. {
    4. GLWidget *openGL = new GLWidget(&helper, this);
    5.  
    6. QTimer *timer = new QTimer(this);
    7. // connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
    8. connect(timer, SIGNAL(timeout()), this, SLOT(calc()));
    9. connect(this, SIGNAL(newData()), openGL, SLOT(animate()));
    10. timer->start(250);
    11.  
    12. ellipse = 0.0;
    13.  
    14. setWindowTitle(tr("2D Painting on Native and OpenGL Widgets"));
    15. }
    16. //! [0]
    17.  
    18. void Window::calc()
    19. {
    20. // ...
    21. emit newData();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. GLWidget::GLWidget(Helper *helper, QWidget *parent)
    2. : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
    3. {
    4. elapsed = 0;
    5. setFixedSize(600, 600);
    6. setAutoFillBackground(false);
    7. }
    8.  
    9. void GLWidget::animate()
    10. {
    11. elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
    12. repaint();
    13. }
    14.  
    15. void GLWidget::paintEvent(QPaintEvent *event)
    16. {
    17. QImage image(100, 100, QImage::Format_RGB16);
    18. for (int i = 0; i < 100; i++) {
    19. for (int j = 0; j < 100; j++) {
    20. image.setPixel(i,j,rand());//data[i][j]);
    21. // qDebug() << image.pixel(i,j);
    22. }
    23. }
    24. QPainter painter;
    25. painter.begin(this);
    26. painter.setRenderHint(QPainter::Antialiasing);
    27. painter.setViewport(0,0,600,600);
    28. painter.scale(6,6);
    29. painter.fillRect(event->rect(), QBrush(QColor(64, 32, 64)));
    30. painter.drawImage(0,0,image);
    31. painter.end();
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for the help.
    QtBNL
    Last edited by qtbnl; 11th February 2011 at 19:41. Reason: spelling corrections

  11. #11
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    Has said at least twice, in void GLWidget::animate() qobject_cast<QTimer*>(sender()) will probably return NULL pointer

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

    Default Re: QGLWidget doesn't update

    I'm in a good mood, let's analyze the code together.

    I'm skipping irrelevant parts of the code.

    Qt Code:
    1. Window::Window()
    2. : QWidget()
    3. {
    4. GLWidget *openGL = new GLWidget(&helper, this);
    5.  
    6. QTimer *timer = new QTimer(this);
    7. connect(timer, SIGNAL(timeout()), this, SLOT(calc()));
    8. connect(this, SIGNAL(newData()), openGL, SLOT(animate()));
    9. timer->start(250);
    10. }
    To copy to clipboard, switch view to plain text mode 

    The above code makes two connections which are quite obvious so let's not explain them.

    Qt Code:
    1. void Window::calc()
    2. {
    3. // ...
    4. emit newData();//data);
    5. }
    To copy to clipboard, switch view to plain text mode 

    The calc() routine emits "newData()" signal on behalf of an instance of a Window class.

    Qt Code:
    1. void GLWidget::animate()
    2. {
    3. elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
    4. repaint();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now the hard part, let's go step by step. The animate() routine says

    Qt Code:
    1. qobject_cast<QTimer*>(sender())
    To copy to clipboard, switch view to plain text mode 
    "if sender() is a QTimer() then return a pointer to it cast to QTimer, otherwise return 0".

    Qt Code:
    1. elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
    To copy to clipboard, switch view to plain text mode 
    "Do some math asking for QTimer::interval() as part of the calculation".

    Since sender() is Window and not QTimer, qobject_cast returns 0 which effectively transforms the last expression to:
    Qt Code:
    1. elapsed = (elapsed + null->interval()) % 1000;
    To copy to clipboard, switch view to plain text mode 
    Since null is not QTimer, then asking for its interval() member fails and causes a crash.

    Do you understand what the problem is?
    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
    Jan 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget doesn't update

    Wysota,

    I appreciate your help with such detailed explanation. You're a nice person.

    QtBNL

Similar Threads

  1. QComboBox doesn't update when on hidden widget
    By martinb0820 in forum Qt Programming
    Replies: 1
    Last Post: 10th December 2010, 22:09
  2. Replies: 9
    Last Post: 3rd December 2010, 12:30
  3. Replies: 2
    Last Post: 29th September 2010, 17:44
  4. Replies: 1
    Last Post: 3rd October 2009, 04:20
  5. QGLWidget doesn't paint when I add another data member
    By Thoughtjacked in forum Qt Programming
    Replies: 6
    Last Post: 17th October 2007, 15:45

Tags for this Thread

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.