Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Pass parameters from action to other QML object?
Qt 6.12 LTS is out! See what's new in the release blog

Pass parameters from action to other QML object?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 641 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pokemonsrqt
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      @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")
      }
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved