有几种方法可以做到这一点,它们更简洁。一种方法是您可以更改向导以获取指向 MyMainWindow 类的指针。然后你可以更干净地进行连接。
class Page : public QWizardPage
{
public:
Page(MyMainWindow *mainWindow, QWidget *parent) : QWizardPage(parent)
{
if(mainWindow)
{
connect(button, SIGNAL(clicked()), mainWindow, SLOT(launchWidgetOne()));
} else
{
qDebug() << "Super informative debug message";
}
}
// other members, etc
};
一个更简单的设计是向上传播信号。毕竟,如果单击该按钮对父级很重要,则让父级处理它:
class Page : public QWizardPage
{
public:
Page(QWidget *parent) : QWizardPage(parent)
{
connect(button, SIGNAL(clicked()), this, SIGNAL(launchWidgetOneRequested()));
}
signals:
void launchWidgetOneRequested();
};
void MyMainWindow::showWizard() // or wherever you launch the wizard
{
Page *p = new Page;
QWizard w;
w.addPage(p);
connect(p, SIGNAL(launchWidgetOneRequested()), this, SLOT(launchWidgetOne()));
w.show();
}
我强烈推荐第二种方法,因为它减少了孩子需要知道父母细节的耦合。