0

Hi

need help passing an argument through signal emitted from a function at module level,

when the function is executed I want to emit a signal with an argument whose value i want to pass to the method that is connected to that signal in the class in that same module. the class is the QWidget UI

from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PyQt4.QtGui import QWidget, QApplication
from PyQt4 import QtCore
import sys

_qObject = QtCore.QObject()

has_error = pyqtSignal(Exception)

class SomeOtherClass(QWidget):

    # this is my UI class

    def __init__(self, parent=None):
        super(SomeOtherClass, self).__init__(parent)

        # Initialise the Class and connect signal to slot
        QtCore.QObject.connect(_qObject, has_error, self.thrown_error)
        # QtCore.QObject.has_error.connect(self.thrown_error)


    @pyqtSlot(Exception)
    def thrown_error(self, my_err):
        #Do Stuff with the Exception
        print(type(my_err), my_err)
        self.close()


def makeError():
    try:
        print 1/0
    except ZeroDivisionError, ze:
        has_error.emit(ze)

makeError()

app = QApplication(sys.argv)
SomeOtherClass()

but this is throwing me error..

2
Contributors
1
Reply
14
Views
2 Years
Discussion Span
Last Post by woooee
1

You would use partial. I don't have Qt installed so can't test, but this should work. Note that if it is self.thrown error, you don't have to pass it to the function as "self" variables can be accessed anywhere within the class.

from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PyQt4.QtGui import QWidget, QApplication
from PyQt4 import QtCore
import sys
from functools import partial

_qObject = QtCore.QObject()

has_error = pyqtSignal(Exception)

class SomeOtherClass(QWidget):

    # this is my UI class

    def __init__(self, parent=None):
        super(SomeOtherClass, self).__init__(parent)

        # Initialise the Class and connect signal to slot
        QtCore.QObject.connect(_qObject, partial(has_error, self.thrown_error))


    @pyqtSlot(Exception)
    def thrown_error(self, my_err):
        #Do Stuff with the Exception
        print(type(my_err), my_err)
        self.close()


def makeError():
    try:
        print 1/0
    except ZeroDivisionError, ze:
        has_error.emit(ze)

makeError()

app = QApplication(sys.argv)
SomeOtherClass()

Edited by woooee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.