[Interest] Prevent drag-selection in QTreeView

Daniel Price daniel.price at fxhome.com
Thu Oct 4 12:19:40 CEST 2012


I have a QTreeView (actually a QTreeWidget) with multiple selection and drag and drop but I want to disable the 'drag and select' behaviour (i.e. if you click and drag an item which is not itself draggable, the selection is extended to include the items that you mouse over). I still want the shift and ctrl behaviours to work, just not the drag-selection but there is no public API to disable this behaviour.

The code to is buried inside QAbstractItemView and is dependent on the QAbstractItemView 'state' so I came up this:

void MyTreeWidget::mouseMoveEvent(QMouseEvent *evt)
{
                const QTreeWidgetItem *item = itemAt(m_startDragPt);
                if (!item)
                {
                                // if the user clicked in empty space, ignore the move event to avoid extending the selection
                                return;
                }

                // prevent drag-selection behavior by assuming all mouse-moves at this stage initiate a drag;
                // this overrides the code in QAbstractItemView::mouseMoveEvent which extends the selection
                setState(QAbstractItemView::DraggingState);

                // pass event to base class to allow drags etc
                QTreeWidget::mouseMoveEvent(evt);
}

This disables the drag-selection by fooling the view into thinking that each mouse move is actually a drag. But it causes other problems; other widgets do not get a mouse leave event if I mouse over them after QDrag.

How can I disable this behaviour?

________________________________
This email is confidential. It may also be privileged or otherwise protected by work product immunity or other legal rules. Errors and Omissions Excluded. If you are not the intended recipient please notify the sender. Please delete the message from all places in your computer where it is stored. You should not copy the email or use it for any purpose or disclose its contents to any other person. To do so may be unlawful. Email is an informal means of communicating and may be subject to data corruption accidentally or deliberately. For this reason it is inappropriate to rely on advice contained in an email without obtaining written confirmation of it first.

FXhome Limited is a limited company registered in England and Wales. Registered number: 04172812. Registered office: Suite 4 St Giles House, 27 St Giles Street, Norwich, Norfolk, NR2 1JN, U.K.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20121004/3203b3b7/attachment.html>


More information about the Interest mailing list