No such slot QMainWindow when loading .ui file

Viewed 28

I'm doing some experimentation with Qt6 and Python using Qt Designer. So far I'm blocked trying to do an good old "onClick + hello world" example and based on my latest research this part of the development is referenced as "actions and slots".

So I've added a new QPushButton name pushButton and created a new slot for the QMainWindow (MainWindow) and called it clickedNewConn.

On the Sinal/Slot Editor add new configuration line following the Sender -> Signal -> Receiver -> Slot:

pushButton -> clicked() -> MainWindow -> clickedNewConn()

So far so good. Until I loaded my main.py and received the following error:

qt.core.qobject.connect: QObject::connect: No such slot QMainWindow::clickedNewConn()
qt.core.qobject.connect: QObject::connect:  (sender name:   'pushButton')
qt.core.qobject.connect: QObject::connect:  (receiver name: 'MainWindow')

At a first glance I thought that would be the simple case of (somehow) loading the .ui file with a custom QMainWindow class that actually implements a function called clickedNewConn.

With that in mind I thought on passing a second param to QUiLoader.load function as mentioned here

class MyMainWindow(QMainWindow):
    def clickedNewConn(self):
        print("ops!")
# ...
ui_file = QFile("views/main.ui")
loader = QUiLoader()
window = loader.load(ui_file, parentWidget=MyMainWindow())

But I still got the same result. I also noticed that most of the tutorials with Python + Qt + .ui Files never mention or make use of this second param. Instead they are always talking about a python based version of the .ui file generated by a program called uic.

Does that mean the only way I can use custom slots is inheriting from the python class generated by the uic?

0 Answers
Related