Results 1 to 4 of 4

Thread: Custom widget: paintEvent, can't change member variable

  1. #1
    Join Date
    Feb 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question Custom widget: paintEvent, can't change member variable

    Hello,

    I've created a custom widget. My goal was to simply draw a rectangle filled with color. I want the color to be changeable via setColor method, which is called from a slot of parent window object.

    The problem is, that whenever paintEvent is called, the private member color is -- somehow -- initialized back to the value provided in constructor (Qt::white by default). That happens even though setColor method with a new color was called before the paintEvent. As a consequence, the widget is always painted in the same color that was provided in constructor.

    I have also found a very similar unanswered question here:
    http://stackoverflow.com/questions/1...mber-variables

    What is causing the problem? What should I do in order to enable the color switching?


    ColorPreviewWidget.h:

    Qt Code:
    1. #include <QWidget>
    2. #include <QColor>
    3.  
    4. class ColorPreviewWidget : public QWidget
    5. {
    6. public:
    7.  
    8. ColorPreviewWidget
    9. (
    10. const QColor & preview_color = Qt::white,
    11. QWidget *parent = 0
    12. );
    13.  
    14. void setColor(const QColor& new_color);
    15.  
    16. protected:
    17.  
    18. void paintEvent(QPaintEvent* event);
    19.  
    20. private:
    21.  
    22. QColor color;
    23. };
    To copy to clipboard, switch view to plain text mode 

    ColorPreviewWidget.cpp:

    Qt Code:
    1. #include "ColorPreviewWidget.h"
    2. #include <QPainter>
    3. #include <QDebug>
    4.  
    5. ColorPreviewWidget::ColorPreviewWidget
    6. (
    7. const QColor & new_color,
    8. QWidget *parent
    9. )
    10. :
    11. QWidget(parent),
    12. color(new_color)
    13. {}
    14.  
    15. void ColorPreviewWidget::setColor(const QColor & new_color)
    16. {
    17. color = new_color;
    18. qDebug() << "setColor : color = " << color;
    19. // This debug message shows the right color (= new_color)
    20. }
    21.  
    22. void ColorPreviewWidget::paintEvent(QPaintEvent* e)
    23. {
    24. qDebug() << "paintEvent : color = " << color;
    25. // This debug message shows the wrong, default color, over
    26. // and over again!
    27. QPainter painter(this);
    28. painter.fillRect(0, 0, size().width(), size().height(), color);
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom widget: paintEvent, can't change member variable

    There is nothing wrong that I can see with the code you have posted. What I guess is probably wrong is in the way you are using this class. Are you sure that you are setting the new color on the SAME instance that is being used in the painting? Did you maybe accidentally create a local variable with the same name as your widget's instance that is hiding the one you think is being painted?

  3. The following user says thank you to d_stranz for this useful post:

    jforejtn (24th February 2015)

  4. #3
    Join Date
    Feb 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget: paintEvent, can't change member variable

    Further desperate investigation revealed following:

    When i print &color (i.e. address of the private member color) inside different functions, I get two different results:

    Both ColorPreviewWidget::ColorPreviewWidget and ColorPreviewWidget:aintEvent give

    &color = 0x90753e4d60

    while ColorPreviewWidget::setColor gives

    &color = 0x907301b4a0


    Added after 20 minutes:


    Quote Originally Posted by d_stranz View Post
    There is nothing wrong that I can see with the code you have posted. What I guess is probably wrong is in the way you are using this class. Are you sure that you are setting the new color on the SAME instance that is being used in the painting? Did you maybe accidentally create a local variable with the same name as your widget's instance that is hiding the one you think is being painted?
    You guess right. I've been working with two different instances of the custom widget, painting one and changing color to another. Thanks for your answer. Problem solved.
    Last edited by jforejtn; 24th February 2015 at 18:03.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom widget: paintEvent, can't change member variable

    We've all been there and done that.

Similar Threads

  1. Change background color of a custom widget
    By rubikon in forum Newbie
    Replies: 10
    Last Post: 5th July 2012, 13:21
  2. Replies: 1
    Last Post: 9th May 2012, 08:58
  3. Replies: 3
    Last Post: 12th April 2011, 10:58
  4. Replies: 22
    Last Post: 8th October 2008, 13:54
  5. Std iterator as private member variable
    By Michiel in forum General Programming
    Replies: 5
    Last Post: 21st April 2006, 15:27

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.