Qt:如何将类连接到自定义 Qt Designer Widget
也许我的想法完全错误...
我在 Qt Creator 中使用设计器文件创建了一个新的小部件(我选择了小部件模板,它为我的自定义小部件类生成了源文件和头文件,还有一个设计器文件)。
然后我用设计器设计了这个小部件。我现在可以创建此小部件的实例,它将显示在我的应用程序中。
但它并不是很有用,因为我不知道如何在运行时自定义小部件。
假设小部件中只有一个标签和一个按钮。在运行时,如何更改该标签的文本?我不知道如何将设计器的东西连接到我的实际类,并且我找不到任何有关如何执行此操作的文档。我错过了什么吗?
谢谢!
Maybe I'm thinking about this completely wrong...
I've created a new widget in Qt Creator with a Designer file (I picked the Widget template, which generated a source and header file for my custom widget class, and also a designer file).
I then designed the widget with the Designer. I can now create instances of this widget and it will show up in my app.
But it's not terribly useful because I don't know how to customize the widget at runtime.
Let's say all I've got in the widget is a Label and a Button. At runtime, how can I change the text of this label? I can't figure out how to connect the designer stuff to my actual class, and I can't find any documentation on how to do this. Am I missing something?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些事情:
在设计器中,每个小部件(QPushButton 和您的情况下的 QLabel)都分配有一个名称。该名称是变量的名称,您可以在 C++ 中使用该变量来引用该小部件并调用其上的函数。
根据您的自定义小部件的实现方式,您将能够使用以下两种方法之一引用这些变量:
如果您的类继承自 Ui::MyCustomwidget,那么您的变量只是类的成员变量,可以随时访问 (myLabel->setText())
如果您有一个成员变量(通常名为 ui,类型为 Ui::MyCustomWidget),那么您可以使用 ui 对象访问您的小部件 (ui->myLabel->setText())
A few things:
In designer, each of your widgets (the QPushButton, and the QLabel in your case) has a name assigned to it. This name is the name of the variable that you can use in C++ to reference that widget and call functions on it.
Depending on how your custom widget was implemented, you will be able to reference these variables using one of two methods:
If your class inherits from Ui::MyCustomwidget, then your variables are simply member variables of your class and can be accessed at any time (myLabel->setText())
If you have a member variable (generally named ui, of type Ui::MyCustomWidget), then you can access your widgets using the ui object (ui->myLabel->setText())