I'm using a qstackedwidget in my MainWindow class and I'm defining the widgets in another class (PageLayouts) in order to keep things seperate. But I'm doing the connections between the objects in the MainWindow class (change pages...), which fails. Basically:
//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()
I suppose I do not get the PushButtons via the findchild methods properly. So my questions are:
Thanks, Sebastian
Edit 1:
Here's the code which generates Page1 in "pagelayouts.h":
class PageLayouts
{
QWidget * Page1;
QWidget * Page2;
public:
PageLayouts();
//Get
QWidget * getPage1();
....
And the code which generates page1 in "pagelayouts.cpp":
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;
}
You can see it's experimental code, but I'm testing qstackedwidgets for further design decisions and hope someone can solve this issue.
You are passing "Btn_ToPage2" as a name to QObject::findChild()
. The name of the variable is Btn_ToPage2
, but you are not setting an object name:
Btn_ToPage2->setObjectName("Btn_ToPage2");