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

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.

    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
                    }
    


  • @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.


Log in to reply