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. [PySide2] How come this code is not getting executed in a line by line order?
Forum Updated to NodeBB v4.3 + New Features

[PySide2] How come this code is not getting executed in a line by line order?

Scheduled Pinned Locked Moved Solved Qt for Python
9 Posts 4 Posters 1.2k 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.
  • L Offline
    L Offline
    lolcocks
    wrote on last edited by
    #1

    Hello,

    I am using a code to connect my Raspberry Pi to the WiFi and it works.
    But changing the text on the labels, that doesn't work in t he correct order.

    For example, this is the code to view a list of WiFi networks and add them to a combobox:

            self.label_connectionStatus2.setText("Refreshing...")
            
            out = subprocess.Popen('sudo rfkill unblock 0', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            out = subprocess.Popen('ifconfig wlan0 up', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            out = subprocess.Popen('sudo /sbin/iwlist wlan0 scan | grep ESSID', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            stdout = out.communicate()[0]
            stdout = stdout.decode("utf-8")
            wifilist = stdout.split('"')[1::2]
            self.comboBox_WiFiName.clear()
            self.comboBox_WiFiName.addItems(wifilist)
            
    

    The code works but the part where I use 'setText("Refreshing...")' on the label, that part is executed at the end, it shows up after the code below it has been executed.

    I even tried adding a 'time.sleep(10)' after the 'self.label_connectionStatus2.setText("Refreshing...")' line, but nothing changed.

    Why is this happening?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MEMekaniske
      wrote on last edited by MEMekaniske
      #2

      Hey

      Some GUI's are only drawn after code blocks are executed or if you call a function for drawing.
      In Qt you could try running this to signal Qt to process pending events, or emit a signal for change.

      self.label_connectionStatus2.setText("Refreshing...")
      app.processEvents()
      

      https://doc.qt.io/qt-5/qcoreapplication.html#processEvents

      L 1 Reply Last reply
      0
      • M MEMekaniske

        Hey

        Some GUI's are only drawn after code blocks are executed or if you call a function for drawing.
        In Qt you could try running this to signal Qt to process pending events, or emit a signal for change.

        self.label_connectionStatus2.setText("Refreshing...")
        app.processEvents()
        

        https://doc.qt.io/qt-5/qcoreapplication.html#processEvents

        L Offline
        L Offline
        lolcocks
        wrote on last edited by
        #3

        @MEMekaniske Thank you! Works perfectly!

        M 1 Reply Last reply
        0
        • L lolcocks

          @MEMekaniske Thank you! Works perfectly!

          M Offline
          M Offline
          MEMekaniske
          wrote on last edited by
          #4

          @lolcocks very good :)

          No problem!

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lolcocks
            wrote on last edited by
            #5

            @Denni-0 Thank you so much good sir, this does clarify how it works internally. Thank you for sharing and improving my knowledge. :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MEMekaniske
              wrote on last edited by
              #6

              @lolcocks said in [PySide2] How come this code is not getting executed in a line by line order?:

              @Denni-0 Thank you so much good sir, this does clarify how it works internally. Thank you for sharing and improving my knowledge. :)

              You would have learned what dennis said + a lot more from following the link I gave to the documentation.. :)

              1 Reply Last reply
              1
              • JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                I will throw in: personally, I would have tried

                self.label_connectionStatus2.repaint()
                

                https://doc.qt.io/qt-5/qwidget.html#repaint, first, unless a Qt expert says otherwise. (Not update() here, that requires a processEvents().) Provided it works, no need to spin a processEvents() with unknown consequences if all you want to achieve is making text immediately visible to user. If it does not work, or if you see any unacceptable "flicker" (which I doubt you will here), then I would have gone for processEvents().

                Of course, really the subprocess should be run in a separate thread, or asynchronously, and this whole matter of UI updates being blocked would not be an issue. If the OP chose to use Qt's QProcess to do the commands it would all be asynchronous with signals/slots and there would be no issue. This is a better choice than Python subprocess in a Qt GUI application, but that is a matter of preference. (Incidentally, your code for running the 3 sub-processes is subject to race conditions: it runs them in parallel not in series.)

                M 1 Reply Last reply
                1
                • JonBJ JonB

                  I will throw in: personally, I would have tried

                  self.label_connectionStatus2.repaint()
                  

                  https://doc.qt.io/qt-5/qwidget.html#repaint, first, unless a Qt expert says otherwise. (Not update() here, that requires a processEvents().) Provided it works, no need to spin a processEvents() with unknown consequences if all you want to achieve is making text immediately visible to user. If it does not work, or if you see any unacceptable "flicker" (which I doubt you will here), then I would have gone for processEvents().

                  Of course, really the subprocess should be run in a separate thread, or asynchronously, and this whole matter of UI updates being blocked would not be an issue. If the OP chose to use Qt's QProcess to do the commands it would all be asynchronous with signals/slots and there would be no issue. This is a better choice than Python subprocess in a Qt GUI application, but that is a matter of preference. (Incidentally, your code for running the 3 sub-processes is subject to race conditions: it runs them in parallel not in series.)

                  M Offline
                  M Offline
                  MEMekaniske
                  wrote on last edited by
                  #8

                  @JonB guess this process uses less resources and time when executed too compared to processEvents :)

                  JonBJ 1 Reply Last reply
                  0
                  • M MEMekaniske

                    @JonB guess this process uses less resources and time when executed too compared to processEvents :)

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

                    @MEMekaniske
                    I'm just a purist :) repaint() has no side-effects and is self-contained. If you do a processEvents() you do not know that only that label will be updated, there could be other pending events/signals which could do potentially other things.

                    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