Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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
Qt5/PyQt5:具有QML前端和Python后端的定制QML组件_Python_Qt_Qml_Qt5_Pyqt5 - Fatal编程技术网

Qt5/PyQt5:具有QML前端和Python后端的定制QML组件

Qt5/PyQt5:具有QML前端和Python后端的定制QML组件,python,qt,qml,qt5,pyqt5,Python,Qt,Qml,Qt5,Pyqt5,如果有意义的话,我想创建一个带有python“后端”的QML模块。基本上,我想使用QML来定义组件的外观,然后在python类中实现特定的行为,这应该扩展这个QML类型,并且——在我的想象中——某种程度上必须能够链接到QML组件 我了解如何在python中创建自定义类,并通过qmlRegisterType将其提供给QML。到目前为止,这是可行的,但是所有的绘图都必须在类本身中实现——没有QML (基本上,我想要的是在kivy中使用kv语言进行模拟) 一个小例子: 我实现了一个简单的ColorPi

如果有意义的话,我想创建一个带有python“后端”的QML模块。基本上,我想使用QML来定义组件的外观,然后在python类中实现特定的行为,这应该扩展这个QML类型,并且——在我的想象中——某种程度上必须能够链接到QML组件

我了解如何在python中创建自定义类,并通过qmlRegisterType将其提供给QML。到目前为止,这是可行的,但是所有的绘图都必须在类本身中实现——没有QML

(基本上,我想要的是在kivy中使用kv语言进行模拟)

一个小例子:

我实现了一个简单的ColorPicker小部件,如下所示:

class ColorWheel(QLabel):
    radius = 0

    color_changed = pyqtSignal(float, float)

    def __init__(self, width, height):
        super().__init__()

        pixmap = QPixmap("colorwheel.png").scaled(width, height)

        self.setFixedSize(width, height)
        self.setPixmap(pixmap)
        self.radius = width / 2

    def mouseReleaseEvent(self, event):
        # {...} get mouse position, calc polar corrdinates (omitted)
        # emit signal: new color was selected
        self.color_changed.emit(r, angle)

    def get_polar(self, x, y):
        # {...} calculate polar coordinates for HSV color space (omitted)
        return r, theta
import QtQuick 2.0
Item {
    Image {
        id: img
        anchors.fill: parent
        source: "./colorwheel.png"  
    }
}
import QtQuick 2.2
import ColorWheel 1.0

ApplicationWindow {
    title: qsTr("Test Invoke")
    width: 500
    height: 400

    ColorWheel {
        radius: 200
    }
}
现在我想将GUI代码(pixmap绘图等)移动到QML文件ColorWheel.QML中,如下所示:

class ColorWheel(QLabel):
    radius = 0

    color_changed = pyqtSignal(float, float)

    def __init__(self, width, height):
        super().__init__()

        pixmap = QPixmap("colorwheel.png").scaled(width, height)

        self.setFixedSize(width, height)
        self.setPixmap(pixmap)
        self.radius = width / 2

    def mouseReleaseEvent(self, event):
        # {...} get mouse position, calc polar corrdinates (omitted)
        # emit signal: new color was selected
        self.color_changed.emit(r, angle)

    def get_polar(self, x, y):
        # {...} calculate polar coordinates for HSV color space (omitted)
        return r, theta
import QtQuick 2.0
Item {
    Image {
        id: img
        anchors.fill: parent
        source: "./colorwheel.png"  
    }
}
import QtQuick 2.2
import ColorWheel 1.0

ApplicationWindow {
    title: qsTr("Test Invoke")
    width: 500
    height: 400

    ColorWheel {
        radius: 200
    }
}
在主QML文件main.QML中,我想执行如下操作:

class ColorWheel(QLabel):
    radius = 0

    color_changed = pyqtSignal(float, float)

    def __init__(self, width, height):
        super().__init__()

        pixmap = QPixmap("colorwheel.png").scaled(width, height)

        self.setFixedSize(width, height)
        self.setPixmap(pixmap)
        self.radius = width / 2

    def mouseReleaseEvent(self, event):
        # {...} get mouse position, calc polar corrdinates (omitted)
        # emit signal: new color was selected
        self.color_changed.emit(r, angle)

    def get_polar(self, x, y):
        # {...} calculate polar coordinates for HSV color space (omitted)
        return r, theta
import QtQuick 2.0
Item {
    Image {
        id: img
        anchors.fill: parent
        source: "./colorwheel.png"  
    }
}
import QtQuick 2.2
import ColorWheel 1.0

ApplicationWindow {
    title: qsTr("Test Invoke")
    width: 500
    height: 400

    ColorWheel {
        radius: 200
    }
}
这可能吗?我在Qt和pyQt文档中找不到关于这方面的任何信息。它总是让C++类可用到QML或其他方式……/P> 有人能给我指出正确的方向吗


非常感谢

如果您从
QQuickItem
继承并注册它,它的行为显然类似于
Item{}

如果您在C++/python端添加一些属性,它将是一个带有属性的

colorWheeleImpl.h
(制作一个与python等效的程序):

ColorWheel.qml

import QtQuick 2.0
import com.mycompany.myitems 1.0
ColorWheelImpl {
    Image {
        id: img
        anchors.fill: parent
        source: "./colorwheel.png"  
    }
}

你的解决方案正是我想要的。我花了一段时间才弄清楚文件名和类名之间的关系,但现在它有了意义。非常感谢。C++/Python Impl如何知道使用ColorWheel.qml?@KevenWang,我在答案中添加了qmlRegisterType和import。我懒得搜索qmlRegisterType的Python语法。使用C++/Python Impl的是ColorWheel.qml。