Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pass parameters from action to other QML object?



  • I have three actions

    Action {
        id: actionOpenProject
        text: qsTr("Open Project...")
        shortcut: StandardKey.Open
        onTriggered: requestLoadProject()
        enabled: !progressBar.visible
    }
    
    Action {
        id: actionImportProject
        text: qsTr("Import Project...")
        enabled: projectView.visible && !progressBar.visible
        onTriggered: importProjectDialog.open()
    }
    
    Action {
        id: actionSaveProject
        text: qsTr("Save Project")
        shortcut: StandardKey.Save
        enabled: projectView.visible && !progressBar.visible
        onTriggered: core.projectPath.toString() !== "" ? core.save() : saveProjectDialog.open()
    }
    

    Each action opens a similar QML FileDialog.

    FileDialog {
        id: openProjectDialog
        title: qsTr("Open " + capitalizedAppName() + " project")
        folder: core.projectFolder.toString() !== "" ? core.projectFolder : shortcuts.home
        nameFilters: Qt.platform.os === "linux" ?
                         [ "All files (*)", capitalizedAppName() + " project (*." + Qt.application.name.toLowerCase() + ")" ]
                       : [ capitalizedAppName() + " project (*." + Qt.application.name.toLowerCase() + ")", "All files (*)" ]
        onAccepted: core.load(Qt.resolvedUrl(openProjectDialog.fileUrl).toString())
    }
    
    FileDialog {
        id: importProjectDialog
        title: qsTr("Import Project")
        folder: core.projectFolder.toString() !== "" ? core.projectFolder : shortcuts.home
        nameFilters: Qt.platform.os === "linux" ?
                         [ "All files (*)", capitalizedAppName() + " project (*." + Qt.application.name.toLowerCase() + ")" ]
                       : [ capitalizedAppName() + " project (*." + Qt.application.name.toLowerCase() + ")", "All files (*)" ]
        onAccepted: core.importProject(Qt.resolvedUrl(importProjectDialog.fileUrl).toString())
    }
    
    FileDialog {
        id: saveProjectDialog
        title: qsTr("Save Project As")
        folder: core.projectFolder.toString() !== "" ? core.projectFolder : shortcuts.home
        selectExisting: false
        nameFilters: Qt.platform.os === "linux" ?
                         [ "All files (*)", capitalizedAppName() + " project (*." + Qt.application.name.toLowerCase() + ")" ]
                       : [ capitalizedAppName() + " project (*." + Qt.application.name.toLowerCase() + ")", "All files (*)" ]
        onAccepted: {
            core.projectPath = Qt.resolvedUrl(saveProjectDialog.fileUrl).toString()
            core.save()
        }
    }
    

    This leads to some duplicated code. What is a good way to avoid this duplication of code?


  • Qt Champions 2017

    @pokemonsrqt said in Pass parameters from action to other QML object?:

    saveProjectDialog

    Can you used only one filedialog object & use it every where ? What is the difficulty you have ?

    FileDialog {
        id: saveProjectDialog
        title: qsTr("Save Project As")
    }
    

Log in to reply