简体   繁体   中英

PySide2 with qml as ui

I am using pyqt5 and pyside2 to play around with qtquick2 controls, even though pySide2 claims they pyQt syntax and logic, its not always true, and pySide2 documentation is either seriously outdated or its simply inaccurate. (i do realise pySide2 is not ready for proper use, but i would still try to get hang of it)

For example slot usage i do get pyQt5

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

pySide2

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

Then just from myQml i call that function and it works.

However i need alternatives for :

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

And last an actual code :

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
    }
}

the code above for example uses pyQT5 and qtQuick2 to just print out value of slider, when slider is moved.

Is there a way to implement this with pySide2, i tried some options, and i can do simple button clicks in pySide, however, for properties and setters in pySide i found no valuable information. (well what i found was outdated and for qtQuick1)

if anyone give me a working example, or point me somewhere i would be very thankfull! cheers

In the case of PySide2 it has the same PySide nomenclature so I recommend you check the following link .

In the case of PySide you must use Property that is similar to pyqtProperty, Slot equals pyqtSlot and Signal equals 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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM