我目前正在学习 Qt,但我被困在使用多个 QWidget 和一个 QMainWindow 的问题上。
我已经设置了一个包含 2 个 QWidgets 和一个 QMainWindow 的项目。这是我使用它的想法:根据需要设计两个 QWidget,将它们添加到主窗口对象,将按钮连接到正确的插槽并在需要时切换中心小部件。所以我从一个 QMainWindow 开始,然后添加了两个 QWidget,包括 cpp 文件、h 文件和 ui 文件。在两个 QWidgets 上,我添加了一个 QPushButton,并将其命名为 pushButtonConvert。
然后我转到附加到 QMainWindow (mainwindow.cpp) 的 cpp 文件并执行以下操作:
EpochToHuman * epochToHuman = new EpochToHuman();
HumanToEpoch * humanToEpoch = new HumanToEpoch();
到目前为止,一切都很好。现在我想将按钮连接到主窗口对象中的插槽,但我找不到按钮。epochToHuman->pushButtonConvert 似乎不存在,我找不到任何其他方式来获取按钮。那么,根据 Qt,我是否以一种不正确的方式思考,或者我错过了什么?
澄清我想要的另一个尝试:我想在 QMainWindows 的 cpp 文件中使用 QWidget 中的元素。我希望能够做这样的事情:
//In object MainWindow.cpp
QWidget * a = new QWidget
//Let's say a is a custom widget with a label in it. This label is called Label
a->Label->setText("Hello, World!");
//This gives an error because a does not have a member called Label
//How can I change the text on the label of a?
//And I think if I will be able to change the text of this label, I will also be able to dance around with buttons as needed.