c++ - Qt: QListView 改变 MultiSelection 选择模式的行为

标签 c++ qt qlistview

我有 QListView 和用于选择项目的 MultiSelection 选项。

listView->setSelectionMode(MultiSelection);

除了一件事,这正是我想要的。我不希望在拖动选择时出现取消选择行为(当我拖动选定的项目时,它们会变为未选定状态)。我希望始终选中拖动选择下方的项目。

有没有办法改变这种行为?

更新:启用了环绕,因此项目被绘制成几行。

最佳答案

要更改 QListView 的选择行为,您应该重新实现 QAbstractItemView::selectionCommand 函数。这是一个例子:

我的列表控件.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

我的列表控件.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;
}

关于c++ - Qt: QListView 改变 MultiSelection 选择模式的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965482/

相关文章:

c++ - 如何制作高效的C++跳转表?

c++ - OpenGL - 如何创建顺序无关的透明度?

python - 从 QListView 中正确删除多选行

qt - 在 QFileSystemModel() 中列出文件的最佳方法?

Qt:使用拖放重新排序 ListView 项目

c++ - 我应该从 std::exception 继承吗?

c++ - 为什么这不会编译以及如何实现才能编译?

c++ - 在制作(Qt 等)GUI 时声明对象

c++ - 错误 : could not convert ‘((const MyClass*)this)->MyClass::myLabel’ from ‘QLabel* const’ to ‘QLabel’

Qt Creator、AutoCAD 中使用的 Qt 样式表,