binding SetProperty with subproperty

Viewed 150

I'm currently developed a GUI application with QML/Python.

My problem is the next : I use SetProperty to fix some QML object properties, but for some properties, there is sub properties, and so far, I was not successful to finding the correct syntax of SetProperty with this kind of stuff.

To be more precise, here is a sample of my code :

in my main.qml :

Button {
    id: request_copy
    objectName: "request_copy"
    text: "Copier la requête"
    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
    onClicked: ATRCReport.request_to_clipboard(search_type.currentText)
    ToolTip.visible: hovered
    ToolTip.text: "Request copied"
}

My python code :

@pyqtSlot(QVariant)     
def request_to_clipboard(self, selected_request):
    if selected_request is "":
        self.no_search_type_dialog.open()
    else:
        clipboard.copy(selected_request)
        self.request_copy.setProperty("ToolTip.visible", "released")

My goal in this precise exemple is to show a tooltip after an action. But so far, I tried many syntaxes, but nothing work. I found nothind in the online doc of PySide2 etc.

1 Answers

don't modify anything from QML directly from python, just return a flag telling QML what to do. On the other hand the dynamic binding Qt.binding can be used:

@pyqtSlot(str, result=bool)     
def request_to_clipboard(self, selected_request):
    if selected_request:
        clipboard.copy(selected_request)
        return True
    self.no_search_type_dialog.open()
    return False
Button {
    id: request_copy
    text: "Copier la requête"
    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
    onClicked: {
        if(ATRCReport.request_to_clipboard(search_type.currentText)){
            ToolTip.visible = Qt.binding(function(){ return request_copy.released })
        }
    }
    ToolTip.visible: hovered
    ToolTip.text: "Request copied"
}

Another more elegant solution is to use a property (my python code is not modified):

Button {
    id: request_copy
    property bool flag: true
    text: "Copier la requête"
    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
    onClicked: {
        if(ATRCReport.request_to_clipboard(search_type.currentText)){
            flag = false
        }
    }
    ToolTip.visible: flag ? hovered: released
    ToolTip.text: "Request copied"
}
Related