2
votes

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

  1. some way I can stop the layout from recalculating itself until after all the add/remove operations are done, or
  2. some better way to insert widgets into the layout (using only the QLayout interface) that won't cause flickering?
1
Yes, you are right, I saw more source and inside addWidget there are only addChildWidget(w); addItem(QLayoutPrivate::createWidgetItem(this, w)); and no one of this methods doesn't call update or something anotherKosovan

1 Answers

0
votes

not sure it will help, since I used in a different context (semantic syntax highlighting, in my project loqt).

#ifndef BLOCKSIG_H
#define BLOCKSIG_H

#include <QObject>
#include <QPointer>

/** temporary stop signals communication
 *  use with care: notably QTextEdit formatting can be lost
 *  on multiple changes notification
 */
struct blockSig {

    blockSig(QObject* target) : target(target) { current = target->blockSignals(true); }
    ~blockSig() { off(); }

    void off() { if (target) { target->blockSignals(current); target = 0; } }

private:

    QPointer<QObject> target;
    bool current;
};

#endif

example usage, avoiding unwanted notifications on formatting changes

#include "blockSig.h"

void ConsoleEdit::selectionChanged()
{
    blockSig bs(this);

    foreach (ExtraSelection s, extraSelections())
        s.cursor.setCharFormat(s.format);
    extraSelections().clear();

    ...
}