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. Easy to read 60 lines of code, minimal threading example, shows blank widget! 😊
Forum Updated to NodeBB v4.3 + New Features

Easy to read 60 lines of code, minimal threading example, shows blank widget! 😊

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 1.5k 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #7

    You may consider this library if you want true parallelism in Python:
    https://docs.python.org/2/library/multiprocessing.html
    Available for both 2.7 and 3.x

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    1
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by
      #8

      Okay, here is the updated code, thanks @SGaist .

      from PyQt5.QtCore import QObject, QThread, pyqtSignal
      from PyQt5.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, 
                                   QPushButton, QPlainTextEdit, QApplication, QGridLayout)
      import sys
      
      
      class Thread1(QThread):
          statusMessage = pyqtSignal(str)
          
          def __init__(self):
              super().__init__()
              
          def run(self):
              while self._running:
                  self.statusMessage.emit('Doing the work of thread 1.')
                  self.sleep(1)
                  
          def startRunning(self):
              self._running = True
              self.start()    
                  
      class Thread2(QThread):
          statusMessage = pyqtSignal(str)
          
          def __init__(self):
              super().__init__()
              
          def run(self):
              while self._running:
                  self.statusMessage.emit('Doing the work of thread 2.')
                  self.sleep(2)
                  
          def startRunning(self):
              self._running = True
              self.start()
              
      
      class Widget(QWidget):
          def __init__(self):
              super().__init__()
              self.textEdit = QPlainTextEdit(parent=self)
              self.button = QPushButton('click me')
              lyt = QVBoxLayout()
              self.setLayout(lyt)
              lyt.addWidget(self.button)
              lyt.addWidget(self.textEdit)
              self.button.clicked.connect(self.runTwoThreads)
              
          def runTwoThreads(self):
              self.thread1 = Thread1()
              self.thread1.statusMessage.connect(self.showStatusMessage)
              self.thread2 = Thread2()
              self.thread2.statusMessage.connect(self.showStatusMessage)
              self.thread1.startRunning()
              self.thread2.startRunning()
              
          def showStatusMessage(self, msg):
              self.textEdit.appendPlainText(msg)
              
              
      if __name__ == '__main__':
          app = QApplication([])
          
          window = Widget()
          window.show()
          
          sys.exit(app.exec_())
      

      And it demonstrates the bug I'm seeing at a higher level with my app. For me, when I click the button, only thread two is running, and thread 1 never emits a message, or if you put a breakpoint in thread1's run() method, it will not be hit.

      Thanks all for your help.

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

        What version of PyQt5 and Qt are you using ?
        On what OS ?

        I have both thread outputs here.

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

        enjoysmathE 2 Replies Last reply
        0
        • SGaistS SGaist

          What version of PyQt5 and Qt are you using ?
          On what OS ?

          I have both thread outputs here.

          enjoysmathE Offline
          enjoysmathE Offline
          enjoysmath
          wrote on last edited by
          #10

          @SGaist

          Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
          Type "help", "copyright", "credits" or "license" for more information.
          >>>
          
          Name: PyQt5
          Version: 5.12.1
          Summary: Python bindings for the Qt cross platform UI and application toolkit
          Home-page: https://www.riverbankcomputing.com/software/pyqt/
          Author: Riverbank Computing Limited
          Author-email: info@riverbankcomputing.com
          License: GPL v3
          Location: c:\python36-32\lib\site-packages
          Requires: PyQt5-sip
          Required-by: QScintilla, PyQtChart
          

          0_1556226645570_481fde4d-7f07-4604-bf7b-b6d2ba40c890-image.png

          1 Reply Last reply
          0
          • enjoysmathE Offline
            enjoysmathE Offline
            enjoysmath
            wrote on last edited by
            #11

            I know I'm running 32-bit python because it's installed to C:\Python36-32 and that's my convention. I think I updated everything a few days ago in order to look at a pyinstaller issue.

            1 Reply Last reply
            0
            • SGaistS SGaist

              What version of PyQt5 and Qt are you using ?
              On what OS ?

              I have both thread outputs here.

              enjoysmathE Offline
              enjoysmathE Offline
              enjoysmath
              wrote on last edited by
              #12

              @SGaist

              What are your versions, maybe I should try those out?

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

                Just tested with the latest version available on macOS through pip and it worked as expected.

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

                enjoysmathE 2 Replies Last reply
                0
                • SGaistS SGaist

                  Just tested with the latest version available on macOS through pip and it worked as expected.

                  enjoysmathE Offline
                  enjoysmathE Offline
                  enjoysmath
                  wrote on last edited by
                  #14
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Just tested with the latest version available on macOS through pip and it worked as expected.

                    enjoysmathE Offline
                    enjoysmathE Offline
                    enjoysmath
                    wrote on last edited by enjoysmath
                    #15

                    @SGaist

                    I'm trying out Python 3.7 now, but maybe PyQt5 won't even install, so... I'll try some things out. Any ideas? Should I just combine the work of the two threads into one thread, since I do have exactly the same loop sleep set of 1 ms (the practical min).

                    I'm testing 3.7.3 both 32 and 64-bit.

                    1 Reply Last reply
                    0
                    • enjoysmathE Offline
                      enjoysmathE Offline
                      enjoysmath
                      wrote on last edited by
                      #16

                      I tried 3.7.3 64-bit and the latest PyQt5 did install. However, the minimal app is having the exact same issue on my system.

                      So I will just combine the different threads all into one, and switch between the cases.

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

                        Might be a silly test, but did you try using the QThread method to start your thread ? As I wrote earlier, I don't know how startRunning is implemented.

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

                        enjoysmathE 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Might be a silly test, but did you try using the QThread method to start your thread ? As I wrote earlier, I don't know how startRunning is implemented.

                          enjoysmathE Offline
                          enjoysmathE Offline
                          enjoysmath
                          wrote on last edited by
                          #18

                          @SGaist said in Easy to read 60 lines of code, minimal threading example, shows blank widget! 😊:

                          Might be a silly test, but did you try using the QThread method to start your thread ? As I wrote earlier, I don't know how startRunning is implemented.

                          Nope, that had no effect.

                          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