Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot change value/text after setValue in paintEvent



  • Problem

    In an Qt Widgets Application, I cannot change the value of my QSpinBox after I setValue for it in overloaded paintEvent of MainWindow.

    Reproduce the issue

    I can reproduce the issue in the following steps.

    1. Create a QtWidget Application by Qt Creator
    2. Create a QSpinBox by embedded Qt Designer
    3. Add following code to mainwindow.h
    void paintEvent(QPaintEvent *);
    
    1. Add following code to mainwindow.cpp
    void MainWindow::paintEvent(QPaintEvent *) { ui->spinBox->setValue(2); }
    
    1. Build and run the application.
    2. Try to change the value of the spinBox and you will find you cannot change the value.

    I can move my function setValue to another place to avoid this issue, but why this happen? I can't find what mistake that I make.

    Some trials

    • I tested QDoubleSpinBox and QLineEdit, they behave similarly.
    • Trying to setReadOnly(false), it doesn't work.
    • When I try to change the value, I get signal of valueChanged(int) emiting, but setValue(int) does not work anymore.
    • I read documentation about QPaintEvent and QAbstractSpinBox. I can't find anything useful.

    Help me please!

    Environments

    OS: Windows10
    Qt Compiler: Desktop Qt 5.11.1 MinGW 32bit

    Source Code

    The complete source code can be found in mainwindow.h, main.cpp, mainwindow.cpp, mainwindow.ui.


  • Qt Champions 2019

    First, you don't call the baseclass implementation of paintEvent() so where should the painting occour?
    Second the documentation of QWidget::paintEvent() says that you should not call update() inside paintEvent:

    Note: Generally, you should refrain from calling update() or repaint() inside a paintEvent(). For example, calling update() or repaint() on children inside a paintEvent() results in undefined behavior; the child may or may not get a paint event.


  • Lifetime Qt Champion

    Hi,

    To add to @Christian-Ehrlicher, why are you using the paint event to change a value in another widget ?



  • @Christian-Ehrlicher Thank you. I tried to search paintEvent() in documentation but I failed to find it. (Suggest Qt Documentation to support search a function) As your first question, the baseclass called in main.cpp, which generated automatically by wizard.



  • @SGaist Thx for replying. I simplify a large project(my Qt assignment) to the routine in my question. At first, I change a value just because of convenience, and I don't think it could be any problem at all. I spend several hours to debug and simplify it, I think my effort deserve a reason, why I can't use the paint event to change a value.


  • Qt Champions 2019

    @namasikanam said in Cannot change value/text after setValue in paintEvent:

    As your first question, the baseclass called in main.cpp, which generated automatically by wizard.

    You code above does not call the base class paint event implementation...


  • Lifetime Qt Champion

    @namasikanam said in Cannot change value/text after setValue in paintEvent:

    @SGaist Thx for replying. I simplify a large project(my Qt assignment) to the routine in my question. At first, I change a value just because of convenience, and I don't think it could be any problem at all. I spend several hours to debug and simplify it, I think my effort deserve a reason, why I can't use the paint event to change a value.

    Nobody said you can't, however as the name suggest the paintEvent method goal is to draw the widget. Changing a property value in it doesn't particularly make sense. Depending on what you do in your application or with your system, it can be called episodically to several times a second.



  • @Christian-Ehrlicher Sorry, I misunderstood you before. I doesn't call the paint event of base class because I think the default paintEvent of QMainWindow does nothing.


Log in to reply