python - PyQt5 Dynamically add rectangles to QML grid -


i have built grid of rectangles in qml run python. engine.load('main.qml')

window {     id: channels     grid {         columns: 2         spacing: 9         rectangle {             color: "#333"             width: 75             height: 75         }         rectangle {             color: "#333"             width: 75             height: 75         }     } } 

however, have on fifty rectangles, need able dynamically create , update them python. how can that?

to provide information python (or c++) qml can read link. in recommends use qabstractlistmodel since notifies changes qml, in addition added dynamically use repeater.

main.qml:

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

.py:

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 

to make test use following code:

main.py

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_()) 

the complete example find here.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -