I am currently developing a PyQt application in Visual Studio. Debugging has been working great, until I decided to keep my UI responsive by moving stuff to a worker thread with Qt Threads.
class MainWindow(base, form):
start_work = QtCore.pyqtSignal()
def __init__(self, parent=None):
# Create a seperate thread in which the update information is polled.
self.thread = QtCore.QThread()
# Create Worker object and move it to new thread
self.worker = Worker()
self.worker.moveToThread(self.thread)
# connect signal to start work in the extra tread
self.start_work.connect(self.worker.get_work_done)
self.thread.start()
#function emit a signal to start doing the work
def do_work(self):
self.startWork.emit()
Any function that is invoked on my worker object is connected via signal slots
class Worker(QtCore.QObject):
@QtCore.pyqtSlot()
def get_work_done(self):
#lets do some time consuming work.
The code works fine. The only problem is now, I cannot debug anything that is happening inside get_work_done
. Visual studio won't break at those breakpoints.
When I break inside any MainWindow
function, the Visual Studio Debugger shows only one thread. It seems unaware of any other threads created by the application.
As said already all threads not created using Python need to be registered with PTVS. To do this for a QThread, call this in the threads run method:
import pydevd;pydevd.settrace(suspend=False)
Some more info here: Can I put break points on background threads in Python?
Debugger needs to play some tricks to detect new threads and set up its hooks (which are needed to hit breakpoints etc). It does so by hijacking the standard Python _thread
module. If you're creating the threads in some way that circumvents that module altogether, which is what I suspect Qt does here, the debugger will not be aware of those threads.
Try using the standard threading
module instead of QThread
, and see if that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With