所以我要做的是当我按下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); } }
更多推荐
发布评论