Search code examples
c++qtcontextmenutoolbar

How to lock all toolbars via menu in Qt?


I am experimenting with Qt Creator and the Application Example.

I would like to add a checkable menu entry to a toolbar menu that reads "Lock toolbars" and, when checked, locks the positions of all tool bars. I guess that it is a quite common feature.

I have managed to find a command that locks single bars via :

toolBar->setMovable(false);

But I cannot figure out how to lock all toolbars.

Edit

This question used to contain an inquiry concerning the toolbar context menu rather than the standard menu. Since I got an answer concerning the context menu elsewhere I removed it from this question.

How to add an entry to toolbar context menu in qt?


Solution

  • Here is an example of how you can achieve it. First, add a QAction and a QMenu; also, declare all your toolbars private :

    private:
        QMenu*     m_pLockMenu;
    
        QToolBar*  m_pFileToolBar;
        QToolBar*  m_pEditToolBar;
        QToolBar*  m_pHelpToolBar;
    
        QAction*   m_pLockAction;
    

    Also, declare a slot where you will manage the locking of your toolbars when the action will be triggered :

    public slots :
        void lockActionTriggered();
    

    Implement your slot. You just need to lock all the toolbars :

    void lockActionTriggered()
    {
        m_pFileToolBar->setMovable(false);
        m_pEditToolbar->setMovable(false);
        m_pHelpToolBar->setMovable(false);
    }
    

    Now, you just have to declare your main window in your .cpp, and add the menu, the toolbars and the action in it :

    QMainWindow* mainWindow = new QMainWindow();
    
    m_pLockMenu = mainWindow->menuBar()->addMenu("Lock Toolbars");
    
    m_pFileToolBar = mainWindow->addToolBar("File");
    m_pEditToolBar = mainWindow->addToolBar("Edit");
    m_pHelpToolBar = mainWindow->addToolBar("Help");
    
    m_pLockAction = new QAction("Lock", this);
    

    Now, add the action to the menu :

    m_pLockMenu->addAction(m_pLockAction);
    

    And connect the QAction's signal triggered() to your slot :

    connect(m_pLockAction, SIGNAL(triggered()), this, SLOT(lockActionTriggered()));
    

    Don't forget to show() your main window :

    mainWindow->show();
    

    And it should be working now!


    EDIT

    Your code must look like this :

    In the mainwindow.h :

    class MainWindow : public QMainWindow
    {
    
    ...
    
    private:
        QMenu*     m_pLockMenu;
    
        QToolBar*  m_pFileToolBar;
        QToolBar*  m_pEditToolBar;
        QToolBar*  m_pHelpToolBar;
    
        QAction*   m_pLockAction;
    
    public slots :
        void lockActionTriggered();
    };
    

    In the main.cpp:

    int main(int argc, char *argv[])
    {
    
        ...
        QApplication app(argc, argv);
        MainWindow window;
        window.show();
    
        app.exec();
    
    }
    

    In the mainwindow.cpp:

    void MainWindow::createActions()
    {
        m_pLockMenu = menuBar()->addMenu("Lock Toolbars");
    
        m_pFileToolBar = addToolBar("File");
        m_pEditToolBar = addToolBar("Edit");
        m_pHelpToolBar = addToolBar("Help");
    
        m_pLockAction = new QAction("Lock", this);
    
        m_pLockMenu->addAction(m_pLockAction);
    
        connect(m_pLockAction, SIGNAL(triggered()), this, SLOT(lockActionTriggered()));
    
        ...
    }
    
    void MainWindow::lockActionTriggered()
    {
        m_pFileToolBar->setMovable(false);
        m_pEditToolbar->setMovable(false);
        m_pHelpToolBar->setMovable(false);
    }