2014-12-04 212 views
0

如何添加插槽来切换显示并隐藏qmenu中的工具栏?这是我的代码:Qtoolbar切换显示隐藏在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编程真正的新),你能不能给我的示例代码?我试过Google搜索,但没有找到它的用法的例子。

回答

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

非常感谢您@vahancho .. – 2014-12-04 14:58:17