Cannot change value/text after setValue in paintEvent
-
Problem
In an Qt Widgets Application, I cannot change the value of my
QSpinBox
after IsetValue
for it in overloadedpaintEvent
ofMainWindow
.Reproduce the issue
I can reproduce the issue in the following steps.
- Create a QtWidget Application by Qt Creator
- Create a
QSpinBox
by embedded Qt Designer - Add following code to
mainwindow.h
void paintEvent(QPaintEvent *);
- Add following code to
mainwindow.cpp
void MainWindow::paintEvent(QPaintEvent *) { ui->spinBox->setValue(2); }
- Build and run the application.
- 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
andQLineEdit
, 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, butsetValue(int)
does not work anymore. - I read documentation about
QPaintEvent
andQAbstractSpinBox
. I can't find anything useful.
Help me please!
Environments
OS: Windows10
Qt Compiler: Desktop Qt 5.11.1 MinGW 32bitSource Code
The complete source code can be found in mainwindow.h, main.cpp, mainwindow.cpp, mainwindow.ui.
-
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.
-
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 inmain.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.
-
@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...
-
@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
ofQMainWindow
does nothing.