简体   繁体   English

Qt:如何将类连接到自定义Qt Designer小部件

[英]Qt: How to connect class to custom Qt Designer Widget

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). 我已经在Qt Creator中使用Designer文件创建了一个新的窗口小部件(选择了Widget模板,该模板为我的自定义窗口小部件类生成了源文件和头文件,还生成了一个设计器文件)。

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! 谢谢!

A few things: 一些东西:

  • In designer, each of your widgets (the QPushButton, and the QLabel in your case) has a name assigned to it. 在设计器中,为每个小部件(您的情况下的QPushButton和QLabel)分配了一个名称。 This name is the name of the variable that you can use in C++ to reference that widget and call functions on it. 此名称是您可以在C ++中用来引用该小部件并在其上调用函数的变量的名称。

  • 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()) 如果您的类继承自Ui :: MyCustomwidget,则您的变量只是该类的成员变量,可以随时访问(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()) 如果您有一个成员变量(通常名为ui,类型为Ui :: MyCustomWidget),则可以使用ui对象(ui-> myLabel-> setText())访问您的窗口小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM