Last active
July 28, 2017 15:24
-
-
Save eyllanesc/b2f190dbf191b2c345b35ae75f9dd354 to your computer and use it in GitHub Desktop.
PyQt5 Dynamically add rectangles to QML grid
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 PyQt5.QtGui import QGuiApplication, QColor | |
from PyQt5.QtQml import QQmlApplicationEngine, QQmlComponent | |
from PyQt5.QtCore import QUrl, Qt, QCoreApplication, QAbstractListModel, QModelIndex, QTimer, qsrand, qrand, QTime | |
from PyQt5.QtQuick import QQuickItem | |
class Data(object): | |
def __init__(self, width=35, height=35, color=QColor("red")): | |
self._width = width | |
self._height = height | |
self._color = color | |
def width(self): | |
return self._width | |
def height(self): | |
return self._height | |
def color(self): | |
return self._color | |
class Model(QAbstractListModel): | |
WidthRole = Qt.UserRole + 1 | |
HeightRole = Qt.UserRole + 2 | |
ColorRole = Qt.UserRole + 3 | |
_roles = {WidthRole: b"width", HeightRole: b"height", ColorRole: b"color"} | |
def __init__(self, parent=None): | |
QAbstractListModel.__init__(self, parent) | |
self._datas = [] | |
def addData(self, data): | |
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) | |
self._datas.append(data) | |
self.endInsertRows() | |
def rowCount(self, parent=QModelIndex()): | |
return len(self._datas) | |
def data(self, index, role=Qt.DisplayRole): | |
try: | |
data = self._datas[index.row()] | |
except IndexError: | |
return QVariant() | |
if role == self.WidthRole: | |
return data.width() | |
if role == self.HeightRole: | |
return data.height() | |
if role == self.ColorRole: | |
return data.color() | |
return QVariant() | |
def roleNames(self): | |
return self._roles | |
if __name__ == "__main__": | |
import sys | |
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) | |
app = QGuiApplication(sys.argv) | |
engine = QQmlApplicationEngine() | |
model = Model() | |
model.addData(Data(44, 33, QColor("red"))) | |
model.addData(Data(23, 53, QColor("#333"))) | |
context = engine.rootContext() | |
context.setContextProperty('myModel', model) | |
engine.load(QUrl.fromLocalFile("main.qml")) | |
if len(engine.rootObjects()) == 0: | |
sys.exit(-1) | |
qsrand(QTime.currentTime().msec()) | |
timer = QTimer(engine) | |
timer.timeout.connect(lambda: model.addData(Data(20 + qrand() % 40, | |
20 + qrand() % 40, | |
QColor(qrand() % 255, qrand() % 255, qrand() % 255)))) | |
timer.start(1000) | |
engine.quit.connect(app.quit) | |
sys.exit(app.exec_()) |
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.Window 2.2 | |
import QtQuick 2.0 | |
import QtQuick.Controls 1.4 | |
Window { | |
visible: true | |
id: channels | |
Grid { | |
columns: 3 | |
spacing: 9 | |
Repeater{ | |
model: myModel | |
delegate: Rectangle{ | |
height: model.height | |
width: model.height | |
color: model.color | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment