1

So I tried setting up a simple button widget->main window (ground symbol) signal->slot. Once doing so, I edit in a custom slot name, which I will add to the class later.

Basic Example:

Connecting pushButton.clicked()->MyMainWindow.my_slot()

class Ui_MyMainWindow(object):
    def setupUi(self, MyMainWindow):
        MyMainWindow.setObjectName(_fromUtf8("MyMainWindow"))
        MyMainWindow.setEnabled(True)
        MyMainWindow.resize(537, 349)
        ...
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout.addWidget(self.pushButton)
        MyMainWindow.setCentralWidget(self.centralwidget)


        '''PROBLEM MyMainWindow.my_slot() in the following line should be
        Ui_MyMainWindow.my_slot so slot/function belongs to the class 
        or even without a parent so I can do a ''from my_existing_module import *''. 
        For widget->widget calls, this way of linking makes sense, but
        if I just want to call some pre-written function, it doesnt
        work without stupid hand editing'''

        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MyMainWindow.my_slot)
        QtCore.QMetaObject.connectSlotsByName(MyMainWindow)
        ...


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MyMainWindow = QtGui.QMainWindow()
    ui = Ui_MyMainWindow()
    ui.setupUi(MyMainWindow)
    MyMainWindow.show()
    sys.exit(app.exec_())

I tried looking into the compiler.py file and other related ones to see if I could modify the argument name of setupUi but I got lost in all the calls to imported functions and jumping back and forth between files. I mostly just broke things. Thank god for backups.

Conner Phillips
  • 403
  • 4
  • 7
  • 2
    You are making the very common mistake of attempting to edit the gui module generated by pyuic. Please read [this section](http://pyqt.sourceforge.net/Docs/PyQt4/designer.html) of the pyqt docs so that you understand how to use pyuic correctly. – ekhumoro Aug 31 '15 at 22:39
  • But I don't want to! The slot name I created in Qt Designer isn't being referenced properly because the slot expects to see a parent widget named 'MyMainWindow' but because it is THE main window, pyuic renames it to **Ui**_MyMainWindow, making the slot nonfunctional. – Conner Phillips Sep 01 '15 at 06:43
  • As I'm looking into this, it seems that if I want to write my own slots, I have to make a subclass of Ui_MyMainWindow and then write in the connections by hand in the subclass? If that is how I have to do it, fine, but it makes no sense when there are suggestions like the following (what I tried): http://stackoverflow.com/questions/7964869/qt-designer-how-to-add-custom-slot-and-code-to-a-button – Conner Phillips Sep 01 '15 at 09:33
  • You don't need to make the connections "by hand", because they have already been made in the module generated by pyuic. All you need to do is ensure that the object passed in to `setupUi` has a slot with the right name. If you look at the second and third examples in the link I posted above, it should be easy to see that you just need to add a method named `my_slot` and everything will work as expected. (Obviously, you should ignore the signal connections in those examples, since they are not relevant to your use-case). – ekhumoro Sep 01 '15 at 15:59

0 Answers0