2

This is a follow-up to my unanswered question here. The code, exactly as it is below crashes with a segfault (copy/paste it into your system and run it). When I remove type=QtCore.Qt.DirectConnection from one or both of the signal constructor calls (thereby using QtCore.Qt.AutoConnection instead), everything runs the way it should: A widget appears, showing five progress bars filling up then emptying in an endless loop.

from PySide import QtCore, QtGui
import time

class Worker(QtCore.QThread):
    sig_worker_update_progress = QtCore.Signal(int, int)

    def __init__(self, thread_id, *args, **kwargs):
        super(Worker, self).__init__(*args, **kwargs)
        self.thread_id = thread_id
        self.stop_requested = False

    def slot_interrupt(self):
        self.stop_requested = True

    def run(self):
        progress = 0
        while(True):
            self.sig_worker_update_progress.emit(self.thread_id, progress % 100)
            progress += 1
            if self.stop_requested:
                break
            else:
                time.sleep(0.1)


class Controller(QtCore.QObject):
    sig_controller_update_progress = QtCore.Signal(int, int)

    def __init__(self, num_workers, *args, **kwargs):
        super(Controller, self).__init__(*args, **kwargs)

        self.workers = []
        for i in range(num_workers):
            self.workers.append(Worker(i))
            self.workers[i].sig_worker_update_progress.connect(
                self.slot_worker_update_progress,
                type=QtCore.Qt.DirectConnection)
        for worker in self.workers:
            worker.start()

    def slot_worker_update_progress(self, thread_id, progress):
        # Do
        # Stuff
        self.sig_controller_update_progress.emit(thread_id, progress)


class Monitor(QtGui.QWidget):
    def __init__(self, num_workers, *args, **kwargs):
        super(Monitor, self).__init__(*args, **kwargs)
        main_layout = QtGui.QVBoxLayout()
        self.setLayout(main_layout)
        self.progress_bars = []

        for _ in range(num_workers):
            progress_bar = QtGui.QProgressBar()
            main_layout.addWidget(progress_bar)
            self.progress_bars.append(progress_bar)

        self.controller  = Controller(num_workers)
        self.controller.sig_controller_update_progress.connect(
            self.slot_controller_update_progress,
            type=QtCore.Qt.DirectConnection)

    def slot_controller_update_progress(self, thread_id, progress):
        self.progress_bars[thread_id].setValue(progress)


if __name__ == "__main__":
    app = QtGui.QApplication([])
    monitor = Monitor(5)
    monitor.show()
    app.exec_()

Why does using two nested DirectConnection signals cause a segfault? If Qt does not want you to do that, why isn't a more informative error given?

I am using PySide v1.2.2 which wraps the Qt 4.8 framework.

Community
  • 1
  • 1
nullstellensatz
  • 756
  • 8
  • 18

1 Answers1

1

I found a satisfying explanation here. Apparently, emitting a signal of type DirectConnection is equivalent to a direct function call. So the GUI is after all updated on a Worker thread when both signals are DirectConnect-ed. As mentioned on my other question, threads are not allowed to change the GUI. The issue is NOT with nesting DirectConnections per se.

Community
  • 1
  • 1
nullstellensatz
  • 756
  • 8
  • 18
  • As a sidenote: one probably never needs to change the connection type from AutoConnection to DirectConnection unless in very special cases or when heavily optimizing. So probably never. – NoDataDumpNoContribution Jan 06 '15 at 12:57
  • See [here](http://stackoverflow.com/questions/27682241/qpixmap-it-is-not-safe-to-use-pixmaps-outside-the-gui-thread) for background on why I needed to use DirectConnections. – nullstellensatz Jan 08 '15 at 14:15