Created
May 27, 2016 14:23
-
-
Save estan/7206221205e764b3535bd90cfd60040b to your computer and use it in GitHub Desktop.
QML Enum Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sys import exit, argv | |
| from PyQt5.QtCore import pyqtSignal, pyqtProperty, Q_ENUMS, QObject | |
| from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType | |
| from PyQt5.QtGui import QGuiApplication | |
| app = None | |
| class Switch(QObject): | |
| class State: | |
| On = 0 | |
| Off = 1 | |
| Q_ENUMS(State) | |
| stateChanged = pyqtSignal(State, arguments=['state']) | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self._state = Switch.State.Off | |
| @pyqtProperty(State, notify=stateChanged) | |
| def state(self): | |
| return self._state | |
| @state.setter | |
| def state(self, state): | |
| if state != self._state: | |
| self._state = state | |
| self.stateChanged.emit(self._state) | |
| def main(): | |
| global app | |
| app = QGuiApplication(argv) | |
| qmlRegisterType(Switch, 'Switch', 1, 0, 'Switch') | |
| engine = QQmlApplicationEngine() | |
| engine.load('example.qml') | |
| exit(app.exec_()) | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import QtQuick 2.0 | |
| import QtQuick.Window 2.0 | |
| import Switch 1.0 | |
| Window { | |
| title: 'QML Enum Example' | |
| visible: true | |
| width: 400 | |
| height: 400 | |
| color: colorSwitch.state === Switch.On ? "green" : "red" | |
| Switch { | |
| id: colorSwitch | |
| state: Switch.Off | |
| onStateChanged: console.log('state: ' + state) | |
| } | |
| Text { | |
| text: "Press window to switch state" | |
| } | |
| MouseArea { | |
| anchors.fill: parent | |
| onClicked: { | |
| if (colorSwitch.state === Switch.Off) | |
| colorSwitch.state = Switch.On | |
| else | |
| colorSwitch.state = Switch.Off | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment