如何在QT中访问父控件指针

发布于 2024-10-07 19:09:33 字数 659 浏览 0 评论 0原文

我有这样的代码

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,QWidget2QWidget3 继承自 QWidget,每个类都包含两个按钮 btn1和<代码>btn2。这些按钮我想使用每个小部件中的两个按钮导航到添加到堆叠小部件中的其他两个小部件。因此,要导航到堆叠小部件中的其他页面,我必须使用 setCurrentIndex() ,为此我需要父 QStackedWidget 指针。有人可以建议我如何访问其页面小部件内的 QStackedWidget 指针以导航到另一个页面吗?

如果我没有清楚地解释问题,请告诉我。

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);
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-10-14 19:09:33

我会让你的子类发出一个信号 - 例如“下一个”和“上一个” - 然后在主窗口中连接这个信号来切换 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.

紫轩蝶泪 2024-10-14 19:09:33

http://doc.qt.io/archives/4.6/qwidget.html#parentWidget

(centralApp==wgt1->parentWidget()) //true

http://doc.qt.io/archives/4.6/qwidget.html#parentWidget

(centralApp==wgt1->parentWidget()) //true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文