Jason94
Jason94

Reputation: 13610

how to connect a event to a function?

I have this small media app that holds two buttons that should start and stop music. but there is something wrong to my logic... please take a look:

class AppWindow(QtGui.QWidget):

    def mediastart():
        os.system("...")

    def mediastop():
        os.system("...")

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 800, 480)
        self.setWindowTitle("EasySteerQT")

        start = QtGui.QPushButton("Start", self)
        start.setGeometry(50, 50, 60, 35)

        stop = QtGui.QPushButton("Stop", self)
        stop.setGeometry(100, 50, 60, 35)

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(quit, QtCore.SIGNAL("clicked()"),
            QtGui.qApp, QtCore.SLOT("quit()"))

        self.connect(start, QtCore.SIGNAL("clicked()"),
            QtGui.qApp, QtCore.SLOT("mediastart()"))

        self.connect(start, QtCore.SIGNAL("clicked()"),
            self, QtCore.SLOT("mediastop()"))

in what way is the propper way to connect a action to a function in this class?

Upvotes: 1

Views: 866

Answers (1)

user395760
user395760

Reputation:

Use self.mediastop. QtCore.SLOT('mediastop()') refers to a (in this case non-existent) slot defined in C++. Not that that's not really necessary anyway, you can use WidgetClass.slotName - it just saves you a conversion.

And while we're at it, you should propably switch from old-style signals/slots to new-style signals/slots. E.g. self.clicked.connect(self.mediastop).

Upvotes: 4

Related Questions