【发布时间】:2016-08-23 01:57:58
【问题描述】:
我在我的 MainWindow 类中使用了 qstackedwidget,并且我在另一个类 (PageLayouts) 中定义了小部件,以保持独立。但是我正在 MainWindow 类中的对象之间进行连接(更改页面...),但失败了。基本上:
//mainwindow.cpp
....
QSignalMapper * SM = new QSignalMapper();
Layouter = new PageLayouts();
//Instance of the class which contains the widgets
QStackedWidget * Stack = new QStackedWidget();
Stack->addWidget(Layouter->getPage1());
connect(SM, SIGNAL(mapped(int)), Stack, SLOT(setCurrentIndex(int)));
SM->setMapping(Stack->widget(0)->findChild<QPushButton *>("Btn_ToPage2"), 1);
connect(Stack->widget(0)->findChild<QPushButton *>("Btn_ToPage2"), SIGNAL(clicked(bool)), SM, SLOT(map()));
....
//Debugging says to the first connect:
QObject::connect: Cannot connect (null)::destroyed() to QSignalMapper::_q_senderDestroyed()
//To the second:
QObject::connect: Cannot connect (null)::clicked(bool) to QSignalMapper::map()
我想我没有通过 findchild 方法正确获取 PushButtons。 所以我的问题是:
- 如何在stackedwidget 中输入widget 的子级?
- 既然我还在用stackedwidgets做实验,有没有更优雅的使用方式?
谢谢,塞巴斯蒂安
编辑 1:
这是在“pagelayouts.h”中生成 Page1 的代码:
class PageLayouts
{
QWidget * Page1;
QWidget * Page2;
public:
PageLayouts();
//Get
QWidget * getPage1();
....
以及在“pagelayouts.cpp”中生成page1的代码:
PageLayouts::PageLayouts()
{
//Page1
QHBoxLayout * HL_112 = new QHBoxLayout();
QVBoxLayout * VL_122 = new QVBoxLayout();
QLabel * Lbl_NamePage1 = new QLabel("Page 1");
QPushButton * Btn_ToPage2 = new QPushButton("Page 2");
QPushButton * Btn_Close1 = new QPushButton("Close");
//Assembling of Page1
VL_122->addWidget(Btn_ToPage2);
VL_122->addWidget(Btn_Close1);
HL_112->addWidget(Lbl_NamePage1);
HL_112->addLayout(VL_122);
Page1 = new QWidget();
Page1->setLayout(HL_112);
//Page2
....
}
QWidget* PageLayouts::getPage1()
{
return Page1;
}
您可以看到它是实验性代码,但我正在测试 qstackedwidgets 以做出进一步的设计决策,并希望有人能解决这个问题。
【问题讨论】:
-
你能展示一下创建page1及其按钮的代码吗?
-
好的,刚刚编辑好了。
标签: qt qstackedwidget