Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Action-s in separete file
Forum Updated to NodeBB v4.3 + New Features

Action-s in separete file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 196 Views 1 Watching
  • 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.
  • qtprogrammer123Q Offline
    qtprogrammer123Q Offline
    qtprogrammer123
    wrote on last edited by
    #1

    Hi,
    I'm trying place actions is separete file Act.qml, after that i add Act{ id: actions} in main.qml, but they dont work.

    Act.qml

    import QtQuick 2.5
    import QtQuick.Controls 2.15
    
    Item {
    
        Action {
            id: copy
            text: qsTr("&Copy")
            icon.name: "edit-copy"
            shortcut: StandardKey.Copy
            onTriggered: window.activeFocusItem.copy()
        }
        Action {
            id: paste
            text: qsTr("&Paste")
            icon.name: "edit-paste"
            shortcut: StandardKey.Paste
            onTriggered: window.activeFocusItem.paste()
        }
        Action {
            id: cut
            text: qsTr("&Cut")
            icon.name: "edit-cut"
            shortcut: StandardKey.Cut
            onTriggered: window.activeFocusItem.cut()
        }
    }
    

    main.qml

    Act {
        id: act
    }
    

    Try use in TextArea

      ScrollView {
    
                            height: parent.height
                            width: parent.width
                            TextArea {
                                selectByMouse: true
                                font.pixelSize: 20
                                id: box
                                readOnly: true
                                wrapMode: Text.WordWrap
                                persistentSelection: true
                                MouseArea {
    
                                    anchors.fill: parent
                                    acceptedButtons: Qt.RightButton
                                    preventStealing: true
                                    onClicked: {
                                        switch (mouse.button) {
                                        case Qt.RightButton:
                                            men.open()
                                            break
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Menu {
                        id: men
                        MenuItem {
                            text: "copy"
                            id: menuItem
                            action: act.copy
                        }
    //rest of actions
                    }
    

    Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

    jeremy_kJ 1 Reply Last reply
    0
    • qtprogrammer123Q qtprogrammer123

      Hi,
      I'm trying place actions is separete file Act.qml, after that i add Act{ id: actions} in main.qml, but they dont work.

      Act.qml

      import QtQuick 2.5
      import QtQuick.Controls 2.15
      
      Item {
      
          Action {
              id: copy
              text: qsTr("&Copy")
              icon.name: "edit-copy"
              shortcut: StandardKey.Copy
              onTriggered: window.activeFocusItem.copy()
          }
          Action {
              id: paste
              text: qsTr("&Paste")
              icon.name: "edit-paste"
              shortcut: StandardKey.Paste
              onTriggered: window.activeFocusItem.paste()
          }
          Action {
              id: cut
              text: qsTr("&Cut")
              icon.name: "edit-cut"
              shortcut: StandardKey.Cut
              onTriggered: window.activeFocusItem.cut()
          }
      }
      

      main.qml

      Act {
          id: act
      }
      

      Try use in TextArea

        ScrollView {
      
                              height: parent.height
                              width: parent.width
                              TextArea {
                                  selectByMouse: true
                                  font.pixelSize: 20
                                  id: box
                                  readOnly: true
                                  wrapMode: Text.WordWrap
                                  persistentSelection: true
                                  MouseArea {
      
                                      anchors.fill: parent
                                      acceptedButtons: Qt.RightButton
                                      preventStealing: true
                                      onClicked: {
                                          switch (mouse.button) {
                                          case Qt.RightButton:
                                              men.open()
                                              break
                                          }
                                      }
                                  }
                              }
                          }
                      }
                      Menu {
                          id: men
                          MenuItem {
                              text: "copy"
                              id: menuItem
                              action: act.copy
                          }
      //rest of actions
                      }
      
      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      @qtprogrammer123 said in Action-s in separete file:

      Hi,
      I'm trying place actions is separete file Act.qml, after that i add Act{ id: actions} in main.qml, but they dont work.

      Hi,

      It's helpful to have a description of what not working means. A compiler or interpreter error message, a screen capture, or something to differentiate the expected from the actual outcome.

      Act.qml

      Item {
       ...
          Action {
              id: copy
         }
      }
      

      main.qml:

      Act {
          id: act
      }
      
      Menu {
           id: men
          MenuItem {
              text: "copy"
              id: menuItem
              action: act.copy
          }
      //rest of actions
      }
      

      I may be leaping to conclusions, but it looks like this is attempting to use an id from Act.qml in main.qml. That won't work. The id property of items should only used within the same file.

      The cleanest solution is to export each of the actions as a property of Act.qml. Eg
      Act.qml:

      Item {
          property Action copy: Action {
              text: qsTr("&Copy")
          }
          property Action paste: Action {
              text: qsTr("&Paste")
          }
      }
      

      main.qml:

      Window {
          Act { id: act }
          MenuItem {
              action: act.copy
          }
      }
      

      It's also possible to access children of a top level item via the children, data, or resources properties of an Item. Finding a particular instance using these tends to require more code and more effort to maintain. They are useful for handling all children.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      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 Extensions
      • Unsolved