python - How to insert QWidgets in the middle of a Layout? -


i'm using qt framework build graphical user interface. use qgridlayout position qwidgets neatly.
gui looks this:

enter image description here

my application regularly adds new widgets gui @ runtime. these new widgets not added @ end of qlayout, somewhere in middle.

the procedure bit cumbersome. applied on figure above, need take out widg_c, widg_d, ... qgridlayout. next, add widg_xand widg_y, , put other widgets again.
how remove widgets qgridlayout:

for in reversed(range(mygridlayout.count())):     self.itemat(i).widget().setparent(none) ### 

as long you're dealing small amount of widgets, procedure not disaster. in application display lot of small widgets - perhaps 50 or more! application freezes second while procedure ongoing, annoying user.
there way insert widgets somewhere in qlayout, without need take out other widgets?


edit: apparently solution qvboxlayout simple. use function insertwidget(..) instead of addwidget(..). docs can found link: http://doc.qt.io/qt-5/qboxlayout.html#insertwidget
unfortunately, couldn't find similar function qgridlayout.


edit: many people rightly mentioned putting lot of widgets shouldn't cause performance issue - fast indeed (thank @ekhumoro point out). apparently, performance issue faced had algorithm putting widgets back. complicated recursive algorithm puts every widget on right coordinates in qgridlayout. resulted in "flicker" on display. widgets taken out, , put inside delay (due algorithm) - causing flicker.


edit: found solution such can insert new rows qgridlayout. inserting new rows means don't need take out , replace widgets scratch - hence avoid expensive recursive algorithm run.
solution can found in answer below.

thank @ekhumoro, @stuart fisher, @vahancho , @mbjoe help. found way solve issue. no longer use qgridlayout(). instead, built wrapper around qvboxlayout behave if gridlayout, function insert new rows:

class customgridlayout(qvboxlayout):     def __init__(self):         super(customgridlayout, self).__init__()         self.setalignment(qt.aligntop)  # !!!         self.setspacing(20)       def addwidget(self, widget, row, col):         # 1. how many horizontal layouts (rows) present?         horlaysnr = self.count()          # 2. add rows if necessary         if row < horlaysnr:             pass         else:             while row >= horlaysnr:                 lyt = qhboxlayout()                 lyt.setalignment(qt.alignleft)                 self.addlayout(lyt)                 horlaysnr = self.count()             ###         ###          # 3. insert widget @ specified column         self.itemat(row).insertwidget(col, widget)      ''''''      def insertrow(self, row):         lyt = qhboxlayout()         lyt.setalignment(qt.alignleft)         self.insertlayout(row, lyt)      ''''''      def deleterow(self, row):         j in reversed(range(self.itemat(row).count())):             self.itemat(row).itemat(j).widget().setparent(none)         ###         self.itemat(row).setparent(none)      def clear(self):         in reversed(range(self.count())):             j in reversed(range(self.itemat(i).count())):                 self.itemat(i).itemat(j).widget().setparent(none)             ###         ###         in reversed(range(self.count())):             self.itemat(i).setparent(none)         ###      '''''' 

Comments

  1. Online Casino And Sports Betting | KATG
    KATG is a 온카지노 leading international online casino and sports betting platform. We choegocasino offer sports betting, 샌즈카지노 live casino, virtual sports, keno, and many other types of

    ReplyDelete

Post a Comment

Popular posts from this blog

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -