英文:
Connecting a Qt5 Designer design with a slot function in my main() code without modifying the Designer file
问题
我尝试使用Qt5和面向对象编程,试图使用设计师来设计我的GUI。
我预见到一个按钮,点击后会调用一个自定义的槽函数。在Qt5 Designer中,我使用了一个自定义槽,自动设置为slot1()。我将设计保存为一个 .ui 文件,然后运行 pyuic5 以获取我的GUI的 .py 文件。
我可以运行这个 .py 文件,但它给出了一个 AttributeError,指出对象 'Ui_HomeWindow' 没有 'slot1' 属性。
这正如我所预料的,因为我没有在类中提供 slot1 函数。从Designer中我明白,在运行 pyuic5 后获得的 .py 文件不应该被修改(完全可以理解)。
我需要在我的 main() 中定义 slot1 函数,以便它被接受为由Designer生成的类的方法。
阅读了很多关于多态性和继承的内容,我完全迷失了(我是面向对象编程的新手)。
从Designer运行pyuic5后继承设计的类将允许我在自己定义的另一个类中重用方法。在那个类中,我可以定义 slot1 函数。
我卡住的地方是,通过点击一个按钮,“Designer”生成的类需要访问我类中定义的 slot1 函数。在面向对象编程的概念中,父类(由Designer生成)需要访问子类(由我定义)中定义的函数。
这是正确的吗?这是可能的吗,如何实现这一点?有人能给我一个简单的解释吗?我读到的内容总是复杂和深奥,而我不相信为了使用Qt5 Designer,一个人必须成为面向对象编程的大师。非常感谢提前的提示。
英文:
Being new in using Qt5 and Object Oriented Programming, I try to use the Designer for the design of my GUI.
I have foreseen a button that calls a custom slot once clicked. In Qt5 Designer I used a custom slot automatically set to slot1(). I save the design into an .ui file and I run pyuic5 in order to obtain a .py file for my GUI.
This .py file I can run but it gives me an AttributeError stating that the object 'Ui_HomeWindow' has no attribute 'slot1'.
This is as I expected because I did not provide the slot1 function in the class. From Designer I understand that the .py file obtained after running pyuic5 should not be modified (fully understandable).
I need to define the slot1 function in, let say, my main() so that it gets accepted as a method of the class as generated by Designer.
Having read a lot about polymorphism and inheritance I get completely lost (I am new in OOP).
Inheriting from the class defined by Designer after running pyuic5 will allow me to reuse methods in another class defined by myself. In that class I can then define the slot1 function.
Where I get stuck is that by clicking a button, the "Designer" generated class needs access to the slot1 function defined in my class. Translated in OOP concepts, the parent class (resulting from Designer) needs to access a function defined in a child class (defined by me).
Is this correct? Is this possible, and how can I make this happen? Could someone point me to a simple explanation please? What I read was always sophisticated and complex, while I cannot believe that one needs to be a guru in OOP for being able to use Qt5 Designer. Thank you very much in advance for the hints.
答案1
得分: 1
我建议您使用clicked
方法,并将按钮连接到您的类中的一个方法,就像这样:
from ui_main import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, app, force_quit=False):
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton_export.clicked.connect(self.export)
def export(self):
...
因此,我使用设计师创建了一个名为ui_main.py
的文件,其中包含Ui_MainWindow
类,其中包含一个名为export
的按钮。这保持不变,但由您开发的MainWindow
类继承,其中包含您需要的所有方法。
英文:
I suggest you use the clicked method, and connect the button to a method in your class, like this:
from ui_main import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, app, force_quit=False):
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton_export.clicked.connect(self.export)
def export(self):
...
So, I created a ui_main.py file using designer which contains the Ui_MainWindow class, which contains a button that I named export. This remains unchanged but is inherited by your MainWindow class that you develop with all the methods you need.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论