1

我正在使用 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)

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

4

1 回答 1

4

如果PySide2它具有相同的PySide命名法,因此我建议您查看以下链接

对于 PySide,您必须使用类似于 pyqtProperty 的 Property,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_())
于 2018-05-03T17:35:59.780 回答