我正在实现一个拖放事件过滤器以重新排列布局中的小部件,并在某一时刻将其中几个弹出到队列中,在拖动的小部件所在的位置添加一个橡皮筋,然后将其余小部件添加回布局(因为似乎没有办法使用 QLayout 界面“插入”),如下所示:
// HANDLE DRAG ENTER EVENTS
if (p_event->type() == QEvent::DragEnter)
{
QDragEnterEvent* dragEnterEvent = static_cast<QDragEnterEvent*>(p_event);
if (dragEnterEvent->mimeData()->hasFormat("text/plain"))
{
QString objectName = dragEnterEvent->mimeData()->text();
// findChild doesn't work on layouts because they don't ever
// inject themselves into the parent/child hierarchy, so we
// use the itemAt approach instead.
for (int i = 0; i < layout->count(); ++i)
{
dragItem = layout->itemAt(i)->widget();
if (dragItem->objectName() == objectName)
{
dragEnterEvent->acceptProposedAction();
// 'Rearrange' the widgets. This basically entails removing
// everything after the drag item, adding a placeh older, and
// then adding them back
QQueue<QWidget*> fifo;
// take everything after the drag item out
// important to have count as a local var, because otherwise it will
// decrement with every loop iteration.
int count = layout->count();
for (int j = i + 1; j < count; j++)
{
fifo.enqueue(layout->takeAt(i+1)->widget()); // the indices shift left on their own, so we only ever want to take i+1.
}
// add a 'rubber band' placeholder
m_band = new QRubberBand(QRubberBand::Rectangle);
m_band->setObjectName("placeholderBand");
m_band->setVisible(true);
m_band->setFixedSize(dragItem->size());
layout->addWidget(m_band);
// put the widgets in the fifo back in
count = fifo.count();
for(int j = 0; j < count; j++)
{
layout->addWidget(fifo.dequeue());
}
break;
}
}
}
}
这种方法的问题是添加/删除小部件会导致非常明显和讨厌的闪烁。有没有
- 我可以通过某种方式阻止布局重新计算自身,直到完成所有添加/删除操作,或者
- 将小部件插入布局(仅使用
QLayout
界面)不会导致闪烁的更好方法?