How to clear the selection of QListView when multiple items are selected?

Gojira picture Gojira · Jun 27, 2013 · Viewed 7.5k times · Source

I am working on a Qt App where I have a QListView. There are few items present in the list. My application requires items to be rearranged according to user's choice. Everything is working fine but I am facing a small issue.

When I do multiple selection using a mouse i.e. select items by dragging the mouse, it leaves the selection mark on the QlistView even after I do some rearranging operation. I am sure its got something to do with clearing the selection. I tried to use repaint() or clearFocus() but nothing seems to be working.

E.g When we select a group of folders we drag our mouse, which intern gives us rectangular box which covers all the items which come under it. That particular rectangle box stays inside my QListView. I do not want it after I have selected the items.

I have a mousemoveevent inside which I am doing it:

    void BinListView::mouseMoveEvent (QMouseEvent *event) {

        if (NULL == event) {
            return;
        } else {

            if (Qt::LeftButton & event->buttons ()) {
                int nDis = (event->pos () - m_posStart).manhattanLength ();

                if (nDis >= QApplication::startDragDistance ()) {
                    startDrag (m_posStart);
                }
            }

#ifdef QT_NO_DEBUG
            QListView::mouseMoveEvent (event);
            QListView::repaint();
            QListView::clearFocus();

#endif

        }
        repaint();
    } 

If you notice QT_NO_DEBUG area, you will see my clearing focus and repainting but nothing is helping me. ANy solution to this problem?

P.S.: I am running it in a release mode.

Answer

trompa picture trompa · Jun 27, 2013

Use

void QAbstractItemView::clearSelection() [slot]

to clear selection

Also, all views have a selection model you can access through:

QItemSelectionModel * QAbstractItemView::selectionModel() const

that allows doing a lot more things selecting

Look at Handling selections in item views

Another thing ...

#ifdef QT_NO_DEBUG

means release

#ifndef QT_NO_DEBUG

would be debug mode, and im sure, it will not compile the code you pasted