Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
QML Python中的自定义对象引用_Python_Qml_Pyside2 - Fatal编程技术网

QML Python中的自定义对象引用

QML Python中的自定义对象引用,python,qml,pyside2,Python,Qml,Pyside2,我正在尝试使用qmlRegisterType扩展QML。我有一个python类——PyQml.py、main.qml文件和它的样板代码。 问题是我无法引用(导入)main.qml文件中的PyQml对象,我收到一个错误-->找不到qml模块(PyQml) 到目前为止,我已经确定了QML_IMPORT_PATH变量路径。由于我是hopelles,我在其中一个路径中创建了名为PyQml的文件夹,其中包含PyQml.py,但仍然没有成功。此外,我在Qt Creator项目中找不到*.pro文件。我想我应

我正在尝试使用qmlRegisterType扩展QML。我有一个python类——PyQml.py、main.qml文件和它的样板代码。 问题是我无法引用(导入)main.qml文件中的PyQml对象,我收到一个错误-->找不到qml模块(PyQml)

到目前为止,我已经确定了QML_IMPORT_PATH变量路径。由于我是hopelles,我在其中一个路径中创建了名为PyQml的文件夹,其中包含PyQml.py,但仍然没有成功。此外,我在Qt Creator项目中找不到*.pro文件。我想我应该为自定义对象添加另一条路径

PyQml.py

