切换QAction设置QStackedWidget的当前小部件(Toggle A QAction To Set QStackedWidget's Current Widget)

编程入门 行业动态 更新时间:2024-10-26 17:32:20
切换QAction设置QStackedWidget的当前小部件(Toggle A QAction To Set QStackedWidget's Current Widget)

所以我要做的是当我按下showMenu(QAction)时,容器(QStackedWidget)将当前小部件更改为menuWidget,当我再次按下它时,它会隐藏。

好的,所以我设法得到这个代码:

connect(showMenu, SIGNAL(triggered()), map, SLOT(map())); map->setMapping(menuWidget, container); Object::connect(map, SIGNAL(mapped(QWidget *)), container, SLOT(setCurrentWidget(QWidget *)));

如果我跑:

container->setCurrentWidget(menuWidget);

直接,它工作正常,所以我没有这样搞砸。

So what I am trying to do is when I press the showMenu (QAction), the container (QStackedWidget) changes the current widget to menuWidget AND when I press it again it hides.

Ok so I have managed to get this code:

connect(showMenu, SIGNAL(triggered()), map, SLOT(map())); map->setMapping(menuWidget, container); Object::connect(map, SIGNAL(mapped(QWidget *)), container, SLOT(setCurrentWidget(QWidget *)));

also if I run:

container->setCurrentWidget(menuWidget);

directly, it works fine, so I have not messed up in that way.

最满意答案

您应该在类中创建一个显示/隐藏menuWidget 。

如果使用可检查的QAction对象,则可以使用QAction::toggled(bool checked)信号,并使用checked变量确定是否应显示或隐藏窗口小部件。

如果您没有使用可检查的QAction对象,那么您应该创建一个类型为bool的类成员变量,您可以在插槽中切换:

class MainWindow : public QMainWindow { Q_OBJECT public: ... private: bool toggleStatus; // set this to false in your constructor ... };

void MainWindow::menuToggled() { toggleStatus = !toggleStatus; if(toggleStatus) { container->setCurrentWidget(menuWidget); } else { container->setCurrentWidget(mdiContainer); } }

You should create a slot in your class where you show/hide menuWidget.

If you are using a checkable QAction object, then you can use QAction::toggled(bool checked) signal, and use the checked variable to determine if you should show or hide your widget.

If you're not using a checkable QAction object, then you should create a class member variable of type bool that you toggle in your slot:

class MainWindow : public QMainWindow { Q_OBJECT public: ... private: bool toggleStatus; // set this to false in your constructor ... };

 

void MainWindow::menuToggled() { toggleStatus = !toggleStatus; if(toggleStatus) { container->setCurrentWidget(menuWidget); } else { container->setCurrentWidget(mdiContainer); } }

更多推荐

本文发布于:2023-08-04 13:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   QStackedWidget   QAction   Toggle   Widget

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!