【问题标题】:FindChild Method with StackedWidget fails带有 StackedWidget 的 FindChild 方法失​​败
【发布时间】: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。 所以我的问题是:

  1. 如何在stackedwidget 中输入widget 的子级?
  2. 既然我还在用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


【解决方案1】:

您将“Btn_ToPage2”作为名称传递给QObject::findChild()。变量名是Btn_ToPage2,但是你没有设置对象名:

Btn_ToPage2->setObjectName("Btn_ToPage2");

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2016-09-04
    • 2020-05-31
    • 2020-06-27
    相关资源
    最近更新 更多