I'm implementing a drap-and-drop event filter to rearrange widgets in a layout, and at one point pop several of them into a queue, add a rubber band where the dragged widget was, then add the rest of the widgets back to the layout (since there doesn't seem to be a way to 'insert at' using the QLayout interface) like so:
// 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;
}
}
}
}
The problem with this approach is the adding/removing of the widgets causes a very noticeable and nasty flicker. Is there either
- some way I can stop the layout from recalculating itself until after all the add/remove operations are done, or
- some better way to insert widgets into the layout (using only the
QLayout
interface) that won't cause flickering?
addChildWidget(w);
addItem(QLayoutPrivate::createWidgetItem(this, w));
and no one of this methods doesn't call update or something another – Kosovan