-1

I am coding GUI in Python and PyQt and some parts of the logic needs to be executed in specific time intervals measured in ms. I think that running those parts as threads and keep track of time would be best but please correct me if I am wrong.

Second question, can I use the Threads as an classic objects and use them in same way? Call methods or just use them for specific task and keep them as simple as possible? Because from the thread nature I am usually tempted to write run() method as procedure and somehow lacking OOP approach.

Thanks

Michal Gonda
  • 174
  • 2
  • 14
  • Why don't you use QtCore.QTimer? – x3al Jun 04 '15 at 14:23
  • Thanks for the hint. But isn't QTimer just for trigger the events after specific time interval? I need to dig little bit deeper, but I also need to track the time from start of the app, read file as source of messages with time-stamps and if time-stamp match the time passed, transmit the message. – Michal Gonda Jun 05 '15 at 09:13

1 Answers1

2

1st part:

If the timed part of your code is rather computation heavy it would be good to do it in a QThread - if not there is no need to make things complicated.

As said by x3al in the comment, there is no obvious reason why you should not be able to do the timing using QTimer. If you need a repeating signal you can use setInterval. If its about a single signal after X ms you can just use singleShot (pyqt4-doc).

2nd part:

If I get this part correctly you are asking for advice on how to use threads - as this depends on your implementation (movetothread vs subclassing), have a look at this answers Background thread with QThread in PyQt

Allow me to recycle an example I used before on this matter:

class ThreadObject(QObject):
started = pyqtSignal()
finished = pyqtSignal()

def __init__(self, var1, var2):
    super(ThreadObject, self).__init__()
    self.var1 = var1
    self.var2 = var2

    self._isRunning = True

def stop(self):
    _isRunning = False

def run(self):
    # do something with the var's and send the results to where they are needed while _isRunning is True

class UIelement(QWidget):
    # initialize, create UI and so on
    def clicksomebutton(self):
        self.obj = ThreadObject(self.var1, self.var2) # handle copies of vars to object
        self.thread = = QThread(self, objectName='workerThread')
        self.obj.moveToThread(self.thread)
        # connect start, stop, run, etc.
        self.thread.start()

Hope this helps you!

Community
  • 1
  • 1
mmensing
  • 311
  • 1
  • 7
  • thank's! I will use the QTimer and besed on "time_from_file - current_time" set qtimer to be fired. I already using QThreads but I think I was little bit confused about proper way to use them. – Michal Gonda Jun 09 '15 at 15:49