Search code examples
pyqtqmlqt-quickqtquick2

How to set property type of qml signal?


I am learning qml,quick and pyqt5 and write a small test script.

In this script, I want to drop something on my UI and print the url of it.

test.qml

import QtQuick 2.3

Rectangle {
    id : root
    signal clicked(int x, int y)
    signal filedroped(list url)
    width: 800
    height: 450

    MouseArea {
        anchors.fill: parent
        onClicked: {
            parent.clicked(mouseX, mouseY)
        }
        DropArea {
            anchors.fill: parent
            onDropped: {
            root.filedroped(drop.urls)
            }
        } 
    }
}

The doc says:Any of the QML Basic Types aside from the enumeration type can be used as custom property types.

But I got error like this in signal filedroped:

Invalid signal parameter type: list

Also, I have tried urllist and string.
urllist failed and string works.

What's wrong with my script?

EDIT

Since I use qml with pyqt, I do not want to use the type var.
With var, I'll got a QJSValue object instead of basic type of python in my python script.
Why qml performs different with the official document? Is the document wrong?


Solution

  • It seems on there's indeed an error in the Qt Documentation. It is said (here) that

    the allowed parameter types [for signal parameters] are the same as those listed under Defining Property Attributes on this page.

    Yet one can define a property as follow:

    property list<Item> items
    

    whereas this is invalid:

    signal mysignal(list<Item> items)
    

    But anyway, the QML list type was not a solution. The official documentation is quite clear:

    A list can only store QML objects, and cannot contain any basic type values. (To store basic types within a list, use the var type instead.).

    In other words you can't use list to store strings, url, int. You have to use var. Another solution would be to use a formatted string with a custom separator instead of your url list, and split it on the Python side.