Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot change value/text after setValue in paintEvent

Cannot change value/text after setValue in paintEvent

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    namasikanam
    wrote on last edited by namasikanam
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      N 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          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.

          N Offline
          N Offline
          namasikanam
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            N Offline
            N Offline
            namasikanam
            wrote on last edited by
            #5

            @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.

            SGaistS 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              N 1 Reply Last reply
              2
              • N namasikanam

                @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.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @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...

                  N Offline
                  N Offline
                  namasikanam
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved