如何在 QT 中访问父小部件指针 [英] How to access parent widget pointer in QT
问题描述
我有一个类似这样的代码
I have a code something like this
Window::Window()
{
QStackedWidget *centralApp = new QStackedWidget;
QWidget1 *wgt1 = QWidget1;
QWidget2 *wgt2 = QWidget2;
QWidget3 *wgt3 = QWidget3;
centralApp->addWidget(wgt1);
centralApp->addWidget(wgt2);
centralApp->addWidget(wgt3);
}
QWidget1,QWidget2
和 QWidget3
类继承自 QWidget
,每个类都包含两个按钮 btn1
和 <代码>btn2.这些按钮我想使用每个小部件中的两个按钮导航到添加到堆叠小部件的其他两个小部件.因此,要导航到堆叠小部件中的其他页面,我必须使用 setCurrentIndex()
并且为此我需要父 QStackedWidget
指针.任何人都可以建议我如何访问其页面小部件内的 QStackedWidget
指针以导航到另一个页面?
The classes QWidget1,QWidget2
and QWidget3
are inherited from QWidget
and each contains two buttons btn1
and btn2
. These buttons I want to use the two buttons in each widget to navigate to other two widgets added to stacked widget. So to navigate to other page in stacked widget I have to use the setCurrentIndex()
and for this I need the parent QStackedWidget
pointer. Can anybody suggest me how I can access the QStackedWidget
pointer inside on of its page widgets to navigate to another page?
如果我在解释问题时不清楚,请告诉我.
Please let me know if I am not clear in explaining the problem.
推荐答案
我会让你的子类发出一个信号 - 例如 'next' 和 'prev' - 然后在你的主窗口中连接这个信号来切换 QStackWidget
的当前小部件.
I would have your subclasses emit a signal - 'next' and 'prev' for example - and then connect this signal in your main window to switch the QStackWidget
's current widget.
否则,您会以不必要的方式紧密耦合堆叠的小部件.
Otherwise, you're tightly coupling your stacked widgets in a way that is unnecessary.
这篇关于如何在 QT 中访问父小部件指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!