Auto-scroll while selecting entire rows/columns did not work
If you press and hold a section in a header view you can extend the selection to more rows by moving the mouse. This worked fine until you moved the mouse outside the geometry of the header view. The expected behavior was then to scroll the view (this is what happens with extended selections on regular table cells). [ChangeLog][QtWidgets][QHeaderView] Auto-scroll the view when making extended row/column selections. Change-Id: Ic65aa34d370e74054b2123ab57edb1add0e8adb9 Task-number: QTBUG-21201 Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
This commit is contained in:
parent
62bf16d992
commit
f9408317e7
@ -2371,7 +2371,7 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
|
||||
{
|
||||
Q_D(QHeaderView);
|
||||
int pos = d->orientation == Qt::Horizontal ? e->x() : e->y();
|
||||
if (pos < 0)
|
||||
if (pos < 0 && d->state != QHeaderViewPrivate::SelectSections)
|
||||
return;
|
||||
if (e->buttons() == Qt::NoButton) {
|
||||
#if !defined(Q_WS_MAC)
|
||||
@ -2430,7 +2430,9 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
|
||||
return;
|
||||
}
|
||||
case QHeaderViewPrivate::SelectSections: {
|
||||
int logical = logicalIndexAt(pos);
|
||||
int logical = logicalIndexAt(qMax(-d->offset, pos));
|
||||
if (logical == -1 && pos > 0)
|
||||
logical = d->lastVisibleVisualIndex();
|
||||
if (logical == d->pressed)
|
||||
return; // nothing to do
|
||||
else if (d->pressed != -1)
|
||||
|
@ -33,6 +33,148 @@
|
||||
|
||||
#include <QtWidgets/QtWidgets>
|
||||
|
||||
struct ManualTask {
|
||||
const char *title;
|
||||
const char *instructions;
|
||||
unsigned sectionsMovable : 1;
|
||||
unsigned selectionMode : 3;
|
||||
};
|
||||
|
||||
ManualTask tasks[] = {
|
||||
{ QT_TR_NOOP("0. Default"),
|
||||
"Please provide instructions",
|
||||
true, QAbstractItemView::SingleSelection
|
||||
},
|
||||
{ QT_TR_NOOP("1. Autoscroll"),
|
||||
"<ol>"
|
||||
"<li>Press and hold on section 9 of vertical header.<br/>"
|
||||
"<em>(all cells in the row will be selected)</em>"
|
||||
"</li>"
|
||||
"<li>Extend the selection by moving the mouse down.<br/>"
|
||||
"<em>(selection will extend to the next rows)</em>"
|
||||
"</li>"
|
||||
"<li>Continue to move the mouse down and outside the window geometry.<br/>"
|
||||
"<em>(The view should scroll automatically and the selection should still extend)</em>"
|
||||
"</li>"
|
||||
"<li>While still holding the button, do the same in the opposite direction, i.e. move mouse up and outside the window geometry.<br/>"
|
||||
"<em>(Verify that the view scrolls automatically and the selection changes)</em>"
|
||||
"</li>"
|
||||
"<li>Verify that it works in the other dimension, i.e Press and hold section 9 of the horizontal header.<br/>"
|
||||
"<em>All cells in the column will be selected</em>"
|
||||
"</li>"
|
||||
"<li>Extend the selection by moving the mouse to the far right and outside the window geometry.<br/>"
|
||||
"<em>(selection will extend to the next columns)</em>"
|
||||
"</li>"
|
||||
"<li>Verify that it works in the opposite direction (i.e. move mouse to the left of the window geometry).<br/>"
|
||||
"<em>(Verify that the view scrolls automatically and the selection changes)</em>"
|
||||
"</li>"
|
||||
"</ol>",
|
||||
false, QAbstractItemView::ExtendedSelection
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class Window : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Window(QWidget *parent = 0): QWidget(parent), ckMovable(0), tableView(0), cbSelectionMode(0), m_taskInstructions(0)
|
||||
{
|
||||
m_taskInstructions = new QLabel();
|
||||
if (sizeof(tasks) > 0)
|
||||
m_taskInstructions->setText(tr(tasks[0].instructions));
|
||||
|
||||
QVBoxLayout *vbox = new QVBoxLayout(this);
|
||||
vbox->addLayout(setupComboBox());
|
||||
vbox->addWidget(setupGroupBox());
|
||||
vbox->addWidget(setupTableView());
|
||||
vbox->addWidget(m_taskInstructions);
|
||||
}
|
||||
|
||||
void updateControls()
|
||||
{
|
||||
ckMovable->setChecked(tableView->verticalHeader()->sectionsMovable());
|
||||
QAbstractItemView::SelectionMode sMode = tableView->selectionMode();
|
||||
cbSelectionMode->setCurrentIndex((int)sMode);
|
||||
}
|
||||
|
||||
private:
|
||||
QFormLayout *setupComboBox()
|
||||
{
|
||||
QComboBox *combo = new QComboBox;
|
||||
for (size_t i = 0; i < sizeof(tasks) / sizeof(tasks[0]); ++i) {
|
||||
combo->addItem(tr(tasks[i].title));
|
||||
}
|
||||
|
||||
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_taskCombo_currentIndexChanged(int)));
|
||||
QFormLayout *form = new QFormLayout;
|
||||
form->addRow(tr("Choose task:"), combo);
|
||||
return form;
|
||||
}
|
||||
|
||||
QGroupBox *setupGroupBox()
|
||||
{
|
||||
QGroupBox *grp = new QGroupBox(tr("Properties"));
|
||||
QFormLayout *form = new QFormLayout;
|
||||
grp->setLayout(form);
|
||||
ckMovable = new QCheckBox;
|
||||
ckMovable->setObjectName(QLatin1String("ckMovable"));
|
||||
connect(ckMovable, SIGNAL(toggled(bool)), this, SLOT(on_ckMovable_toggled(bool)));
|
||||
form->addRow(tr("SectionsMovable"), ckMovable);
|
||||
|
||||
cbSelectionMode = new QComboBox;
|
||||
cbSelectionMode->setObjectName(QLatin1String("cbSelectionMode"));
|
||||
cbSelectionMode->addItems(QStringList() << QLatin1String("NoSelection")
|
||||
<< QLatin1String("SingleSelection")
|
||||
<< QLatin1String("MultiSelection")
|
||||
<< QLatin1String("ExtendedSelection")
|
||||
<< QLatin1String("ContiguousSelection")
|
||||
);
|
||||
|
||||
connect(cbSelectionMode, SIGNAL(currentIndexChanged(int)), this, SLOT(on_cbSelectionMode_currentIndexChanged(int)));
|
||||
form->addRow(tr("SelectionMode"), cbSelectionMode);
|
||||
return grp;
|
||||
}
|
||||
|
||||
QTableView *setupTableView()
|
||||
{
|
||||
tableView = new QTableView;
|
||||
m.setRowCount(500);
|
||||
m.setColumnCount(250);
|
||||
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
tableView->setModel(&m);
|
||||
return tableView;
|
||||
}
|
||||
|
||||
private Q_SLOTS:
|
||||
void on_ckMovable_toggled(bool arg)
|
||||
{
|
||||
tableView->verticalHeader()->setSectionsMovable(arg);
|
||||
tableView->horizontalHeader()->setSectionsMovable(arg);
|
||||
}
|
||||
|
||||
void on_cbSelectionMode_currentIndexChanged(int idx)
|
||||
{
|
||||
tableView->setSelectionMode((QAbstractItemView::SelectionMode)idx);
|
||||
}
|
||||
|
||||
void on_taskCombo_currentIndexChanged(int idx)
|
||||
{
|
||||
ManualTask &task = tasks[idx];
|
||||
m_taskInstructions->setText(tr(task.instructions));
|
||||
ckMovable->setChecked(task.sectionsMovable);
|
||||
cbSelectionMode->setCurrentIndex((QAbstractItemView::SelectionMode)task.selectionMode);
|
||||
}
|
||||
|
||||
public:
|
||||
QCheckBox *ckMovable;
|
||||
QTableView *tableView;
|
||||
QStandardItemModel m;
|
||||
QComboBox *cbSelectionMode;
|
||||
QLabel *m_taskInstructions;
|
||||
};
|
||||
|
||||
class SomeHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -86,19 +228,18 @@ void SomeHandler::slotSectionResized(int logsection, int oldsize, int newsize)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QTableView tv;
|
||||
QStandardItemModel m;
|
||||
m.setRowCount(500);
|
||||
m.setColumnCount(250);
|
||||
tv.setModel(&m);
|
||||
tv.setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
Window window;
|
||||
// Comment in the line below to test selection with keyboard (space)
|
||||
// tv.setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
SomeHandler handler(tv.horizontalHeader(), &tv);
|
||||
tv.horizontalHeader()->setDefaultSectionSize(30);
|
||||
tv.show();
|
||||
tv.horizontalHeader()->setSectionsMovable(true);
|
||||
tv.verticalHeader()->setSectionsMovable(true);
|
||||
QHeaderView *hHeader = window.tableView->horizontalHeader();
|
||||
QHeaderView *vHeader = window.tableView->verticalHeader();
|
||||
SomeHandler handler(hHeader, window.tableView);
|
||||
hHeader->setDefaultSectionSize(30);
|
||||
window.resize(600, 600);
|
||||
window.show();
|
||||
hHeader->setSectionsMovable(true);
|
||||
vHeader->setSectionsMovable(true);
|
||||
window.updateControls();
|
||||
app.exec();
|
||||
}
|
||||
#include "qheaderviewtest1.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user