PySide2 与 qml 作为 ui

PySide2 with qml as ui

我正在使用 pyqt5 和 pyside2 来玩弄 qtquick2 控件,尽管 pySide2 声称它们是 pyQt 语法和逻辑,但它并不总是正确的,而且 pySide2 文档要么严重过时,要么根本不准确。 (我确实意识到 pySide2 还没有准备好正确使用,但我仍然会尝试掌握它)

例如插槽使用我确实得到 pyQt5

@pyqtSlot()
def sayHi(self):
    print("Hi")

pySide2

@Slot()
def sayHi(self):
    print("Hi")

然后我从 myQml 调用该函数并且它起作用了。

但是我需要替代品:

@pyqtProperty(float, notify=currentValueChanged)
@currentValue.setter
variableX =pyqtSignal()

最后一个实际代码:

import sys
import os

from PyQt5.QtCore import QObject, QUrl, Qt, pyqtSlot, pyqtSignal, pyqtProperty
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine

class Manager(QObject):
    #slider Value
    currentValueChanged = pyqtSignal()
    def __init__(self):
        QObject.__init__(self)
        self.m_currentValue =0
        #slider
        self.currentValueChanged.connect(self.on_currentValueChanged)

    #slide stuff    
    @pyqtProperty(float, notify=currentValueChanged)
    def currentValue(self):             
        return self.m_currentValue       

    #slider    
    @currentValue.setter
    def currentValue(self, val):
        if self.m_currentValue == val:
            return
        self.m_currentValue = val
        self.currentValueChanged.emit()


    #slider VOlUME CHANGED <<<<<<<<<<<<<<<<<<<<<<<<< WORKS>>>>>>>>>>>>>>>>>>>>>>    
    @pyqtSlot()
    def on_currentValueChanged(self):
        print(self.m_currentValue)

if __name__ == "__main__":
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    app = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    manager = Manager()
    ctx = engine.rootContext()
    ctx.setContextProperty("Manager", manager)
    engine.load('main.qml')
    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())

QML

import QtQuick 2.10
import QtQuick.Controls 2.1
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.3

ApplicationWindow {
    id: applicationWindow
    Material.theme: Material.Light
    title: qsTr("Test Invoke")
    visible: true

    width: 600
    height: 500

    Slider {
        id: slider
        x: 160
        y: 311
        value: 0.5
        property bool updateValueWhileDragging: true
        onMoved: Manager.currentValue = value
    }
}

上面的代码例如使用 pyQT5 和 qtQuick2 在移动滑块时打印出滑块的值。

有没有办法用 pySide2 实现这个,我尝试了一些选项,我可以在 pySide 中进行简单的按钮点击,但是,对于 pySide 中的属性和设置器,我没有找到有价值的信息。 (好吧,我发现的内容已经过时并且适用于 qtQuick1)

如果有人给我一个可行的例子,或者指出某个地方,我将非常感激!干杯

PySide2 的情况下,它具有相同的 PySide 命名法,因此我建议您检查以下 link

对于 PySide,你必须使用类似于 pyqt属性 的 属性,Slot 等于 pyqtSlot,Signal 等于 pyqtSignal。

import sys
import os

from PySide2.QtCore import Qt, QObject, Signal, Slot, Property
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine

class Manager(QObject):
    currentValueChanged = Signal()

    def __init__(self):
        QObject.__init__(self)
        self.m_currentValue = 0.0
        self.currentValueChanged.connect(self.on_currentValueChanged)

    @Property(float, notify=currentValueChanged)
    def currentValue(self):
        return self.m_currentValue

    @currentValue.setter
    def setCurrentValue(self, val):
        if self.m_currentValue == val:
            return
        self.m_currentValue = val
        self.currentValueChanged.emit()

    @Slot()
    def on_currentValueChanged(self):
        print(self.m_currentValue)


if __name__ == "__main__":
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    app = QGuiApplication(sys.argv)

    engine = QQmlApplicationEngine()
    manager = Manager()
    ctx = engine.rootContext()
    ctx.setContextProperty("Manager", manager)
    engine.load('main.qml')
    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())