2014-12-04 6 views
0

qmenu에서 툴바를 보이게하고 숨기려면 토글을 추가하는 방법은 무엇입니까? 이 내 코드 :Qtoolbar는 Qmenu에서 show hide를 토글합니다.

#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

다음 작은 예는 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 감사 .. –