I have QListView with MultiSelection option for selecting items.
listView->setSelectionMode(MultiSelection);
It is exactly what I want except one thing. I don't want a deselect behaviour on drag select(When I'm dragging over selected items they become unselected). I want items below drag selection to be always selected.
Is there a way to change this behaviour?
UPDATE: Wrapping is enabled so items are drawn in a couple of lines.
To change the selection behaviour of the QListView you should re-implement QAbstractItemView::selectionCommand
function. Here is an example:
mylistwidget.h
#ifndef MYLISTWIDGET_H
#define MYLISTWIDGET_H
#include <QListWidget>
#include <QItemSelectionModel>
class MyListWidget : public QListWidget
{
Q_OBJECT
public:
explicit MyListWidget(QWidget *parent = 0);
protected:
virtual QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex &index,
const QEvent *event = 0) const;
};
#endif // MYLISTWIDGET_H
mylistwidget.cpp
MyListWidget::MyListWidget(QWidget *parent) :
QListWidget(parent)
{
}
QItemSelectionModel::SelectionFlags MyListWidget::selectionCommand(const QModelIndex & index, const QEvent * event) const
{
QItemSelectionModel::SelectionFlags flags = QAbstractItemView::selectionCommand(index, event);
if (event->type() == QEvent::MouseMove)
{
flags &= ~QItemSelectionModel::Toggle;
flags |= QItemSelectionModel::Select;
}
return flags;
}