如何添加插槽以切换显示并隐藏qmenu中的工具栏?这是我的代码:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMinimumSize(800, 600);
CreateAct();
CreateMenus();
createToolBars();
}
void MainWindow::CreateAct()
{
undoAct = new QAction(QIcon::fromTheme("edit-undo", QIcon(":/images/undo.png")), tr("&Undo"), this);
redoAct = new QAction(QIcon::fromTheme("edit-redo", QIcon(":/images/redo.png")), tr("&Redo"), this);
cutAct = new QAction(QIcon::fromTheme("edit-cut", QIcon(":/images/cut.png")), tr("Cu&t"), this);
copyAct = new QAction(QIcon::fromTheme("edit-copy", QIcon(":/images/copy.png")), tr("&Copy"), this);
pasteAct = new QAction(QIcon::fromTheme("edit-paste", QIcon(":/images/paste.png")), tr("&Paste"), this);
editToolBarAct = new QAction(tr("Show edit toolbar"), this);
editToolBarAct->setCheckable(true);
editToolBarAct->setChecked(true);
// connect(editToolBarAct, SIGNAL(toggled(bool)), editToolBar, SLOT());
fileToolBarAct = new QAction(tr("Show file toolbar"), this);
fileToolBarAct->setCheckable(true);
fileToolBarAct->setChecked(true);
// connect(fileToolBarAct, SIGNAL(toggled(bool)), fileToolBar, SLOT());
}
void MainWindow::CreateMenus()
{
windowMenu = menuBar()->addMenu(tr("&Window"));
windowMenu->addAction(fileToolBarAct);
windowMenu->addAction(editToolBarAct);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar("file");
fileToolBar->addAction(undoAct);
fileToolBar->addAction(redoAct);
fileToolBar->toggleViewAction();
fileToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
editToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
}
MainWindow::~MainWindow() {}
我知道使用toggleViewAction,但是如何使用该代码(我在qt编程中真的很新),你能给我样例代码吗?我试过谷歌搜索,但没有找到它的使用示例。
答案 0 :(得分:0)
以下小例子演示了如何使用QToolBar::toggleViewAction()
:
class MainWindow : public QMainWindow
{
public:
MainWindow()
{
// Create a tool bar
QToolBar *tb = addToolBar("My Tool Bar");
[..]
// Create a menu and add toggle action for the tool bar.
QAction *tba = tb->toggleViewAction();
QMenu *m = menuBar()->addMenu("&Window");
m->addAction(tba);
}
};