多个 QWidets 到一个 QMainWindow 中
Multiple QWidets into one QMainWindow
我目前正在研究Qt,我被困在使用多个QWidgets和一个QMainWindow的问题上。
我已经设置了一个包含2个QWidgets和一个QMainWindow的项目。这是我使用它的想法:根据需要设计两个QWidgets,将它们添加到主窗口对象,将按钮连接到正确的插槽,并在需要时切换中心小部件。所以我从一个QMainWindow开始,然后添加两个QWidgets,包括cpp文件,h文件和ui文件。在两个QWidgets上,我都添加了一个QPushButton,并称之为pushButtonConvert。
然后我转到附加到QMainWindow(主窗口.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.
您可以将pushButtonConvert
按钮连接到 MainWindow
的构造函数中的MainWindow::convertFromEpochToHuman
,使用:
connect(epochToHuman->ui->pushButtonConvert, SIGNAL(clicked(bool)), this, SLOT(convertFromEpochToHuman()));
您需要先公开ui
成员,就像您为HumanToEpoch
所做的那样。
您应该将小部件的声明移动到MainWindow.h
:
// ...
private:
Ui::MainWindow *ui;
EpochToHuman * epochToHuman;
HumanToEpoch * humanToEpoch;
// ...
并像这样初始化它们:
epochToHuman = new EpochToHuman(this);
humanToEpoch = new HumanToEpoch(this);
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 如何从另一个类编辑 QMainWindow UI 小部件?
- 如何使用OpenGL渲染到一个QMainWindow的两个QWidget?
- 是否可以制作一个未经装饰的“QMainWindow”
- 将QMainWindow或Widget添加到另一个Widget-访问成员
- 创建一个实现选项卡并可用作 QMainWindow 中的"central widget"的类
- 从QMainWindow和Ui_MainWindow继承一个MainWindow类
- 将当前QWidget替换为QMainWindow中的另一个QWidget
- 多个 QWidets 到一个 QMainWindow 中
- 将多个QMainWindow合并为QTabWidget中的一个选项卡