Results 1 to 6 of 6

Thread: Newbie-Question: update does not trigger paintEvent

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Newbie-Question: update does not trigger paintEvent

    Hello there,

    I'm new to Qt and just working my way through the tutorials and docs. But now I am stuck on a seemingly simple problem. I hope that you can help me out.

    So I was trying to hack one of Qt's tutorials a bit: http://doc.trolltech.com/4.3/tutorial-t10.html
    I created a custom widget like this
    Qt Code:
    1. #include "forceindicator.h"
    2.  
    3. CannonField::CannonField(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. /* stuff */
    7.  
    8. indicator = new ForceIndicator;
    9. overlayLayout = new QGridLayout(this);
    10. overlayLayout->addWidget(indicator,0,1,Qt::AlignRight | Qt::AlignTop);
    11. this->setLayout(overlayLayout);
    12. /* more stuff */
    13. }
    To copy to clipboard, switch view to plain text mode 

    Now this child widget "indicator" has a slot "setForce" which is triggered by some events. Within this slot (see below) update() is called.
    I was thinking that this in turn would trigger a paint event and call my paintEvent(QPaintEvent *) method. However this is not working. The slot is called alright and everything before and after my update() calls is executed (checked with some couts), but the paintEvent (again couts) is not called.

    I tried repaint instead - same result. Triggering the paintEvent explicitely does call my custom function.

    So, what stupid mistake am I missing out on?

    Thanks for your help!

    Here is the header of my custom widget
    Qt Code:
    1. #ifndef FORCEINDICATOR_H
    2. #define FORCEINDICATOR_H
    3.  
    4. #include <QWidget>
    5.  
    6. class ForceIndicator : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ForceIndicator(QWidget *parent = 0);
    12.  
    13. int force() const {return currentForce;}
    14.  
    15. public slots:
    16. void setForce(int force);
    17.  
    18. signals:
    19. void forceChanged(int newForce);
    20.  
    21. protected:
    22. void paintEvent(QPaintEvent *event);
    23.  
    24. private:
    25. int currentForce;
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    And here are parts of the body
    #
    Qt Code:
    1. include <QPainter>
    2. #include <iostream>
    3. #include "forceindicator.h"
    4.  
    5. void ForceIndicator::setForce(int force)
    6. {
    7. if (currentForce == force)
    8. return;
    9. std::cout << "bla" << std::endl;
    10. currentForce = force;
    11.  
    12. update();
    13. std::cout << "blabla" << std::endl;
    14. //emit forceChanged(currentForce);
    15. }
    16.  
    17.  
    18. void ForceIndicator::paintEvent(QPaintEvent* /*event*/)
    19. {
    20. std::cout << "blub" << std::endl;
    21. QPainter painter(this);
    22.  
    23. /* stuff */
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Newbie-Question: update does not trigger paintEvent

    as said in tutorial... update() will not call the paintEvent() everytime.
    but repaint() should call it.

    i have not used cout<< in any of my qt programs. try this...

    Qt Code:
    1. #include<QDebug>
    2. #include<QTime>
    3.  
    4. void ForceIndicator::paintEvent(QPaintEvent* /*event*/)
    5. {
    6. //std::cout << "blub" << std::endl;
    7. qWarning()<<"Current Time ="<<QTime::currentTime();
    8.  
    9. QPainter painter(this);
    10.  
    11. //paint the current time somewhere it is visible in your widget.
    12. painter.drawText(50,50,QTime::currentTime().toString());
    13.  
    14. /* stuff */
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Newbie-Question: update does not trigger paintEvent

    Quote Originally Posted by MrDeath View Post
    as said in tutorial... update() will not call the paintEvent() everytime.
    but repaint() should call it.
    Well, as I wrote - I tried using repaint() and it doesn't change anything.
    Also using qWarning() instead of cout doesn't help, the paintEvent simply isn't called.

    Could it be something in the way I create the widget? It's a child of this CannonField widget (see original post)
    Qt Code:
    1. indicator = new ForceIndicator(this);
    To copy to clipboard, switch view to plain text mode 
    and then added to a layout (see below). I also omitted passing this as a parent, but that doesn't affect the behavior.
    The constructor right now by the way is just:
    Qt Code:
    1. ForceIndicator::ForceIndicator(QWidget* parent)
    2. : QWidget(parent)
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help!
    Last edited by janitor; 3rd June 2009 at 18:05. Reason: updated contents

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

    Default Re: Newbie-Question: update does not trigger paintEvent

    Could you prepare 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.


  5. #5
    Join Date
    Jun 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Newbie-Question: update does not trigger paintEvent

    Thanks a lot for your replies!

    While boiling it down to a minimal example I actually found my mistake. I never assigned my custom child widget a geometry or a minimum size or the like. So apparently it just had size zero and that's why the paintEvent was never even called.

  6. #6
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: Newbie-Question: update does not trigger paintEvent

    Post deleted wrong section...sorry

Similar Threads

  1. Y-Axis Scale Spacing - Newbie Question
    By Rick108 in forum Qwt
    Replies: 3
    Last Post: 13th October 2009, 08:20
  2. Newbie question regarding multi platform development
    By bronkopavel in forum Installation and Deployment
    Replies: 2
    Last Post: 15th September 2008, 10:08
  3. Newbie question about special characters in QLabel
    By WinchellChung in forum Newbie
    Replies: 4
    Last Post: 3rd April 2008, 21:39
  4. Paintevent and update
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2008, 07:09
  5. Replies: 1
    Last Post: 15th March 2007, 20:45

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.