我按照这个示例创建一个流布局:
http://doc.qt.io/qt-4.8/qt-layouts-flowlayout-example.html http://doc.qt.io/qt-4.8/qt-layouts-flowlayout-example.html
这个没有addToIndex(int _index)
功能,所以我想实现这个。由于此布局使用 QLayoutItems,我想创建一个可以在特定索引后插入项目的函数。
如何获取项目的索引作为整数并插入到布局中?
UPDATE:
流程布局.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();
}
流程窗口.cpp
void FlowWindow::addLineBreak()
{
flowbreak = new FlowLayoutButton(NULL);
QLayoutItem *item = new QWidgetItem(flowbreak);
flowlayout->insertItem(index, item);
//flowlayout->addWidget(flowbreak);
}
flowlayoutbutton.cpp 构造函数
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);
}
添加以下方法:
流程布局.h:
class FlowLayout : public QLayout
{
public:
void insertWidget(int index, QWidget *w);
// rest of the class from the example
};
流程布局.cpp:
// new method
void FlowLayout::insertWidget(int index, QWidget *w) {
addWidget(w);
itemList.move(indexOf(w), index);
}
现在您只需调用即可使用它(假设您有一个指向名为的布局的指针flowLayout
):
flowLayout->insertWidget(2,new QPushButton("Button"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)