class PyQml(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        # Initialise the value of the properties.
        self._name = ''
        self._shoeSize = 0

    # Define the getter of the 'name' property.  The C++ type of the
    # property is QString which Python will convert to and from a string.
    @Property('str')
    def name(self):
        return self._name

    # Define the setter of the 'name' property.
    @name.setter
    def name(self, name):
        self._name = name

    # Define the getter of the 'shoeSize' property.  The C++ type and
    # Python type of the property is int.
    @Property(int)
    def shoeSize(self):
        return self._shoeSize

    # Define the setter of the 'shoeSize' property.
    @shoeSize.setter
    def shoeSize(self, shoeSize):
        self._shoeSize = shoeSize

qmlengine.py

import sys
import sqlite3
from PySide2 import QtCore, QtGui, QtWidgets, QtQuick
from PySide2.QtCore import Qt,QUrl
from PySide2.QtQml import QQmlApplicationEngine,qmlRegisterType
from PySide2.QtGui import QGuiApplication
from ViewModel import PyQml

if __name__ == '__main__':
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    print(QQmlApplicationEngine.importPathList(engine))
    ctx = engine.rootContext()
    ctx.setContextProperty("qmlapp", engine) #the string can be anything
    qmlRegisterType(PyQml.PyQml, 'People', 1, 0, 'Person');
    engine.load('Documents/ctmd/Qml/main.qml')
    win = engine.rootObjects()[0]
    win.show()
    sys.exit(app.exec_())
main.qml

import QtQuick 2.0
import QtQuick.Controls 1.4
import PyQml 1.0 ---- Error QML module not found ( PyQml)
ApplicationWindow {

    menuBar: MenuBar {
        Menu {
            title: "File"
            MenuItem { text: "Open..." }
            MenuItem { text: "Close" }
        }

        Menu {
            title: "Edit"
            MenuItem { text: "Cut" }
            MenuItem { text: "Copy" }
            MenuItem { text: "Paste" }
        }
    }
    Grid {
        columns: 3
        spacing: 2
        Rectangle { color: "red"; width: 50; height: 50 }
        Rectangle { color: "green"; width: 20; height: 50 }
        Rectangle { color: "blue"; width: 50; height: 20 }
        Rectangle { color: "cyan"; width: 50; height: 50 }
        Rectangle { color: "magenta"; width: 10; height: 10 }
    }

}
项目文件夹树

Qml
   -main.qml
PyQml.py
qmlengine.py

PyQml只是一个示例类,在一天结束时,我想传递我用python计算的自定义数据(x,y坐标),并用qml绘制数据;博士没有消除错误消息的解决方案,因为这是QtCreator使用Qt for Python的一个限制。但是QtCreator只是一个IDE,因此不需要在那里工作,您只需从控制台/CMD运行代码:

python /path/of/script.py

您有以下错误:

  • 当您向qmlRegisterType注册QObject时,“People”是QML中包的名称,“Person”是组件的名称,因此除非更改注册表参数,否则不应在导入中使用PyQml

  • QtCreator/QtQuickDesigner在Python支持方面仍有局限性,因此消息“未找到Qml模块(FooPackage)”就是一个示例。正如qtforpython/PySide2的开发人员在未来版本中指出的那样,他们将添加新的特性,但目前还不可能

  • 我发现您在出版物中指出的结构与您的项目不匹配,因为例如,您指出main.qml位于与qmlengine.py处于同一级别的qml文件夹中,但在您使用“Documents/ctmd/qml/main.qml”的加载中

  • PySide2对属性装饰器及其设置器有限制,因为它不被QML识别,而是使用扩展声明:
    name\u of_Property=Property(type\u of_Property,fget=getter\u of_Property,…)

  • 如果Qt属性具有setter,则它必须具有关联的信号

考虑到上述情况,解决方案是:

├── PyQml.py
├── Qml
│   └── main.qml
└── qmlengine.py
qmlengine.py

导入操作系统
导入系统
从PySide2.QtCore导入QUrl
从PySide2.QtGui导入qgui应用程序
从PySide2.QtQml导入QQmlApplicationEngine,qmlRegisterType
导入PyQml
如果名称=“\uuuuu main\uuuuuuuu”:
app=qgui应用程序(sys.argv)
qmlRegisterType(PyQml.PyQml,“人”,1,0,“人”)
引擎=QQmlApplicationEngine()
ctx=engine.rootContext()
setContextProperty(“qmlapp”,引擎)#字符串可以是任何内容
current_dir=os.path.dirname(os.path.realpath(uu文件_uu))
filename=os.path.join(当前目录“Qml/main.Qml”)
engine.load(QUrl.fromLocalFile(文件名))
如果不是engine.rootObjects():
系统出口(-1)
sys.exit(app.exec_())
PyQml.py

来自PySide2.QtCore导入属性、信号、QObject
PyQml类(QObject):
名称更改=信号(str)
shoeSizeChanged=信号(int)
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
#初始化属性的值。
self._name=“”
self.\u shoeSize=0
#定义“name”属性的getter。C++的类型
#属性是QString,Python将从该字符串转换为字符串。
def get_名称(自身):
返回self.\u name
#定义“name”属性的setter。
def set_名称(自身,名称):
如果是self.\u name!=姓名:
self.\u name=name
self.nameChanged.emit(名称)
name=属性(str,fget=get\u name,fset=set\u name,notify=nameChanged)
#定义'shoeSize'属性的getter。C++类型与
#属性的Python类型为int。
def get_shoeSize(自我):
回归自我
#定义“shoeSize”属性的setter。
def set_鞋底尺寸(自身,鞋底尺寸):
如果是自己的话鞋样:
self._shoeSize=shoeSize
self.shoeSizeChanged.emit(shoeSize)
shoeSize=属性(
int,fget=get\u shoeSize,fset=set\u shoeSize,notify=shoeSizeChanged
)
main.qml

import QtQuick 2.0
import QtQuick.Controls 1.4
import People 1.0

ApplicationWindow {
    visible: true

    Person{
        name: "foo"
        shoeSize: 10
    }

    menuBar: MenuBar {
        Menu {
            title: "File"
            MenuItem { text: "Open..." }
            MenuItem { text: "Close" }
        }

        Menu {
            title: "Edit"
            MenuItem { text: "Cut" }
            MenuItem { text: "Copy" }
            MenuItem { text: "Paste" }
        }
    }
    Grid {
        columns: 3
        spacing: 2
        Rectangle { color: "red"; width: 50; height: 50 }
        Rectangle { color: "green"; width: 20; height: 50 }
        Rectangle { color: "blue"; width: 50; height: 20 }
        Rectangle { color: "cyan"; width: 50; height: 50 }
        Rectangle { color: "magenta"; width: 10; height: 10 }
    }
}

我发现如果我忽略了这个错误


在qml窗口中,一切正常。我甚至尝试重新安装IDE,但错误依然存在。感谢您的澄清。

感谢您的及时回复。我也犯了同样的错误<代码>找不到QML模块(人员)。将鼠标悬停在我获得其他信息-
导入路径:C:/Qt/5.12.3/msvc2017\u 64/qml对于qmake项目,使用qml\u Import\u PATH变量添加导入路径。对于Qbs项目,在产品中声明并设置qmlImportPaths属性以添加导入路径。对于qmlproject项目,请使用ImportPath属性添加导入路径。对于Cmake项目,请确保QML_IMPORT_PATH变量位于CMakeCache.txt
@user2727167 read:QtCreator/QtQuickDesigner在Python支持方面仍有限制,因此消息“QML module not found(FooPackage)”就是一个示例。正如qtforpython/PySide2的开发人员在未来版本中指出的那样,他们将添加新特性,但目前还没有possible@user2727167您似乎还没有读过我的答案,QtCreator目前在python方面存在局限性,尤其是在QML中使用python时,但QtCreator只是一个IDE,所以不需要所有东西都在那里工作。从控制台/CMD:
python/path/of/qmlengine.py为后代运行的测试:这里解释了PySide2中属性装饰器有点损坏的问题和原因。对我来说,这个错误不能被忽略。当我试图打开QML表单编辑器时,出现以下错误(参见下图),然后单击OK返回QML文本编辑器。所以