Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. make 2 updates to label from 1 slot

make 2 updates to label from 1 slot

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 5 Posters 497 Views
  • 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.
  • G Offline
    G Offline
    gblessed
    wrote on last edited by
    #1

    Hello, am making an application, with a QPushButton, when the button is pressed its should set a label's text to 'a' and then after 3 seconds it should set it to 'b'
    I have implemented this in the slot connected to the pushbutton' s click. However whenever I click the button it just sets to 'b' i don't see it setting to a'a' before going to 'b'.

    1 Reply Last reply
    0
    • G gblessed
      def greeting():
          """Slot function."""
          btn.setEnabled(False)
          msg.setText('a.........................................')
          sleep(3)
          msg.setText('b')
          btn.setEnabled(True)
      
      
      app = QApplication(sys.argv)
      window = QWidget()
      window.setWindowTitle('Signals and slots')
      layout = QVBoxLayout()
      btn = QPushButton('Greet')
      msg = QLabel('')
      
      btn.clicked.connect(functools.partial(greeting))  # Connect clicked to greeting
      layout.addWidget(btn)
      
      layout.addWidget(msg)
      window.setLayout(layout)
      window.show()
      sys.exit(app.exec_())
      

      i dont think its a problem with the sleep()
      even without the sleep.... any the last modifications i make in the slot take action
      i cant make multiple changes to a widget from a slot. only the last change is effected.
      even the button is not disabled.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #7

      @gblessed your sleep() call blocks the event loop. That means for 3 seconds your UI will NOT be updated! You never do such things in event driven applications! You really have to learn this.
      The explanation why you don't see button disabled without sleep() is very simple: you disable the button and immediately enable it again.

      To solve your issue do what @JonB already suggested: use QTimer. Remove the sleep() and both lines bellow it and use a QTimer with 3 seconds timeout. In the timeout slot you put the last two lines from greeting().

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,

        How did you implement the three seconds delay ?

        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
        1
        • G Offline
          G Offline
          gblessed
          wrote on last edited by
          #3

          time.sleep(3)

          JonBJ 1 Reply Last reply
          0
          • G gblessed

            time.sleep(3)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @gblessed
            Which blocks, so you don't see the a, just the b 3 seconds later. You need to use QTimer to do this properly.

            1 Reply Last reply
            3
            • G Offline
              G Offline
              gblessed
              wrote on last edited by
              #5
              def greeting():
                  """Slot function."""
                  btn.setEnabled(False)
                  msg.setText('a.........................................')
                  sleep(3)
                  msg.setText('b')
                  btn.setEnabled(True)
              
              
              app = QApplication(sys.argv)
              window = QWidget()
              window.setWindowTitle('Signals and slots')
              layout = QVBoxLayout()
              btn = QPushButton('Greet')
              msg = QLabel('')
              
              btn.clicked.connect(functools.partial(greeting))  # Connect clicked to greeting
              layout.addWidget(btn)
              
              layout.addWidget(msg)
              window.setLayout(layout)
              window.show()
              sys.exit(app.exec_())
              

              i dont think its a problem with the sleep()
              even without the sleep.... any the last modifications i make in the slot take action
              i cant make multiple changes to a widget from a slot. only the last change is effected.
              even the button is not disabled.

              J.HilkJ jsulmJ 2 Replies Last reply
              0
              • G gblessed
                def greeting():
                    """Slot function."""
                    btn.setEnabled(False)
                    msg.setText('a.........................................')
                    sleep(3)
                    msg.setText('b')
                    btn.setEnabled(True)
                
                
                app = QApplication(sys.argv)
                window = QWidget()
                window.setWindowTitle('Signals and slots')
                layout = QVBoxLayout()
                btn = QPushButton('Greet')
                msg = QLabel('')
                
                btn.clicked.connect(functools.partial(greeting))  # Connect clicked to greeting
                layout.addWidget(btn)
                
                layout.addWidget(msg)
                window.setLayout(layout)
                window.show()
                sys.exit(app.exec_())
                

                i dont think its a problem with the sleep()
                even without the sleep.... any the last modifications i make in the slot take action
                i cant make multiple changes to a widget from a slot. only the last change is effected.
                even the button is not disabled.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @gblessed said in make 2 updates to label from 1 slot:

                i dont think its a problem with the sleep()

                And you would be wrong about that.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                4
                • G gblessed
                  def greeting():
                      """Slot function."""
                      btn.setEnabled(False)
                      msg.setText('a.........................................')
                      sleep(3)
                      msg.setText('b')
                      btn.setEnabled(True)
                  
                  
                  app = QApplication(sys.argv)
                  window = QWidget()
                  window.setWindowTitle('Signals and slots')
                  layout = QVBoxLayout()
                  btn = QPushButton('Greet')
                  msg = QLabel('')
                  
                  btn.clicked.connect(functools.partial(greeting))  # Connect clicked to greeting
                  layout.addWidget(btn)
                  
                  layout.addWidget(msg)
                  window.setLayout(layout)
                  window.show()
                  sys.exit(app.exec_())
                  

                  i dont think its a problem with the sleep()
                  even without the sleep.... any the last modifications i make in the slot take action
                  i cant make multiple changes to a widget from a slot. only the last change is effected.
                  even the button is not disabled.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #7

                  @gblessed your sleep() call blocks the event loop. That means for 3 seconds your UI will NOT be updated! You never do such things in event driven applications! You really have to learn this.
                  The explanation why you don't see button disabled without sleep() is very simple: you disable the button and immediately enable it again.

                  To solve your issue do what @JonB already suggested: use QTimer. Remove the sleep() and both lines bellow it and use a QTimer with 3 seconds timeout. In the timeout slot you put the last two lines from greeting().

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  5
                  • G Offline
                    G Offline
                    gblessed
                    wrote on last edited by
                    #8

                    @jsulm perfectly understood, thanks problem solved

                    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