qt - How to add an Item to the specific index in the layout -
i following example create flow layout:
http://doc.qt.io/qt-4.8/qt-layouts-flowlayout-example.html
this doesn't have addtoindex(int _index)
function, implement this. since layout uses qlayoutitems, create function can insert item after specific index.
how can index of item integer , insert layout?
update:
flowlayout.cpp
flowlayout *flowlayout = new flowlayout; void flowlayout::insertitem(int index, qlayoutitem *item) { if(itemlist.size() < index) { itemlist.append(item); } else { itemlist.insert(index,item); qrect tmp = this->geometry(); } update(); }
flowwindow.cpp
void flowwindow::addlinebreak() { flowbreak = new flowlayoutbutton(null); qlayoutitem *item = new qwidgetitem(flowbreak); flowlayout->insertitem(index, item); //flowlayout->addwidget(flowbreak); }
flowlayoutbutton.cpp constructor
flowlayoutbutton::flowlayoutbutton(qwidget *_parent):qwidget(_parent) { qboxlayout *layout = new qhboxlayout; flowbreak = new qpushbutton(tr("-------label-------")); flowbreak->setgeometry(0, 0, 200,20); layout->addwidget(flowbreak); setlayout(layout); }
add following method:
flowlayout.h:
class flowlayout : public qlayout { public: void insertwidget(int index, qwidget *w); // rest of class example };
flowlayout.cpp:
// new method void flowlayout::insertwidget(int index, qwidget *w) { addwidget(w); itemlist.move(indexof(w), index); }
now can use calling (assuming have pointer layout named flowlayout
):
flowlayout->insertwidget(2,new qpushbutton("button"));
Comments
Post a Comment