Search code examples
c++qtqmlqtquick2qtquickcontrols

QML : Link MenuBar and ToolBar actions


I'm quite new to Qt Quick (and Qt in general), and i'd like to have an advice on "good way" to do this.

In an application, if I have a menubar and toolbar that have common actions, is there a way to link the buttons from menubar and buttons from toolbar ?

For instance, if I have a "save" function. This action is avaible through menubar and toolbar. How can I mutualise this action ?

At the moment, the best way i've found is to create a function "save" that is called by both buttons.


Solution

  • I've actually found a "good practice" for this problem on QML example : use Action items.

    For instance :

    FileDialog {
        id: openDialog
        onAccepted: myData.source= fileUrl
    }
    
    Action {
        id: openFile
        iconSource: "images/fileopen.png"
        text: "Open"
        onTriggered: openDialog.open()
    }
    
    menuBar: MenuBar {
        Menu {
           MenuItem { action : openFile }
    
    // ....
    
    toolBar : ToolBar {
            ToolButton { action:openFile}