QTimer, threads, and non-gui events

Firstly, the book has been a really good reference and learning tool… thanks!

I’m a bit stuck trying to implement something. I’ve created the following test script:

from PySide2 import QtCore, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):

        super().__init__()
        self._setup_timer()

        layout = QtWidgets.QVBoxLayout()
        self.l = QtWidgets.QLabel("Tick tester")
        b = QtWidgets.QPushButton("START")
        b.pressed.connect(self._do_thing)
        layout.addWidget(self.l)
        layout.addWidget(b)
        w = QtWidgets.QWidget()
        w.setLayout(layout)
        self.setCentralWidget(w)
        self.show()

    def _setup_timer(self):
        self._timer = QtCore.QTimer()
        self._timer.timeout.connect(self._timer_tick)

    def _do_thing(self):
        self._tick_count = 0
        self._timer.start(1000)
        for n in range(0, 3):
            print(f"n={n}")
            for m in range(0, 5):
                print(f"m={m}")
        # stop timer
        print("do one more thing")

    def _timer_tick(self):
        print(f"Timer tick: {self._tick_count}")
        print("I'm doing something every second!")
        self._tick_count += 1


app = QtWidgets.QApplication([])
window = MainWindow()
app.exec_()

And as you’d expect I get the following output:

n=0
m=0
m=1
m=2
m=3
m=4
n=1
m=0
m=1
m=2
m=3
m=4
n=2
m=0
m=1
m=2
m=3
m=4
do one more thing
Timer tick: 0
I'm doing something every second!
Timer tick: 1
I'm doing something every second!
Timer tick: 2
I'm doing something every second!
Timer tick: 3
I'm doing something every second!

But what I’m actually after is ‘waiting’ for the ticks in do_thing's m for loop such that the output looks something more like this:

n=0
m=0
Timer tick: 0
I'm doing something every second!
m=1
Timer tick: 1
I'm doing something every second!
m=2
Timer tick: 2
I'm doing something every second!
...
do one more thing

And, that do one more thing happens after the nested for loops are done with. (I’m not including the _tick_count test because that’s part of the problem :wink: )

What I’m missing is how to ‘connect’ what’s happening with the ‘ticks’ with the main event loop such that it knows when to proceed / wait .

I’ve read the excellent section in the book about threads:

together with other resources for how to possible achieve what I want. It’s clear that the print in _timer_tick will need to be replaced with a worker thread.

Should I use worker signals? Am I missing something trivial (quite possible!)

What I’d like to understand better is what is the simplest and most robust way to do it. If anyone has good pointers, I’d appreciate it.

Thanks!