How to clear the selection of QListView when multi

2019-03-01 11:11发布

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.

2条回答
可以哭但决不认输i
2楼-- · 2019-03-01 11:44

Use the QAbstractItemView::clearSelection function.

If you have subclassed QListView you can just call it with clearSelection();

查看更多
够拽才男人
3楼-- · 2019-03-01 12:04

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

查看更多
登录 后发表回答