QtCreator does not want to run an application that I wrote a few months ago on another computer. Interestingly, the application itself works OK if I run it directly from the disk, or e.g. via PyCharm. Does anyone know what the reason could be?

Minimal (non)working example:

main.py:

import sys
from pathlib import Path

from PySide6.QtCore import QObject, Signal, Property
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
import os


class PyBackground(QObject):
    def __init__(self):
        QObject.__init__(self)
        self.d1 = {'0': 'zero', '1': 'one', '2': 'two'}


    property_d1_changed = Signal()

    @Property('QVariant', notify=property_d1_changed)
    def property_d1(self):
        return self.d1

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    py_background = PyBackground()
    engine.rootContext().setContextProperty("py_background", py_background)
    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml")),
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())

main.qml:

import QtQuick
import QtQuick.Controls
import QtQuick.Window


Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    property var d1: py_background.property_d1

    Text {
        id: name
        text: qsTr("text: " + d1['2'])
        x: 20
        y: 20
    }
}

error message (Compile Output):

Error parsing C:\DANE\QtProjects\test1\main.py: 'Constant' object has no attribute 'id'
Traceback (most recent call last):
  File "C:\Users\danie\AppData\Roaming\Python\Python311\site-packages\PySide6\scripts\metaobjectdump.py", line 428, in <module>
    json_data = parse_file(file, context, args.suppress_file)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danie\AppData\Roaming\Python\Python311\site-packages\PySide6\scripts\metaobjectdump.py", line 388, in parse_file
    visitor.visit(ast_tree)
  File "C:\Program Files\Python311\Lib\ast.py", line 418, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\ast.py", line 426, in generic_visit
    self.visit(item)
  File "C:\Program Files\Python311\Lib\ast.py", line 418, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "C:\Users\danie\AppData\Roaming\Python\Python311\site-packages\PySide6\scripts\metaobjectdump.py", line 220, in visit_ClassDef
    self.visit(b)
  File "C:\Program Files\Python311\Lib\ast.py", line 418, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "C:\Users\danie\AppData\Roaming\Python\Python311\site-packages\PySide6\scripts\metaobjectdump.py", line 238, in visit_FunctionDef
    self._parse_function_decorator(node.name, d)
  File "C:\Users\danie\AppData\Roaming\Python\Python311\site-packages\PySide6\scripts\metaobjectdump.py", line 315, in _parse_function_decorator
    type = _python_to_cpp_type(_name(node.args[0]))
                               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danie\AppData\Roaming\Python\Python311\site-packages\PySide6\scripts\metaobjectdump.py", line 71, in _name
    return node.id
           ^^^^^^^
AttributeError: 'Constant' object has no attribute 'id'
JSONDecodeError: running pyside6-metaobjectdump on C:\DANE\QtProjects\test1\main.py: Expecting value: line 1 column 1 (char 0)
15:16:02: The process "C:\Users\danie\AppData\Roaming\Python\Python311\Scripts\pyside6-project.exe" exited with code 1.
Error while building/deploying project test1 (kit: Desktop Qt 6.5.3 MinGW 64-bit)
When executing step "Run PySide6 project tool"
15:16:02: Elapsed time: 00:01.

It seems that there is some problem with properties, especially when I want to send python dictionary to qml. Above code is working when run directly from explorer.

INFO:

  • Python 3.11.6
  • PySide 6.5.3
  • Qt 6.5.3
  • QtCreator 11.0.3
  • Windows 10 Home, 22H2, x64
  • laptop HP Omen 15-en0xxx, 16GB RAM, GTX1650Ti (fresh format)

EDIT: .pyproject file:

{
    "files": [
        "main.py",
        "main.qml"
    ]
}
1

There are 1 answers

0
relent95 On

It's a bug of PySide6. Why not report this bug? (See Qt for Python/Reporting Bugs.)

The current implementation assumes the first argument of the Property decorator is a Python type, as you can see in the following.

...
def _name(node: Union[ast.Name, ast.Attribute]) -> str:
    """Return the name of something that is either an attribute or a name,
       such as base classes or call.func"""
    if isinstance(node, ast.Attribute):
        qualifier, name = _attribute(node)
        return f"{qualifier}.{node.attr}"
    return node.id
...
class MetaObjectDumpVisitor(ast.NodeVisitor):
    ...
    def _parse_function_decorator(self, func_name: str, node: AstDecorator):
        ...
        if isinstance(node, ast.Call):
            name = _name(node.func)
            if name == "Property":  # Property getter
                if node.args:  # 1st is type
                    type = _python_to_cpp_type(_name(node.args[0]))
        ...