如何在 Qt QTableView 中设置 Shift + 单击选择的初始索引?
How can I set the initial index for Shift + click selection in Qt QTableView?
我的问题是在按住 Ctrl 键的同时单击某个项目的 deselect。变成了下面Shift+点击的初始索引select.
我想要的是将 Shift selection 的初始索引设置为索引 0。
例如:
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringListModel model({"1", "2", "3", "4", "5", "6", "7", "8", "9"});
QTableView view;
view.horizontalHeader()->setVisible(false);
view.setSelectionBehavior(QAbstractItemView::SelectRows);
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
view.setModel(&model);
view.show();
return a.exec();
}
Select 行 header 2
Deselect 行 header 2 使用 Ctrl + 单击
Shift + 单击行 header 5
预期结果:
第 1、2、3、4 和 5 行是 selected。
实际结果:
第 2、3、4 和 5 行是 selected。
有什么方法可以达到预期的效果吗?
为此,您应该子类化 QTableView
。这是针对 Qt4 测试的小示例。对于 Qt5,它应该非常相似。
Header:
#ifndef CUSTOMTABLEWIDGET_H
#define CUSTOMTABLEWIDGET_H
#include <QTableView>
class CustomTableWidget : public QTableView
{
Q_OBJECT
public:
explicit CustomTableWidget(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event);
};
#endif // CUSTOMTABLEWIDGET_H
Cpp:
#include "customtablewidget.h"
#include <QMouseEvent>
#include <QItemSelectionModel>
CustomTableWidget::CustomTableWidget(QWidget *parent) :
QTableView(parent)
{
}
void CustomTableWidget::mousePressEvent(QMouseEvent *event)
{
QModelIndex clickedIndex = this->indexAt(QPoint(event->x(), event->y()));
bool isShiftClicked = Qt::LeftButton == event->button() && (event->modifiers() & Qt::ShiftModifier);
if (clickedIndex.isValid() && isShiftClicked)
{
bool isRowSelected = this->selectionModel()->isRowSelected(clickedIndex.row(),
clickedIndex.parent());
if (!isRowSelected)
{
QModelIndex initialIndex = this->model()->index(0, 0);
this->selectionModel()->clear();
this->selectionModel()->select(QItemSelection(initialIndex, clickedIndex),
QItemSelectionModel::Select);
event->accept();
return;
}
}
QTableView::mousePressEvent(event);
}
我的问题是在按住 Ctrl 键的同时单击某个项目的 deselect。变成了下面Shift+点击的初始索引select.
我想要的是将 Shift selection 的初始索引设置为索引 0。
例如:
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringListModel model({"1", "2", "3", "4", "5", "6", "7", "8", "9"});
QTableView view;
view.horizontalHeader()->setVisible(false);
view.setSelectionBehavior(QAbstractItemView::SelectRows);
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
view.setModel(&model);
view.show();
return a.exec();
}
Select 行 header 2
Deselect 行 header 2 使用 Ctrl + 单击
Shift + 单击行 header 5
预期结果: 第 1、2、3、4 和 5 行是 selected。
实际结果: 第 2、3、4 和 5 行是 selected。
有什么方法可以达到预期的效果吗?
为此,您应该子类化 QTableView
。这是针对 Qt4 测试的小示例。对于 Qt5,它应该非常相似。
Header:
#ifndef CUSTOMTABLEWIDGET_H
#define CUSTOMTABLEWIDGET_H
#include <QTableView>
class CustomTableWidget : public QTableView
{
Q_OBJECT
public:
explicit CustomTableWidget(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event);
};
#endif // CUSTOMTABLEWIDGET_H
Cpp:
#include "customtablewidget.h"
#include <QMouseEvent>
#include <QItemSelectionModel>
CustomTableWidget::CustomTableWidget(QWidget *parent) :
QTableView(parent)
{
}
void CustomTableWidget::mousePressEvent(QMouseEvent *event)
{
QModelIndex clickedIndex = this->indexAt(QPoint(event->x(), event->y()));
bool isShiftClicked = Qt::LeftButton == event->button() && (event->modifiers() & Qt::ShiftModifier);
if (clickedIndex.isValid() && isShiftClicked)
{
bool isRowSelected = this->selectionModel()->isRowSelected(clickedIndex.row(),
clickedIndex.parent());
if (!isRowSelected)
{
QModelIndex initialIndex = this->model()->index(0, 0);
this->selectionModel()->clear();
this->selectionModel()->select(QItemSelection(initialIndex, clickedIndex),
QItemSelectionModel::Select);
event->accept();
return;
}
}
QTableView::mousePressEvent(event);
}