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

Inheriting QtQuick Item from C++



  • In QtQuickControls there is a QML type called Dialog. Example:

    Dialog {
        parent: ApplicationWindow.overlay
    
        x: (parent.width - width) / 2
        y: (parent.height - height) / 4
        width: Math.min(parent.width, parent.height) / 3 * 2
        modal: true
        focus: true
        standardButtons: Dialog.Yes | Dialog.No
        }
    }
    

    I want to inherit this class and create my own buttons. Example "customButtons: Dialog.Button1 | Dialog.Button2"

    I have to do this in C++ since I must use enums. I can create flags like "Dialog.Ok" but I could not merge them with Dialog.

    My goal is changing this type to something I want. I dont want to write buttons explicitly like this everytime I use this type.

    CustomDialog {
            id: customDialog
    
            title: "Dialog Title"
    
            footer: DialogButtonBox {
                Button {
                    text: qsTr("Button1")
                    DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
                }
                Button {
                    text: qsTr("Button2")
                    DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
                }
            }
    }
    


  • @maydin said in Inheriting QtQuick Item from C++:

    customButtons

    Hi,
    Please look at this for creating reusable custom buttons in QML.
    https://www.ics.com/blog/creating-qml-controls-scratch


Log in to reply