-
-
Save mortbauer/a19788bf16bd3073c6fea1c3716650db to your computer and use it in GitHub Desktop.
This file contains 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 PyQt5.QtCore import * | |
from PyQt5.QtGui import * | |
from PyQt5.QtMultimedia import * | |
from PyQt5.QtQml import QQmlApplicationEngine | |
def ListModelFor(klass): | |
class ListModel(QAbstractItemModel): | |
def __init__(self): | |
super().__init__() | |
self._items = [] | |
meta = klass().metaObject() | |
self._roleNames = {} | |
for i in range(0, meta.propertyCount()): | |
self._roleNames[i] = meta.property(i).name().encode('utf-8') | |
def rowCount(self, parent=QModelIndex()): | |
return len(self._items) | |
def columnCount(self, parent=QModelIndex()): | |
return 1 | |
def parent(self, index): | |
return QModelIndex() | |
def roleNames(self): | |
return self._roleNames | |
def data(self, index, role): | |
item = self._items[index.row()] | |
return item.property(self._roleNames[role]) | |
def index(self, row, column=0, parent=QModelIndex()): | |
return self.createIndex(row, column, parent) | |
def append(self, item): | |
index = self.rowCount() | |
self.beginInsertRows(QModelIndex(), index, index) | |
self._items.append(item) | |
self.endInsertRows() | |
def update(self, index, item): | |
self._items[index] = item | |
self.dataChanged.emit(self.index(index), self.index(index)) | |
def remove(self, item): | |
index = self._items.index(item) | |
self.beginRemoveRows(QModelIndex(), index, index) | |
self._items.remove(item) | |
self.endRemoveRows() | |
return ListModel | |
class ComboItem(QObject): | |
def __init__(self, name=None, value=None, extra=None, parent=None): | |
super().__init__(parent) | |
self._name = name | |
self._value = value | |
self._extra = extra | |
@pyqtProperty(str) | |
def name(self): | |
return self._name | |
@name.setter | |
def name(self, value): | |
self._name = value | |
@pyqtProperty(str) | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
self._value = value | |
@pyqtProperty(str) | |
def extra(self): | |
return self._extra | |
@extra.setter | |
def extra(self, value): | |
self._extra = value | |
class ComboItemListModel(ListModelFor(ComboItem)): | |
clicked = pyqtSignal() | |
def __init__(self): | |
super().__init__() | |
self.clicked.connect(self._on_clicked) | |
def _on_clicked(self): | |
for i in self._items: | |
print(i.__dict__) | |
class Application(QGuiApplication): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.mainWindow = MainWindow(self) | |
def start(self): | |
self.mainWindow.show() | |
class MainWindow(QObject): | |
clicked = pyqtSignal() | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
self.clicked.connect(self._on_clicked) | |
self.items = ComboItemListModel() | |
self.items.append(ComboItem('first', '1', 'extra one')) | |
self.items.append(ComboItem('second', '2', 'extra two')) | |
self.items.append(ComboItem('third', '3', 'extra three')) | |
self.engine = QQmlApplicationEngine(self) | |
ctx = self.engine.rootContext() | |
ctx.setContextProperty('items', self.items) | |
ctx.setContextProperty('main', self) | |
self.engine.quit.connect(parent.quit) | |
self.engine.load(QUrl.fromLocalFile('Application.qml')) | |
self.window = self.engine.rootObjects()[0] | |
def _on_clicked(self): | |
n = self.items.rowCount()+1 | |
self.items.append(ComboItem('%s th'%n, str(n), 'extra one')) | |
def show(self): | |
self.window.show() |
This file contains 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.2 | |
import QtQuick.Window 2.1 | |
import QtQuick.Controls 1.1 | |
import QtQuick.Controls.Styles 1.2 | |
import QtMultimedia 5.0 | |
import QtQuick.Layouts 1.11 | |
Window { | |
id: mainWindow | |
title: "QApplication" | |
width: 800 | |
height: 600 | |
flags: Qt.Window | |
color: "#333" | |
Action { | |
id: closeWindow | |
shortcut: "Esc" | |
onTriggered: Qt.quit(); | |
} | |
ComboBox { | |
model: items | |
implicitWidth: 200 | |
anchors.centerIn: parent | |
textRole: "name" | |
} | |
RowLayout { | |
Button { | |
text: "Main" | |
onClicked: { | |
main.clicked() | |
} | |
} | |
Button { | |
text: "Items" | |
onClicked: { | |
items.clicked() | |
} | |
} | |
} | |
} |
This file contains 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
#!env python3 | |
import sys | |
import asyncio | |
from quamash import QEventLoop | |
from application import Application | |
try: | |
# handle ctrl+c on Linux | |
import signal | |
signal.signal(signal.SIGINT, signal.SIG_DFL) | |
except ImportError: | |
pass | |
if __name__ == '__main__': | |
app = Application(sys.argv) | |
loop = QEventLoop(app) | |
asyncio.set_event_loop(loop) | |
app.start() | |
sys.exit(loop.run_forever()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment