| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <private/qguiapplication_p.h> |
| 5 |
|
| 6 |
#include <qpa/qplatformintegration.h> |
| 7 |
|
| 8 |
#include <QAbstractItemView> |
| 9 |
#include <QDialog> |
| 10 |
#include <QHeaderView> |
| 11 |
#include <QIdentityProxyModel> |
| 12 |
#include <QItemDelegate> |
| 13 |
#include <QLabel> |
| 14 |
#include <QLineEdit> |
| 15 |
#include <QListWidget> |
| 16 |
#include <QProxyStyle> |
| 17 |
#include <QPushButton> |
| 18 |
#include <QScrollBar> |
| 19 |
#include <QScroller> |
| 20 |
#include <QSignalSpy> |
| 21 |
#include <QSortFilterProxyModel> |
| 22 |
#include <QSpinBox> |
| 23 |
#include <QStandardItemModel> |
| 24 |
#include <QStringListModel> |
| 25 |
#include <QStyledItemDelegate> |
| 26 |
#include <QTableWidget> |
| 27 |
#include <QTimer> |
| 28 |
#include <QTreeWidget> |
| 29 |
#include <QTest> |
| 30 |
#include <QVBoxLayout> |
| 31 |
#include <QtTest/private/qtesthelpers_p.h> |
| 32 |
#include <private/qabstractitemview_p.h> |
| 33 |
#include <QtWidgets/private/qapplication_p.h> |
| 34 |
|
| 35 |
#include <vector> |
| 36 |
#include <memory> |
| 37 |
|
| 38 |
Q_DECLARE_METATYPE(Qt::ItemFlags); |
| 39 |
|
| 40 |
using namespace QTestPrivate; |
| 41 |
using IntList = QList<int>; |
| 42 |
|
| 43 |
// Move cursor out of widget area to avoid undesired interaction on Mac. |
| 44 |
static inline void moveCursorAway(const QWidget *topLevel) |
| 45 |
{ |
| 46 |
#ifndef QT_NO_CURSOR |
| 47 |
QCursor::setPos(topLevel->geometry().topRight() + QPoint(100, 0)); |
| 48 |
#else |
| 49 |
Q_UNUSED(topLevel); |
| 50 |
#endif |
| 51 |
} |
| 52 |
|
| 53 |
class GeometriesTestView : public QTableView |
| 54 |
{ |
| 55 |
Q_OBJECT |
| 56 |
public: |
| 57 |
using QTableView::QTableView; |
| 58 |
using QTableView::selectedIndexes; |
| 59 |
bool updateGeometriesCalled = false; |
| 60 |
protected slots: |
| 61 |
void updateGeometries() override { updateGeometriesCalled = true; QTableView::updateGeometries(); } |
| 62 |
}; |
| 63 |
|
| 64 |
class tst_QAbstractItemView : public QObject |
| 65 |
{ |
| 66 |
Q_OBJECT |
| 67 |
|
| 68 |
public: |
| 69 |
void basic_tests(QAbstractItemView *view); |
| 70 |
|
| 71 |
private slots: |
| 72 |
void cleanup(); |
| 73 |
void getSetCheck(); |
| 74 |
void emptyModels_data(); |
| 75 |
void emptyModels(); |
| 76 |
void setModel_data(); |
| 77 |
void setModel(); |
| 78 |
void noModel(); |
| 79 |
void dragSelect(); |
| 80 |
void rowDelegate(); |
| 81 |
void columnDelegate(); |
| 82 |
void selectAll(); |
| 83 |
void ctrlA(); |
| 84 |
void persistentEditorFocus(); |
| 85 |
void pressClosesReleaseDoesntOpenEditor(); |
| 86 |
void setItemDelegate(); |
| 87 |
void setItemDelegate_data(); |
| 88 |
// The dragAndDrop() test doesn't work, and is thus disabled on Mac and Windows |
| 89 |
// for the following reasons: |
| 90 |
// Mac: use of GetCurrentEventButtonState() in QDragManager::drag() |
| 91 |
// Win: unknown reason |
| 92 |
#if !defined(Q_OS_MAC) && !defined(Q_OS_WIN) |
| 93 |
#if 0 |
| 94 |
void dragAndDrop(); |
| 95 |
void dragAndDropOnChild(); |
| 96 |
#endif |
| 97 |
#endif |
| 98 |
void noFallbackToRoot(); |
| 99 |
void setCurrentIndex_data(); |
| 100 |
void setCurrentIndex(); |
| 101 |
|
| 102 |
void checkIntersectedRect_data(); |
| 103 |
void checkIntersectedRect(); |
| 104 |
|
| 105 |
void task221955_selectedEditor(); |
| 106 |
void task250754_fontChange(); |
| 107 |
void task200665_itemEntered(); |
| 108 |
void task257481_emptyEditor(); |
| 109 |
void shiftArrowSelectionAfterScrolling(); |
| 110 |
void shiftSelectionAfterRubberbandSelection(); |
| 111 |
void ctrlRubberbandSelection(); |
| 112 |
void QTBUG6407_extendedSelection(); |
| 113 |
void QTBUG6753_selectOnSelection(); |
| 114 |
void testDelegateDestroyEditor(); |
| 115 |
void testDelegateDestroyEditorChild(); |
| 116 |
void testClickedSignal(); |
| 117 |
void testChangeEditorState(); |
| 118 |
void deselectInSingleSelection(); |
| 119 |
void testNoActivateOnDisabledItem(); |
| 120 |
void testFocusPolicy_data(); |
| 121 |
void testFocusPolicy(); |
| 122 |
void QTBUG31411_noSelection(); |
| 123 |
void QTBUG39324_settingSameInstanceOfIndexWidget(); |
| 124 |
void sizeHintChangeTriggersLayout(); |
| 125 |
void shiftSelectionAfterChangingModelContents(); |
| 126 |
void QTBUG48968_reentrant_updateEditorGeometries(); |
| 127 |
void QTBUG50102_SH_ItemView_ScrollMode(); |
| 128 |
void QTBUG50535_update_on_new_selection_model(); |
| 129 |
void testSelectionModelInSyncWithView(); |
| 130 |
void testClickToSelect(); |
| 131 |
void testDialogAsEditor(); |
| 132 |
void QTBUG46785_mouseout_hover_state(); |
| 133 |
void testClearModelInClickedSignal(); |
| 134 |
void inputMethodEnabled_data(); |
| 135 |
void inputMethodEnabled(); |
| 136 |
void currentFollowsIndexWidget_data(); |
| 137 |
void currentFollowsIndexWidget(); |
| 138 |
void checkFocusAfterActivationChanges_data(); |
| 139 |
void checkFocusAfterActivationChanges(); |
| 140 |
void dragSelectAfterNewPress(); |
| 141 |
void dragWithSecondClick_data(); |
| 142 |
void dragWithSecondClick(); |
| 143 |
void clickAfterDoubleClick(); |
| 144 |
void selectionCommand_data(); |
| 145 |
void selectionCommand(); |
| 146 |
void mouseSelection_data(); |
| 147 |
void mouseSelection(); |
| 148 |
void keepSingleSelectionOnEmptyAreaClick(); |
| 149 |
void scrollerSmoothScroll(); |
| 150 |
void inputMethodOpensEditor_data(); |
| 151 |
void inputMethodOpensEditor(); |
| 152 |
void selectionAutoScrolling_data(); |
| 153 |
void selectionAutoScrolling(); |
| 154 |
void testSpinBoxAsEditor_data(); |
| 155 |
void testSpinBoxAsEditor(); |
| 156 |
void removeIndexWhileEditing(); |
| 157 |
void focusNextOnHide(); |
| 158 |
void shiftSelectionAfterModelSetCurrentIndex(); |
| 159 |
void QTBUG72333_isPersistentEditorOpen(); |
| 160 |
|
| 161 |
private: |
| 162 |
static QAbstractItemView *viewFromString(const QByteArray &viewType, QWidget *parent = nullptr) |
| 163 |
{ |
| 164 |
if (viewType == "QListView") |
| 165 |
return new QListView(parent); |
| 166 |
if (viewType == "QTableView") |
| 167 |
return new QTableView(parent); |
| 168 |
if (viewType == "QTreeView") |
| 169 |
return new QTreeView(parent); |
| 170 |
if (viewType == "QHeaderView") |
| 171 |
return new QHeaderView(Qt::Vertical, parent); |
| 172 |
Q_ASSERT(false); |
| 173 |
return nullptr; |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
class MyAbstractItemDelegate : public QAbstractItemDelegate |
| 178 |
{ |
| 179 |
public: |
| 180 |
using QAbstractItemDelegate::QAbstractItemDelegate; |
| 181 |
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override {} |
| 182 |
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override { return size; } |
| 183 |
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override |
| 184 |
{ |
| 185 |
openedEditor = new QWidget(parent); |
| 186 |
virtualCtorCallCount++; |
| 187 |
return openedEditor; |
| 188 |
} |
| 189 |
void destroyEditor(QWidget *editor, const QModelIndex &) const override |
| 190 |
{ |
| 191 |
virtualDtorCallCount++; |
| 192 |
editor->deleteLater(); |
| 193 |
} |
| 194 |
void changeSize() { size = QSize(50, 50); emit sizeHintChanged(QModelIndex()); } |
| 195 |
mutable QWidget *openedEditor = nullptr; |
| 196 |
QSize size; |
| 197 |
mutable int virtualCtorCallCount = 0; |
| 198 |
mutable int virtualDtorCallCount = 0; |
| 199 |
}; |
| 200 |
|
| 201 |
class DialogItemDelegate : public QStyledItemDelegate |
| 202 |
{ |
| 203 |
public: |
| 204 |
using QStyledItemDelegate::QStyledItemDelegate; |
| 205 |
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override |
| 206 |
{ |
| 207 |
openedEditor = new QDialog(parent); |
| 208 |
return openedEditor; |
| 209 |
} |
| 210 |
|
| 211 |
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override |
| 212 |
{ |
| 213 |
Q_UNUSED(model); |
| 214 |
Q_UNUSED(index); |
| 215 |
|
| 216 |
QDialog *dialog = qobject_cast<QDialog *>(editor); |
| 217 |
result = static_cast<QDialog::DialogCode>(dialog->result()); |
| 218 |
} |
| 219 |
|
| 220 |
mutable QDialog *openedEditor = nullptr; |
| 221 |
mutable QDialog::DialogCode result = QDialog::Rejected; |
| 222 |
}; |
| 223 |
|
| 224 |
// Testing get/set functions |
| 225 |
void tst_QAbstractItemView::getSetCheck() |
| 226 |
{ |
| 227 |
QListView view; |
| 228 |
QAbstractItemView *obj1 = &view; |
| 229 |
// QAbstractItemDelegate * QAbstractItemView::itemDelegate() |
| 230 |
// void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *) |
| 231 |
MyAbstractItemDelegate *var1 = new MyAbstractItemDelegate; |
| 232 |
obj1->setItemDelegate(var1); |
| 233 |
QCOMPARE(var1, obj1->itemDelegate()); |
| 234 |
obj1->setItemDelegate(nullptr); |
| 235 |
QCOMPARE(obj1->itemDelegate(), nullptr); |
| 236 |
delete var1; |
| 237 |
|
| 238 |
// EditTriggers ) |
| 239 |
// void QAbstractItemView::setEditTriggers(EditTriggers) |
| 240 |
obj1->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 241 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::NoEditTriggers); |
| 242 |
obj1->setEditTriggers(QAbstractItemView::CurrentChanged); |
| 243 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::CurrentChanged); |
| 244 |
obj1->setEditTriggers(QAbstractItemView::DoubleClicked); |
| 245 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::DoubleClicked); |
| 246 |
obj1->setEditTriggers(QAbstractItemView::SelectedClicked); |
| 247 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::SelectedClicked); |
| 248 |
obj1->setEditTriggers(QAbstractItemView::EditKeyPressed); |
| 249 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::EditKeyPressed); |
| 250 |
obj1->setEditTriggers(QAbstractItemView::AnyKeyPressed); |
| 251 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::AnyKeyPressed); |
| 252 |
obj1->setEditTriggers(QAbstractItemView::AllEditTriggers); |
| 253 |
QCOMPARE(obj1->editTriggers(), QAbstractItemView::AllEditTriggers); |
| 254 |
|
| 255 |
// bool QAbstractItemView::tabKeyNavigation() |
| 256 |
// void QAbstractItemView::setTabKeyNavigation(bool) |
| 257 |
obj1->setTabKeyNavigation(false); |
| 258 |
QCOMPARE(false, obj1->tabKeyNavigation()); |
| 259 |
obj1->setTabKeyNavigation(true); |
| 260 |
QCOMPARE(true, obj1->tabKeyNavigation()); |
| 261 |
|
| 262 |
// bool QAbstractItemView::dragEnabled() |
| 263 |
// void QAbstractItemView::setDragEnabled(bool) |
| 264 |
#if QT_CONFIG(draganddrop) |
| 265 |
obj1->setDragEnabled(false); |
| 266 |
QCOMPARE(false, obj1->dragEnabled()); |
| 267 |
obj1->setDragEnabled(true); |
| 268 |
QCOMPARE(true, obj1->dragEnabled()); |
| 269 |
#endif |
| 270 |
// bool QAbstractItemView::alternatingRowColors() |
| 271 |
// void QAbstractItemView::setAlternatingRowColors(bool) |
| 272 |
obj1->setAlternatingRowColors(false); |
| 273 |
QCOMPARE(false, obj1->alternatingRowColors()); |
| 274 |
obj1->setAlternatingRowColors(true); |
| 275 |
QCOMPARE(true, obj1->alternatingRowColors()); |
| 276 |
|
| 277 |
// State QAbstractItemView::state() |
| 278 |
// void QAbstractItemView::setState(State) |
| 279 |
obj1->setState(QAbstractItemView::NoState); |
| 280 |
QCOMPARE(QAbstractItemView::NoState, obj1->state()); |
| 281 |
obj1->setState(QAbstractItemView::DraggingState); |
| 282 |
QCOMPARE(QAbstractItemView::DraggingState, obj1->state()); |
| 283 |
obj1->setState(QAbstractItemView::DragSelectingState); |
| 284 |
QCOMPARE(QAbstractItemView::DragSelectingState, obj1->state()); |
| 285 |
obj1->setState(QAbstractItemView::EditingState); |
| 286 |
QCOMPARE(QAbstractItemView::EditingState, obj1->state()); |
| 287 |
obj1->setState(QAbstractItemView::ExpandingState); |
| 288 |
QCOMPARE(QAbstractItemView::ExpandingState, obj1->state()); |
| 289 |
obj1->setState(QAbstractItemView::CollapsingState); |
| 290 |
QCOMPARE(QAbstractItemView::CollapsingState, obj1->state()); |
| 291 |
|
| 292 |
// QWidget QAbstractScrollArea::viewport() |
| 293 |
// void setViewport(QWidget*) |
| 294 |
QWidget *vp = new QWidget; |
| 295 |
obj1->setViewport(vp); |
| 296 |
QCOMPARE(vp, obj1->viewport()); |
| 297 |
|
| 298 |
QCOMPARE(16, obj1->autoScrollMargin()); |
| 299 |
obj1->setAutoScrollMargin(20); |
| 300 |
QCOMPARE(20, obj1->autoScrollMargin()); |
| 301 |
obj1->setAutoScrollMargin(16); |
| 302 |
QCOMPARE(16, obj1->autoScrollMargin()); |
| 303 |
|
| 304 |
QCOMPARE(200U, obj1->updateThreshold()); |
| 305 |
obj1->setUpdateThreshold(4711); |
| 306 |
QCOMPARE(4711U, obj1->updateThreshold()); |
| 307 |
} |
| 308 |
|
| 309 |
void tst_QAbstractItemView::cleanup() |
| 310 |
{ |
| 311 |
QVERIFY(QApplication::topLevelWidgets().isEmpty()); |
| 312 |
} |
| 313 |
|
| 314 |
void tst_QAbstractItemView::emptyModels_data() |
| 315 |
{ |
| 316 |
QTest::addColumn<QByteArray>("viewType"); |
| 317 |
|
| 318 |
const QList<QByteArray> widgets { "QListView", "QTreeView", "QTableView", "QHeaderView" }; |
| 319 |
for (const QByteArray &widget : widgets) |
| 320 |
QTest::newRow(widget) << widget; |
| 321 |
} |
| 322 |
|
| 323 |
void tst_QAbstractItemView::emptyModels() |
| 324 |
{ |
| 325 |
QFETCH(QByteArray, viewType); |
| 326 |
|
| 327 |
QScopedPointer<QAbstractItemView> view(viewFromString(viewType)); |
| 328 |
centerOnScreen(view.data()); |
| 329 |
moveCursorAway(view.data()); |
| 330 |
view->show(); |
| 331 |
QVERIFY(QTest::qWaitForWindowExposed(view.data())); |
| 332 |
|
| 333 |
QVERIFY(!view->model()); |
| 334 |
QVERIFY(!view->selectionModel()); |
| 335 |
//QVERIFY(view->itemDelegate() != 0); |
| 336 |
|
| 337 |
basic_tests(view.data()); |
| 338 |
} |
| 339 |
|
| 340 |
void tst_QAbstractItemView::setModel_data() |
| 341 |
{ |
| 342 |
emptyModels_data(); |
| 343 |
} |
| 344 |
|
| 345 |
void tst_QAbstractItemView::setModel() |
| 346 |
{ |
| 347 |
QFETCH(QByteArray, viewType); |
| 348 |
|
| 349 |
QScopedPointer<QAbstractItemView> view(viewFromString(viewType)); |
| 350 |
centerOnScreen(view.data()); |
| 351 |
moveCursorAway(view.data()); |
| 352 |
view->show(); |
| 353 |
QVERIFY(QTest::qWaitForWindowExposed(view.data())); |
| 354 |
|
| 355 |
QStandardItemModel model(20,20); |
| 356 |
view->setModel(nullptr); |
| 357 |
view->setModel(&model); |
| 358 |
basic_tests(view.data()); |
| 359 |
} |
| 360 |
|
| 361 |
void tst_QAbstractItemView::basic_tests(QAbstractItemView *view) |
| 362 |
{ |
| 363 |
// setSelectionModel |
| 364 |
// Will assert as it should |
| 365 |
//view->setSelectionModel(0); |
| 366 |
// setItemDelegate |
| 367 |
//view->setItemDelegate(0); |
| 368 |
// Will asswert as it should |
| 369 |
|
| 370 |
// setSelectionMode |
| 371 |
view->setSelectionMode(QAbstractItemView::SingleSelection); |
| 372 |
QCOMPARE(view->selectionMode(), QAbstractItemView::SingleSelection); |
| 373 |
view->setSelectionMode(QAbstractItemView::ContiguousSelection); |
| 374 |
QCOMPARE(view->selectionMode(), QAbstractItemView::ContiguousSelection); |
| 375 |
view->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 376 |
QCOMPARE(view->selectionMode(), QAbstractItemView::ExtendedSelection); |
| 377 |
view->setSelectionMode(QAbstractItemView::MultiSelection); |
| 378 |
QCOMPARE(view->selectionMode(), QAbstractItemView::MultiSelection); |
| 379 |
view->setSelectionMode(QAbstractItemView::NoSelection); |
| 380 |
QCOMPARE(view->selectionMode(), QAbstractItemView::NoSelection); |
| 381 |
|
| 382 |
// setSelectionBehavior |
| 383 |
view->setSelectionBehavior(QAbstractItemView::SelectItems); |
| 384 |
QCOMPARE(view->selectionBehavior(), QAbstractItemView::SelectItems); |
| 385 |
view->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 386 |
QCOMPARE(view->selectionBehavior(), QAbstractItemView::SelectRows); |
| 387 |
view->setSelectionBehavior(QAbstractItemView::SelectColumns); |
| 388 |
QCOMPARE(view->selectionBehavior(), QAbstractItemView::SelectColumns); |
| 389 |
|
| 390 |
// setEditTriggers |
| 391 |
view->setEditTriggers(QAbstractItemView::EditKeyPressed); |
| 392 |
QCOMPARE(view->editTriggers(), QAbstractItemView::EditKeyPressed); |
| 393 |
view->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 394 |
QCOMPARE(view->editTriggers(), QAbstractItemView::NoEditTriggers); |
| 395 |
view->setEditTriggers(QAbstractItemView::CurrentChanged); |
| 396 |
QCOMPARE(view->editTriggers(), QAbstractItemView::CurrentChanged); |
| 397 |
view->setEditTriggers(QAbstractItemView::DoubleClicked); |
| 398 |
QCOMPARE(view->editTriggers(), QAbstractItemView::DoubleClicked); |
| 399 |
view->setEditTriggers(QAbstractItemView::SelectedClicked); |
| 400 |
QCOMPARE(view->editTriggers(), QAbstractItemView::SelectedClicked); |
| 401 |
view->setEditTriggers(QAbstractItemView::AnyKeyPressed); |
| 402 |
QCOMPARE(view->editTriggers(), QAbstractItemView::AnyKeyPressed); |
| 403 |
view->setEditTriggers(QAbstractItemView::AllEditTriggers); |
| 404 |
QCOMPARE(view->editTriggers(), QAbstractItemView::AllEditTriggers); |
| 405 |
|
| 406 |
// setAutoScroll |
| 407 |
view->setAutoScroll(false); |
| 408 |
QCOMPARE(view->hasAutoScroll(), false); |
| 409 |
view->setAutoScroll(true); |
| 410 |
QCOMPARE(view->hasAutoScroll(), true); |
| 411 |
|
| 412 |
// setTabKeyNavigation |
| 413 |
view->setTabKeyNavigation(false); |
| 414 |
QCOMPARE(view->tabKeyNavigation(), false); |
| 415 |
view->setTabKeyNavigation(true); |
| 416 |
QCOMPARE(view->tabKeyNavigation(), true); |
| 417 |
|
| 418 |
#if QT_CONFIG(draganddrop) |
| 419 |
// setDropIndicatorShown |
| 420 |
view->setDropIndicatorShown(false); |
| 421 |
QCOMPARE(view->showDropIndicator(), false); |
| 422 |
view->setDropIndicatorShown(true); |
| 423 |
QCOMPARE(view->showDropIndicator(), true); |
| 424 |
|
| 425 |
// setDragEnabled |
| 426 |
view->setDragEnabled(false); |
| 427 |
QCOMPARE(view->dragEnabled(), false); |
| 428 |
view->setDragEnabled(true); |
| 429 |
QCOMPARE(view->dragEnabled(), true); |
| 430 |
#endif |
| 431 |
|
| 432 |
// setAlternatingRowColors |
| 433 |
view->setAlternatingRowColors(false); |
| 434 |
QCOMPARE(view->alternatingRowColors(), false); |
| 435 |
view->setAlternatingRowColors(true); |
| 436 |
QCOMPARE(view->alternatingRowColors(), true); |
| 437 |
|
| 438 |
// setIconSize |
| 439 |
view->setIconSize(QSize(16, 16)); |
| 440 |
QCOMPARE(view->iconSize(), QSize(16, 16)); |
| 441 |
QSignalSpy spy(view, &QAbstractItemView::iconSizeChanged); |
| 442 |
QVERIFY(spy.isValid()); |
| 443 |
view->setIconSize(QSize(32, 32)); |
| 444 |
QCOMPARE(view->iconSize(), QSize(32, 32)); |
| 445 |
QCOMPARE(spy.size(), 1); |
| 446 |
QCOMPARE(spy.at(0).at(0).value<QSize>(), QSize(32, 32)); |
| 447 |
// Should this happen? |
| 448 |
view->setIconSize(QSize(-1, -1)); |
| 449 |
QCOMPARE(view->iconSize(), QSize(-1, -1)); |
| 450 |
QCOMPARE(spy.size(), 2); |
| 451 |
|
| 452 |
QCOMPARE(view->currentIndex(), QModelIndex()); |
| 453 |
QCOMPARE(view->rootIndex(), QModelIndex()); |
| 454 |
|
| 455 |
view->keyboardSearch(""); |
| 456 |
view->keyboardSearch("foo"); |
| 457 |
view->keyboardSearch("1"); |
| 458 |
|
| 459 |
QCOMPARE(view->visualRect(QModelIndex()), QRect()); |
| 460 |
|
| 461 |
view->scrollTo(QModelIndex()); |
| 462 |
|
| 463 |
QCOMPARE(view->sizeHintForIndex(QModelIndex()), QSize()); |
| 464 |
QCOMPARE(view->indexAt(QPoint(-1, -1)), QModelIndex()); |
| 465 |
|
| 466 |
if (!view->model()) { |
| 467 |
QCOMPARE(view->indexAt(QPoint(10, 10)), QModelIndex()); |
| 468 |
QCOMPARE(view->sizeHintForRow(0), -1); |
| 469 |
QCOMPARE(view->sizeHintForColumn(0), -1); |
| 470 |
} else if (view->itemDelegate()) { |
| 471 |
view->sizeHintForRow(0); |
| 472 |
view->sizeHintForColumn(0); |
| 473 |
} |
| 474 |
view->openPersistentEditor(QModelIndex()); |
| 475 |
view->closePersistentEditor(QModelIndex()); |
| 476 |
|
| 477 |
view->reset(); |
| 478 |
view->setRootIndex(QModelIndex()); |
| 479 |
view->doItemsLayout(); |
| 480 |
view->selectAll(); |
| 481 |
view->edit(QModelIndex()); |
| 482 |
view->clearSelection(); |
| 483 |
view->setCurrentIndex(QModelIndex()); |
| 484 |
|
| 485 |
// protected methods |
| 486 |
// will assert because an invalid index is passed |
| 487 |
//view->dataChanged(QModelIndex(), QModelIndex()); |
| 488 |
view->rowsInserted(QModelIndex(), -1, -1); |
| 489 |
view->rowsAboutToBeRemoved(QModelIndex(), -1, -1); |
| 490 |
view->selectionChanged(QItemSelection(), QItemSelection()); |
| 491 |
if (view->model()) { |
| 492 |
view->currentChanged(QModelIndex(), QModelIndex()); |
| 493 |
view->currentChanged(QModelIndex(), view->model()->index(0,0)); |
| 494 |
} |
| 495 |
view->updateEditorData(); |
| 496 |
view->updateEditorGeometries(); |
| 497 |
view->updateGeometries(); |
| 498 |
view->verticalScrollbarAction(QAbstractSlider::SliderSingleStepAdd); |
| 499 |
view->horizontalScrollbarAction(QAbstractSlider::SliderSingleStepAdd); |
| 500 |
view->verticalScrollbarValueChanged(10); |
| 501 |
view->horizontalScrollbarValueChanged(10); |
| 502 |
view->closeEditor(nullptr, QAbstractItemDelegate::NoHint); |
| 503 |
view->commitData(nullptr); |
| 504 |
view->editorDestroyed(nullptr); |
| 505 |
|
| 506 |
// Will assert as it should |
| 507 |
// view->setIndexWidget(QModelIndex(), 0); |
| 508 |
|
| 509 |
view->moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier); |
| 510 |
view->horizontalOffset(); |
| 511 |
view->verticalOffset(); |
| 512 |
|
| 513 |
// view->isIndexHidden(QModelIndex()); // will (correctly) assert |
| 514 |
if (view->model()) |
| 515 |
view->isIndexHidden(view->model()->index(0,0)); |
| 516 |
|
| 517 |
view->setSelection(QRect(0, 0, 10, 10), QItemSelectionModel::ClearAndSelect); |
| 518 |
view->setSelection(QRect(-1, -1, -1, -1), QItemSelectionModel::ClearAndSelect); |
| 519 |
view->visualRegionForSelection(QItemSelection()); |
| 520 |
view->selectedIndexes(); |
| 521 |
|
| 522 |
view->edit(QModelIndex(), QAbstractItemView::NoEditTriggers, nullptr); |
| 523 |
|
| 524 |
view->selectionCommand(QModelIndex(), nullptr); |
| 525 |
|
| 526 |
#if QT_CONFIG(draganddrop) |
| 527 |
if (!view->model()) |
| 528 |
view->startDrag(Qt::CopyAction); |
| 529 |
QStyleOptionViewItem option; |
| 530 |
view->initViewItemOption(&option); |
| 531 |
|
| 532 |
view->setState(QAbstractItemView::NoState); |
| 533 |
QCOMPARE(view->state(), QAbstractItemView::NoState); |
| 534 |
view->setState(QAbstractItemView::DraggingState); |
| 535 |
QCOMPARE(view->state(), QAbstractItemView::DraggingState); |
| 536 |
view->setState(QAbstractItemView::DragSelectingState); |
| 537 |
QCOMPARE(view->state(), QAbstractItemView::DragSelectingState); |
| 538 |
view->setState(QAbstractItemView::EditingState); |
| 539 |
QCOMPARE(view->state(), QAbstractItemView::EditingState); |
| 540 |
view->setState(QAbstractItemView::ExpandingState); |
| 541 |
QCOMPARE(view->state(), QAbstractItemView::ExpandingState); |
| 542 |
view->setState(QAbstractItemView::CollapsingState); |
| 543 |
QCOMPARE(view->state(), QAbstractItemView::CollapsingState); |
| 544 |
#endif |
| 545 |
|
| 546 |
view->startAutoScroll(); |
| 547 |
view->stopAutoScroll(); |
| 548 |
view->doAutoScroll(); |
| 549 |
|
| 550 |
// testing mouseFoo and key functions |
| 551 |
// QTest::mousePress(view, Qt::LeftButton, Qt::NoModifier, QPoint(0,0)); |
| 552 |
// mouseMove(view, Qt::LeftButton, Qt::NoModifier, QPoint(10,10)); |
| 553 |
// QTest::mouseRelease(view, Qt::LeftButton, Qt::NoModifier, QPoint(10,10)); |
| 554 |
// QTest::mouseClick(view, Qt::LeftButton, Qt::NoModifier, QPoint(10,10)); |
| 555 |
// mouseDClick(view, Qt::LeftButton, Qt::NoModifier, QPoint(10,10)); |
| 556 |
// QTest::keyClick(view, Qt::Key_A); |
| 557 |
} |
| 558 |
|
| 559 |
void tst_QAbstractItemView::noModel() |
| 560 |
{ |
| 561 |
// From task #85415 |
| 562 |
|
| 563 |
QStandardItemModel model(20,20); |
| 564 |
QTreeView view; |
| 565 |
setFrameless(&view); |
| 566 |
|
| 567 |
view.setModel(&model); |
| 568 |
// Make the viewport smaller than the contents, so that we can scroll |
| 569 |
view.resize(100,100); |
| 570 |
centerOnScreen(&view); |
| 571 |
moveCursorAway(&view); |
| 572 |
view.show(); |
| 573 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 574 |
|
| 575 |
// make sure that the scrollbars are not at value 0 |
| 576 |
view.scrollTo(view.model()->index(10,10)); |
| 577 |
QApplication::processEvents(); |
| 578 |
|
| 579 |
view.setModel(nullptr); |
| 580 |
// Due to the model is removed, this will generate a valueChanged signal on both scrollbars. (value to 0) |
| 581 |
QApplication::processEvents(); |
| 582 |
QCOMPARE(view.model(), nullptr); |
| 583 |
} |
| 584 |
|
| 585 |
void tst_QAbstractItemView::dragSelect() |
| 586 |
{ |
| 587 |
// From task #86108 |
| 588 |
|
| 589 |
QStandardItemModel model(64, 64); |
| 590 |
|
| 591 |
QTableView view; |
| 592 |
view.setModel(&model); |
| 593 |
centerOnScreen(&view); |
| 594 |
moveCursorAway(&view); |
| 595 |
view.setVisible(true); |
| 596 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 597 |
|
| 598 |
const int delay = 2; |
| 599 |
for (int i = 0; i < 2; ++i) { |
| 600 |
bool tracking = (i == 1); |
| 601 |
view.setMouseTracking(false); |
| 602 |
QTest::mouseMove(&view, QPoint(0, 0), delay); |
| 603 |
view.setMouseTracking(tracking); |
| 604 |
QTest::mouseMove(&view, QPoint(50, 50), delay); |
| 605 |
QVERIFY(view.selectionModel()->selectedIndexes().isEmpty()); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
void tst_QAbstractItemView::rowDelegate() |
| 610 |
{ |
| 611 |
QStandardItemModel model(4, 4); |
| 612 |
MyAbstractItemDelegate delegate; |
| 613 |
|
| 614 |
QTableView view; |
| 615 |
view.setModel(&model); |
| 616 |
view.setItemDelegateForRow(3, &delegate); |
| 617 |
centerOnScreen(&view); |
| 618 |
moveCursorAway(&view); |
| 619 |
view.show(); |
| 620 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 621 |
|
| 622 |
QModelIndex index = model.index(3, 0); |
| 623 |
QVERIFY(!view.isPersistentEditorOpen(index)); |
| 624 |
view.openPersistentEditor(index); |
| 625 |
QVERIFY(view.isPersistentEditorOpen(index)); |
| 626 |
QWidget *w = view.indexWidget(index); |
| 627 |
QVERIFY(w); |
| 628 |
QCOMPARE(w->metaObject()->className(), "QWidget"); |
| 629 |
} |
| 630 |
|
| 631 |
void tst_QAbstractItemView::columnDelegate() |
| 632 |
{ |
| 633 |
QStandardItemModel model(4, 4); |
| 634 |
MyAbstractItemDelegate delegate; |
| 635 |
|
| 636 |
QTableView view; |
| 637 |
view.setModel(&model); |
| 638 |
view.setItemDelegateForColumn(3, &delegate); |
| 639 |
centerOnScreen(&view); |
| 640 |
moveCursorAway(&view); |
| 641 |
view.show(); |
| 642 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 643 |
|
| 644 |
QModelIndex index = model.index(0, 3); |
| 645 |
QVERIFY(!view.isPersistentEditorOpen(index)); |
| 646 |
view.openPersistentEditor(index); |
| 647 |
QVERIFY(view.isPersistentEditorOpen(index)); |
| 648 |
QWidget *w = view.indexWidget(index); |
| 649 |
QVERIFY(w); |
| 650 |
QCOMPARE(w->metaObject()->className(), "QWidget"); |
| 651 |
} |
| 652 |
|
| 653 |
void tst_QAbstractItemView::sizeHintChangeTriggersLayout() |
| 654 |
{ |
| 655 |
QStandardItemModel model(4, 4); |
| 656 |
MyAbstractItemDelegate delegate; |
| 657 |
MyAbstractItemDelegate rowDelegate; |
| 658 |
MyAbstractItemDelegate columnDelegate; |
| 659 |
|
| 660 |
GeometriesTestView view; |
| 661 |
view.setModel(&model); |
| 662 |
view.setItemDelegate(&delegate); |
| 663 |
view.setItemDelegateForRow(1, &rowDelegate); |
| 664 |
view.setItemDelegateForColumn(2, &columnDelegate); |
| 665 |
view.show(); |
| 666 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 667 |
view.updateGeometriesCalled = false; |
| 668 |
delegate.changeSize(); |
| 669 |
QTRY_VERIFY(view.updateGeometriesCalled); |
| 670 |
view.updateGeometriesCalled = false; |
| 671 |
rowDelegate.changeSize(); |
| 672 |
QTRY_VERIFY(view.updateGeometriesCalled); |
| 673 |
view.updateGeometriesCalled = false; |
| 674 |
columnDelegate.changeSize(); |
| 675 |
QTRY_VERIFY(view.updateGeometriesCalled); |
| 676 |
} |
| 677 |
|
| 678 |
void tst_QAbstractItemView::selectAll() |
| 679 |
{ |
| 680 |
QStandardItemModel model(4, 4); |
| 681 |
GeometriesTestView view; |
| 682 |
view.setModel(&model); |
| 683 |
|
| 684 |
QCOMPARE(view.selectedIndexes().size(), 0); |
| 685 |
view.selectAll(); |
| 686 |
QCOMPARE(view.selectedIndexes().size(), 4 * 4); |
| 687 |
} |
| 688 |
|
| 689 |
void tst_QAbstractItemView::ctrlA() |
| 690 |
{ |
| 691 |
QStandardItemModel model(4, 4); |
| 692 |
GeometriesTestView view; |
| 693 |
view.setModel(&model); |
| 694 |
|
| 695 |
QCOMPARE(view.selectedIndexes().size(), 0); |
| 696 |
QTest::keyClick(&view, Qt::Key_A, Qt::ControlModifier); |
| 697 |
QCOMPARE(view.selectedIndexes().size(), 4 * 4); |
| 698 |
} |
| 699 |
|
| 700 |
void tst_QAbstractItemView::persistentEditorFocus() |
| 701 |
{ |
| 702 |
// one row, three columns |
| 703 |
QStandardItemModel model(1, 3); |
| 704 |
for(int i = 0; i < model.columnCount(); ++i) |
| 705 |
model.setData(model.index(0, i), i); |
| 706 |
QTableView view; |
| 707 |
view.setModel(&model); |
| 708 |
|
| 709 |
view.openPersistentEditor(model.index(0, 1)); |
| 710 |
view.openPersistentEditor(model.index(0, 2)); |
| 711 |
|
| 712 |
//these are spinboxes because we put numbers inside |
| 713 |
const QList<QSpinBox*> list = view.viewport()->findChildren<QSpinBox*>(); |
| 714 |
QCOMPARE(list.size(), 2); //these should be the 2 editors |
| 715 |
|
| 716 |
view.setCurrentIndex(model.index(0, 0)); |
| 717 |
QCOMPARE(view.currentIndex(), model.index(0, 0)); |
| 718 |
centerOnScreen(&view); |
| 719 |
moveCursorAway(&view); |
| 720 |
view.show(); |
| 721 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 722 |
|
| 723 |
const QPoint p(5, 5); |
| 724 |
for (QSpinBox *sb : list) { |
| 725 |
QTRY_VERIFY(sb->isVisible()); |
| 726 |
QMouseEvent mouseEvent(QEvent::MouseButtonPress, p, sb->mapToGlobal(p), |
| 727 |
Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); |
| 728 |
QCoreApplication::sendEvent(sb, &mouseEvent); |
| 729 |
if (!QApplication::focusWidget()) |
| 730 |
QSKIP("Some window managers don't handle focus that well"); |
| 731 |
QTRY_COMPARE(QApplication::focusWidget(), sb); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/*! |
| 736 |
A press into the selection area of an item being edited, but outside the editor, |
| 737 |
closes the editor by transferring focus to the view. The corresponding release |
| 738 |
should then not re-open the editor. |
| 739 |
|
| 740 |
QTBUG-20456. |
| 741 |
*/ |
| 742 |
void tst_QAbstractItemView::pressClosesReleaseDoesntOpenEditor() |
| 743 |
{ |
| 744 |
QStandardItemModel model(0, 1); |
| 745 |
auto *parent = new QStandardItem("parent"); |
| 746 |
for (const auto &childText : {"child1", "child2"}) { |
| 747 |
auto *child = new QStandardItem(childText); |
| 748 |
child->setFlags(Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsEditable | Qt::ItemIsSelectable); |
| 749 |
parent->appendRow(child); |
| 750 |
} |
| 751 |
model.appendRow(parent); |
| 752 |
|
| 753 |
QTreeView view; |
| 754 |
view.setModel(&model); |
| 755 |
view.setExpanded(model.indexFromItem(parent), true); |
| 756 |
view.setSelectionMode(QAbstractItemView::SingleSelection); |
| 757 |
view.setEditTriggers(QAbstractItemView::SelectedClicked | QAbstractItemView::DoubleClicked); |
| 758 |
|
| 759 |
view.show(); |
| 760 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 761 |
|
| 762 |
const QRect childRect = view.visualRect(model.indexFromItem(parent->child(0))); |
| 763 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, childRect.center()); // select |
| 764 |
QVERIFY(view.selectionModel()->selectedIndexes().contains(model.indexFromItem(parent->child(0)))); |
| 765 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, childRect.center()); // edit |
| 766 |
QTRY_COMPARE(view.state(), QAbstractItemView::EditingState); |
| 767 |
QPoint inChildOutsideEditor = QPoint(view.indentation() / 2, childRect.center().y()); |
| 768 |
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, inChildOutsideEditor); // focus itemview, editor closes |
| 769 |
QCOMPARE(view.state(), QAbstractItemView::NoState); |
| 770 |
QTest::qWait(10); // process some events, let the internal timer time out |
| 771 |
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, inChildOutsideEditor); // should not reopen editor |
| 772 |
QTest::qWait(QApplication::doubleClickInterval() * 2); |
| 773 |
QCOMPARE(view.state(), QAbstractItemView::NoState); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
#if !defined(Q_OS_MAC) && !defined(Q_OS_WIN) |
| 778 |
|
| 779 |
#if 0 |
| 780 |
|
| 781 |
static void sendMouseMove(QWidget *widget, QPoint pos = QPoint()) |
| 782 |
{ |
| 783 |
if (pos.isNull()) |
| 784 |
pos = widget->rect().center(); |
| 785 |
QMouseEvent event(QEvent::MouseMove, pos, widget->mapToGlobal(pos), Qt::NoButton, 0, 0); |
| 786 |
QCursor::setPos(widget->mapToGlobal(pos)); |
| 787 |
qApp->processEvents(); |
| 788 |
QVERIFY(QTest::qWaitForWindowExposed(widget)); |
| 789 |
QApplication::sendEvent(widget, &event); |
| 790 |
} |
| 791 |
|
| 792 |
static void sendMousePress( |
| 793 |
QWidget *widget, QPoint pos = QPoint(), Qt::MouseButton button = Qt::LeftButton) |
| 794 |
{ |
| 795 |
if (pos.isNull()) |
| 796 |
pos = widget->rect().center(); |
| 797 |
QMouseEvent event(QEvent::MouseButtonPress, pos, widget->mapToGlobal(pos), button, 0, 0); |
| 798 |
QApplication::sendEvent(widget, &event); |
| 799 |
} |
| 800 |
|
| 801 |
static void sendMouseRelease( |
| 802 |
QWidget *widget, QPoint pos = QPoint(), Qt::MouseButton button = Qt::LeftButton) |
| 803 |
{ |
| 804 |
if (pos.isNull()) |
| 805 |
pos = widget->rect().center(); |
| 806 |
QMouseEvent event(QEvent::MouseButtonRelease, pos, widget->mapToGlobal(pos), button, 0, 0); |
| 807 |
QApplication::sendEvent(widget, &event); |
| 808 |
} |
| 809 |
|
| 810 |
class DnDTestModel : public QStandardItemModel |
| 811 |
{ |
| 812 |
Q_OBJECT |
| 813 |
bool dropMimeData(const QMimeData *md, Qt::DropAction action, int r, int c, const QModelIndex &p) |
| 814 |
{ |
| 815 |
dropAction_result = action; |
| 816 |
QStandardItemModel::dropMimeData(md, action, r, c, p); |
| 817 |
return true; |
| 818 |
} |
| 819 |
Qt::DropActions supportedDropActions() const { return Qt::CopyAction | Qt::MoveAction; } |
| 820 |
|
| 821 |
Qt::DropAction dropAction_result; |
| 822 |
public: |
| 823 |
DnDTestModel() : QStandardItemModel(20, 20), dropAction_result(Qt::IgnoreAction) { |
| 824 |
for (int i = 0; i < rowCount(); ++i) |
| 825 |
setData(index(i, 0), QString::number(i)); |
| 826 |
} |
| 827 |
Qt::DropAction dropAction() const { return dropAction_result; } |
| 828 |
}; |
| 829 |
|
| 830 |
class DnDTestView : public QTreeView |
| 831 |
{ |
| 832 |
Q_OBJECT |
| 833 |
|
| 834 |
QPoint dropPoint; |
| 835 |
Qt::DropAction dropAction; |
| 836 |
|
| 837 |
void dragEnterEvent(QDragEnterEvent *event) |
| 838 |
{ |
| 839 |
QAbstractItemView::dragEnterEvent(event); |
| 840 |
} |
| 841 |
|
| 842 |
void dropEvent(QDropEvent *event) |
| 843 |
{ |
| 844 |
event->setDropAction(dropAction); |
| 845 |
QTreeView::dropEvent(event); |
| 846 |
} |
| 847 |
|
| 848 |
void timerEvent(QTimerEvent *event) |
| 849 |
{ |
| 850 |
killTimer(event->timerId()); |
| 851 |
sendMouseMove(this, dropPoint); |
| 852 |
sendMouseRelease(this); |
| 853 |
} |
| 854 |
|
| 855 |
void mousePressEvent(QMouseEvent *e) |
| 856 |
{ |
| 857 |
QTreeView::mousePressEvent(e); |
| 858 |
|
| 859 |
startTimer(0); |
| 860 |
setState(DraggingState); |
| 861 |
startDrag(dropAction); |
| 862 |
} |
| 863 |
|
| 864 |
public: |
| 865 |
DnDTestView(Qt::DropAction dropAction, QAbstractItemModel *model) |
| 866 |
: dropAction(dropAction) |
| 867 |
{ |
| 868 |
header()->hide(); |
| 869 |
setModel(model); |
| 870 |
setDragDropMode(QAbstractItemView::DragDrop); |
| 871 |
setAcceptDrops(true); |
| 872 |
setDragEnabled(true); |
| 873 |
} |
| 874 |
|
| 875 |
void dragAndDrop(QPoint drag, QPoint drop) |
| 876 |
{ |
| 877 |
dropPoint = drop; |
| 878 |
setCurrentIndex(indexAt(drag)); |
| 879 |
sendMousePress(viewport(), drag); |
| 880 |
} |
| 881 |
}; |
| 882 |
|
| 883 |
class DnDTestWidget : public QWidget |
| 884 |
{ |
| 885 |
Q_OBJECT |
| 886 |
|
| 887 |
Qt::DropAction dropAction_request; |
| 888 |
Qt::DropAction dropAction_result; |
| 889 |
QWidget *dropTarget; |
| 890 |
|
| 891 |
void timerEvent(QTimerEvent *event) |
| 892 |
{ |
| 893 |
killTimer(event->timerId()); |
| 894 |
sendMouseMove(dropTarget); |
| 895 |
sendMouseRelease(dropTarget); |
| 896 |
} |
| 897 |
|
| 898 |
void mousePressEvent(QMouseEvent *) |
| 899 |
{ |
| 900 |
QDrag *drag = new QDrag(this); |
| 901 |
QMimeData *mimeData = new QMimeData; |
| 902 |
mimeData->setData("application/x-qabstractitemmodeldatalist", QByteArray("")); |
| 903 |
drag->setMimeData(mimeData); |
| 904 |
startTimer(0); |
| 905 |
dropAction_result = drag->start(dropAction_request); |
| 906 |
} |
| 907 |
|
| 908 |
public: |
| 909 |
Qt::DropAction dropAction() const { return dropAction_result; } |
| 910 |
|
| 911 |
void dragAndDrop(QWidget *dropTarget, Qt::DropAction dropAction) |
| 912 |
{ |
| 913 |
this->dropTarget = dropTarget; |
| 914 |
dropAction_request = dropAction; |
| 915 |
sendMousePress(this); |
| 916 |
} |
| 917 |
}; |
| 918 |
|
| 919 |
void tst_QAbstractItemView::dragAndDrop() |
| 920 |
{ |
| 921 |
// From Task 137729 |
| 922 |
|
| 923 |
|
| 924 |
const int attempts = 10; |
| 925 |
int successes = 0; |
| 926 |
for (int i = 0; i < attempts; ++i) { |
| 927 |
Qt::DropAction dropAction = Qt::MoveAction; |
| 928 |
|
| 929 |
DnDTestModel model; |
| 930 |
DnDTestView view(dropAction, &model); |
| 931 |
DnDTestWidget widget; |
| 932 |
|
| 933 |
const int size = 200; |
| 934 |
widget.setFixedSize(size, size); |
| 935 |
view.setFixedSize(size, size); |
| 936 |
|
| 937 |
widget.move(0, 0); |
| 938 |
view.move(int(size * 1.5), int(size * 1.5)); |
| 939 |
|
| 940 |
widget.show(); |
| 941 |
view.show(); |
| 942 |
QVERIFY(QTest::qWaitForWindowExposed(&widget)); |
| 943 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 944 |
|
| 945 |
widget.dragAndDrop(&view, dropAction); |
| 946 |
if (model.dropAction() == dropAction |
| 947 |
&& widget.dropAction() == dropAction) |
| 948 |
++successes; |
| 949 |
} |
| 950 |
|
| 951 |
if (successes < attempts) { |
| 952 |
QString msg = QString("# successes (%1) < # attempts (%2)").arg(successes).arg(attempts); |
| 953 |
qWarning() << qPrintable(msg); |
| 954 |
} |
| 955 |
QVERIFY(successes > 0); // allow for some "event unstability" (i.e. unless |
| 956 |
// successes == 0, QAbstractItemView is probably ok!) |
| 957 |
} |
| 958 |
|
| 959 |
void tst_QAbstractItemView::dragAndDropOnChild() |
| 960 |
{ |
| 961 |
|
| 962 |
const int attempts = 10; |
| 963 |
int successes = 0; |
| 964 |
for (int i = 0; i < attempts; ++i) { |
| 965 |
Qt::DropAction dropAction = Qt::MoveAction; |
| 966 |
|
| 967 |
DnDTestModel model; |
| 968 |
QModelIndex parent = model.index(0, 0); |
| 969 |
model.insertRow(0, parent); |
| 970 |
model.insertColumn(0, parent); |
| 971 |
QModelIndex child = model.index(0, 0, parent); |
| 972 |
model.setData(child, "child"); |
| 973 |
QCOMPARE(model.rowCount(parent), 1); |
| 974 |
DnDTestView view(dropAction, &model); |
| 975 |
view.setExpanded(parent, true); |
| 976 |
view.setDragDropMode(QAbstractItemView::InternalMove); |
| 977 |
|
| 978 |
const int size = 200; |
| 979 |
view.setFixedSize(size, size); |
| 980 |
view.move(int(size * 1.5), int(size * 1.5)); |
| 981 |
view.show(); |
| 982 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 983 |
|
| 984 |
view.dragAndDrop(view.visualRect(parent).center(), |
| 985 |
view.visualRect(child).center()); |
| 986 |
if (model.dropAction() == dropAction) |
| 987 |
++successes; |
| 988 |
} |
| 989 |
|
| 990 |
QCOMPARE(successes, 0); |
| 991 |
} |
| 992 |
|
| 993 |
#endif // 0 |
| 994 |
#endif // !Q_OS_MAC && !Q_OS_WIN |
| 995 |
|
| 996 |
class TestModel : public QStandardItemModel |
| 997 |
{ |
| 998 |
Q_OBJECT |
| 999 |
public: |
| 1000 |
using QStandardItemModel::QStandardItemModel; |
| 1001 |
bool setData(const QModelIndex &, const QVariant &, int) override |
| 1002 |
{ |
| 1003 |
++setData_count; |
| 1004 |
return true; |
| 1005 |
} |
| 1006 |
void emitDataChanged() |
| 1007 |
{ |
| 1008 |
emit dataChanged(index(0, 0), index(0, 1)); |
| 1009 |
} |
| 1010 |
|
| 1011 |
int setData_count = 0; |
| 1012 |
}; |
| 1013 |
|
| 1014 |
void tst_QAbstractItemView::setItemDelegate_data() |
| 1015 |
{ |
| 1016 |
// default is rows, a -1 will switch to columns |
| 1017 |
QTest::addColumn<IntList>("rowsOrColumnsWithDelegate"); |
| 1018 |
QTest::addColumn<QPoint>("cellToEdit"); |
| 1019 |
QTest::newRow("4 columndelegates") |
| 1020 |
<< (IntList{ -1, 0, 1, 2, 3 }) |
| 1021 |
<< QPoint(0, 0); |
| 1022 |
QTest::newRow("2 identical rowdelegates on the same row") |
| 1023 |
<< (IntList{ 0, 0 }) |
| 1024 |
<< QPoint(0, 0); |
| 1025 |
QTest::newRow("2 identical columndelegates on the same column") |
| 1026 |
<< (IntList{ -1, 2, 2 }) |
| 1027 |
<< QPoint(2, 0); |
| 1028 |
QTest::newRow("2 duplicate delegates, 1 row and 1 column") |
| 1029 |
<< (IntList{ 0, -1, 2 }) |
| 1030 |
<< QPoint(2, 0); |
| 1031 |
QTest::newRow("4 duplicate delegates, 2 row and 2 column") |
| 1032 |
<< (IntList{ 0, 0, -1, 2, 2 }) |
| 1033 |
<< QPoint(2, 0); |
| 1034 |
|
| 1035 |
} |
| 1036 |
|
| 1037 |
void tst_QAbstractItemView::setItemDelegate() |
| 1038 |
{ |
| 1039 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1040 |
QSKIP("Window activation is not supported"); |
| 1041 |
|
| 1042 |
QFETCH(const IntList, rowsOrColumnsWithDelegate); |
| 1043 |
QFETCH(QPoint, cellToEdit); |
| 1044 |
QTableView v; |
| 1045 |
QStyledItemDelegate *delegate = new QStyledItemDelegate(&v); |
| 1046 |
TestModel model(5, 5); |
| 1047 |
v.setModel(&model); |
| 1048 |
|
| 1049 |
bool row = true; |
| 1050 |
for (int rc : rowsOrColumnsWithDelegate) { |
| 1051 |
if (rc == -1) { |
| 1052 |
row = !row; |
| 1053 |
} else { |
| 1054 |
if (row) |
| 1055 |
v.setItemDelegateForRow(rc, delegate); |
| 1056 |
else |
| 1057 |
v.setItemDelegateForColumn(rc, delegate); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
centerOnScreen(&v); |
| 1061 |
moveCursorAway(&v); |
| 1062 |
v.show(); |
| 1063 |
QVERIFY(QTest::qWaitForWindowActive(&v)); |
| 1064 |
|
| 1065 |
QModelIndex index = model.index(cellToEdit.y(), cellToEdit.x()); |
| 1066 |
v.edit(index); |
| 1067 |
|
| 1068 |
// This will close the editor |
| 1069 |
QTRY_VERIFY(QApplication::focusWidget()); |
| 1070 |
QWidget *editor = QApplication::focusWidget(); |
| 1071 |
QVERIFY(editor); |
| 1072 |
editor->hide(); |
| 1073 |
delete editor; |
| 1074 |
QCOMPARE(model.setData_count, 1); |
| 1075 |
delete delegate; |
| 1076 |
} |
| 1077 |
|
| 1078 |
void tst_QAbstractItemView::noFallbackToRoot() |
| 1079 |
{ |
| 1080 |
QStandardItemModel model(0, 1); |
| 1081 |
for (int i = 0; i < 5; ++i) |
| 1082 |
model.appendRow(new QStandardItem("top" + QString::number(i))); |
| 1083 |
QStandardItem *par1 = model.item(1); |
| 1084 |
for (int j = 0; j < 15; ++j) |
| 1085 |
par1->appendRow(new QStandardItem("sub" + QString::number(j))); |
| 1086 |
QStandardItem *par2 = par1->child(2); |
| 1087 |
for (int k = 0; k < 10; ++k) |
| 1088 |
par2->appendRow(new QStandardItem("bot" + QString::number(k))); |
| 1089 |
QStandardItem *it1 = par2->child(5); |
| 1090 |
|
| 1091 |
QModelIndex parent1 = model.indexFromItem(par1); |
| 1092 |
QModelIndex parent2 = model.indexFromItem(par2); |
| 1093 |
QModelIndex item1 = model.indexFromItem(it1); |
| 1094 |
|
| 1095 |
QTreeView v; |
| 1096 |
v.setModel(&model); |
| 1097 |
v.setRootIndex(parent1); |
| 1098 |
v.setCurrentIndex(item1); |
| 1099 |
QCOMPARE(v.currentIndex(), item1); |
| 1100 |
QVERIFY(model.removeRows(0, 10, parent2)); |
| 1101 |
QCOMPARE(v.currentIndex(), parent2); |
| 1102 |
QVERIFY(model.removeRows(0, 15, parent1)); |
| 1103 |
QCOMPARE(v.currentIndex(), QModelIndex()); |
| 1104 |
} |
| 1105 |
|
| 1106 |
void tst_QAbstractItemView::setCurrentIndex_data() |
| 1107 |
{ |
| 1108 |
QTest::addColumn<QByteArray>("viewType"); |
| 1109 |
QTest::addColumn<Qt::ItemFlags>("itemFlags"); |
| 1110 |
QTest::addColumn<bool>("result"); |
| 1111 |
|
| 1112 |
const QList<QByteArray> widgets { "QListView", "QTreeView", "QTableView", "QHeaderView" }; |
| 1113 |
for (const QByteArray &widget : widgets) { |
| 1114 |
QTest::newRow(widget + ": no flags") |
| 1115 |
<< widget << Qt::ItemFlags(Qt::NoItemFlags) << false; |
| 1116 |
QTest::newRow(widget + ": checkable") |
| 1117 |
<< widget << Qt::ItemFlags(Qt::ItemIsUserCheckable) << false; |
| 1118 |
QTest::newRow(widget + ": selectable") |
| 1119 |
<< widget << Qt::ItemFlags(Qt::ItemIsSelectable) << false; |
| 1120 |
QTest::newRow(widget + ": enabled") |
| 1121 |
<< widget << Qt::ItemFlags(Qt::ItemIsEnabled) << true; |
| 1122 |
QTest::newRow(widget + ": enabled|selectable") |
| 1123 |
<< widget << (Qt::ItemIsSelectable|Qt::ItemIsEnabled) << true; |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
void tst_QAbstractItemView::setCurrentIndex() |
| 1128 |
{ |
| 1129 |
QFETCH(QByteArray, viewType); |
| 1130 |
QFETCH(Qt::ItemFlags, itemFlags); |
| 1131 |
QFETCH(bool, result); |
| 1132 |
|
| 1133 |
QScopedPointer<QAbstractItemView> view(viewFromString(viewType)); |
| 1134 |
|
| 1135 |
centerOnScreen(view.data()); |
| 1136 |
moveCursorAway(view.data()); |
| 1137 |
view->show(); |
| 1138 |
QVERIFY(QTest::qWaitForWindowExposed(view.data())); |
| 1139 |
|
| 1140 |
QStandardItemModel *model = new QStandardItemModel(view.data()); |
| 1141 |
QStandardItem *item = new QStandardItem("first item"); |
| 1142 |
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
| 1143 |
model->appendRow(item); |
| 1144 |
|
| 1145 |
item = new QStandardItem("test item"); |
| 1146 |
item->setFlags(itemFlags); |
| 1147 |
model->appendRow(item); |
| 1148 |
|
| 1149 |
view->setModel(model); |
| 1150 |
|
| 1151 |
view->setCurrentIndex(model->index(0, 0)); |
| 1152 |
QCOMPARE(view->currentIndex(), model->index(0, 0)); |
| 1153 |
view->setCurrentIndex(model->index(1, 0)); |
| 1154 |
QVERIFY(view->currentIndex() == model->index(result ? 1 : 0, 0)); |
| 1155 |
} |
| 1156 |
|
| 1157 |
void tst_QAbstractItemView::checkIntersectedRect_data() |
| 1158 |
{ |
| 1159 |
auto createModel = [](int rowCount) -> QStandardItemModel* |
| 1160 |
{ |
| 1161 |
QStandardItemModel *model = new QStandardItemModel; |
| 1162 |
for (int i = 0; i < rowCount; ++i) { |
| 1163 |
const QList<QStandardItem *> sil({new QStandardItem(QString("Row %1 Item").arg(i)), |
| 1164 |
new QStandardItem(QString("2nd column"))}); |
| 1165 |
model->appendRow(sil); |
| 1166 |
} |
| 1167 |
return model; |
| 1168 |
}; |
| 1169 |
QTest::addColumn<QStandardItemModel *>("model"); |
| 1170 |
QTest::addColumn<QList<QModelIndex>>("changedIndexes"); |
| 1171 |
QTest::addColumn<bool>("isEmpty"); |
| 1172 |
{ |
| 1173 |
auto model = createModel(5); |
| 1174 |
QTest::newRow("multiple columns") |
| 1175 |
<< model << QList<QModelIndex>({ model->index(0, 0), model->index(0, 1) }) << false; |
| 1176 |
} |
| 1177 |
{ |
| 1178 |
auto model = createModel(5); |
| 1179 |
QTest::newRow("multiple rows") |
| 1180 |
<< model |
| 1181 |
<< QList<QModelIndex>( |
| 1182 |
{ model->index(0, 0), model->index(1, 0), model->index(2, 0) }) |
| 1183 |
<< false; |
| 1184 |
} |
| 1185 |
{ |
| 1186 |
auto model = createModel(5); |
| 1187 |
QTest::newRow("hidden rows") |
| 1188 |
<< model << QList<QModelIndex>({ model->index(3, 0), model->index(4, 0) }) << true; |
| 1189 |
} |
| 1190 |
{ |
| 1191 |
auto model = createModel(500); |
| 1192 |
QTest::newRow("rows outside viewport") |
| 1193 |
<< model << QList<QModelIndex>({ model->index(498, 0), model->index(499, 0) }) |
| 1194 |
<< true; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
void tst_QAbstractItemView::checkIntersectedRect() |
| 1199 |
{ |
| 1200 |
QFETCH(QStandardItemModel *, model); |
| 1201 |
QFETCH(const QList<QModelIndex>, changedIndexes); |
| 1202 |
QFETCH(bool, isEmpty); |
| 1203 |
|
| 1204 |
class TableView : public QTableView |
| 1205 |
{ |
| 1206 |
public: |
| 1207 |
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, |
| 1208 |
const QList<int> &roles = QList<int>()) override |
| 1209 |
{ |
| 1210 |
QTableView::dataChanged(topLeft, bottomRight, roles); |
| 1211 |
// we want to check the base class implementation here! |
| 1212 |
QAbstractItemViewPrivate *av = static_cast<QAbstractItemViewPrivate*>(qt_widget_private(this)); |
| 1213 |
m_intersectecRect = av->intersectedRect(av->viewport->rect(), topLeft, bottomRight); |
| 1214 |
} |
| 1215 |
mutable QRect m_intersectecRect; |
| 1216 |
}; |
| 1217 |
|
| 1218 |
TableView view; |
| 1219 |
model->setParent(&view); |
| 1220 |
view.setModel(model); |
| 1221 |
view.resize(400, 400); |
| 1222 |
view.show(); |
| 1223 |
view.setRowHidden(3, true); |
| 1224 |
view.setRowHidden(4, true); |
| 1225 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1226 |
|
| 1227 |
view.m_intersectecRect = QRect(); |
| 1228 |
emit view.model()->dataChanged(changedIndexes.first(), changedIndexes.last()); |
| 1229 |
if (isEmpty) { |
| 1230 |
QVERIFY(view.m_intersectecRect.isEmpty()); |
| 1231 |
} else { |
| 1232 |
const auto parent = changedIndexes.first().parent(); |
| 1233 |
const int rCount = view.model()->rowCount(parent); |
| 1234 |
const int cCount = view.model()->columnCount(parent); |
| 1235 |
for (int r = 0; r < rCount; ++r) { |
| 1236 |
for (int c = 0; c < cCount; ++c) { |
| 1237 |
const QModelIndex &idx = view.model()->index(r, c, parent); |
| 1238 |
const auto rect = view.visualRect(idx); |
| 1239 |
if (changedIndexes.contains(idx)) |
| 1240 |
QVERIFY(view.m_intersectecRect.contains(rect)); |
| 1241 |
else |
| 1242 |
QVERIFY(!view.m_intersectecRect.contains(rect)); |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
void tst_QAbstractItemView::task221955_selectedEditor() |
| 1249 |
{ |
| 1250 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1251 |
QSKIP("Window activation is not supported"); |
| 1252 |
|
| 1253 |
QTreeWidget tree; |
| 1254 |
tree.setColumnCount(2); |
| 1255 |
|
| 1256 |
tree.addTopLevelItem(new QTreeWidgetItem({"Foo", "1"})); |
| 1257 |
tree.addTopLevelItem(new QTreeWidgetItem({"Bar", "2"})); |
| 1258 |
tree.addTopLevelItem(new QTreeWidgetItem({"Baz", "3"})); |
| 1259 |
|
| 1260 |
QTreeWidgetItem *dummy = new QTreeWidgetItem(); |
| 1261 |
tree.addTopLevelItem(dummy); |
| 1262 |
QPushButton *button = new QPushButton("More..."); |
| 1263 |
tree.setItemWidget(dummy, 0, button); |
| 1264 |
button->setAutoFillBackground(true); // as recommended in doc |
| 1265 |
|
| 1266 |
centerOnScreen(&tree); |
| 1267 |
moveCursorAway(&tree); |
| 1268 |
tree.show(); |
| 1269 |
tree.setFocus(); |
| 1270 |
tree.setCurrentIndex(tree.model()->index(1,0)); |
| 1271 |
QVERIFY(QTest::qWaitForWindowActive(&tree)); |
| 1272 |
|
| 1273 |
QVERIFY(! tree.selectionModel()->selectedIndexes().contains(tree.model()->index(3,0))); |
| 1274 |
|
| 1275 |
//We set the focus to the button, the index need to be selected |
| 1276 |
button->setFocus(); |
| 1277 |
QTRY_VERIFY(tree.selectionModel()->selectedIndexes().contains(tree.model()->index(3,0))); |
| 1278 |
|
| 1279 |
tree.setCurrentIndex(tree.model()->index(1,0)); |
| 1280 |
QVERIFY(! tree.selectionModel()->selectedIndexes().contains(tree.model()->index(3,0))); |
| 1281 |
|
| 1282 |
//Same thing but with the flag NoSelection, nothing can be selected. |
| 1283 |
tree.setFocus(); |
| 1284 |
tree.setSelectionMode(QAbstractItemView::NoSelection); |
| 1285 |
tree.clearSelection(); |
| 1286 |
QVERIFY(tree.selectionModel()->selectedIndexes().isEmpty()); |
| 1287 |
button->setFocus(); |
| 1288 |
QTest::qWait(50); |
| 1289 |
QVERIFY(tree.selectionModel()->selectedIndexes().isEmpty()); |
| 1290 |
} |
| 1291 |
|
| 1292 |
void tst_QAbstractItemView::task250754_fontChange() |
| 1293 |
{ |
| 1294 |
QString app_css = qApp->styleSheet(); |
| 1295 |
qApp->setStyleSheet("/* */"); |
| 1296 |
|
| 1297 |
QWidget w; |
| 1298 |
QTreeView tree(&w); |
| 1299 |
QVBoxLayout *vLayout = new QVBoxLayout(&w); |
| 1300 |
vLayout->addWidget(&tree); |
| 1301 |
|
| 1302 |
QStandardItemModel *m = new QStandardItemModel(&w); |
| 1303 |
for (int i = 0; i < 20; ++i) { |
| 1304 |
QStandardItem *item = new QStandardItem(QStringLiteral("Item number ") + QString::number(i)); |
| 1305 |
for (int j = 0; j < 5; ++j) { |
| 1306 |
QStandardItem *child = new QStandardItem(QStringLiteral("Child Item number ") + QString::number(j)); |
| 1307 |
item->setChild(j, 0, child); |
| 1308 |
} |
| 1309 |
m->setItem(i, 0, item); |
| 1310 |
} |
| 1311 |
tree.setModel(m); |
| 1312 |
|
| 1313 |
tree.setHeaderHidden(true); // The header is (in certain styles) dpi dependent |
| 1314 |
w.resize(160, 350); // Minimum width for windows with frame on Windows 8 |
| 1315 |
centerOnScreen(&w); |
| 1316 |
moveCursorAway(&w); |
| 1317 |
w.showNormal(); |
| 1318 |
QVERIFY(QTest::qWaitForWindowExposed(&w)); |
| 1319 |
QFont font = tree.font(); |
| 1320 |
font.setPixelSize(10); |
| 1321 |
tree.setFont(font); |
| 1322 |
QTRY_VERIFY(!tree.verticalScrollBar()->isVisible()); |
| 1323 |
|
| 1324 |
font.setPixelSize(60); |
| 1325 |
tree.setFont(font); |
| 1326 |
//now with the huge items, the scrollbar must be visible |
| 1327 |
QTRY_VERIFY(tree.verticalScrollBar()->isVisible()); |
| 1328 |
|
| 1329 |
qApp->setStyleSheet(app_css); |
| 1330 |
} |
| 1331 |
|
| 1332 |
void tst_QAbstractItemView::task200665_itemEntered() |
| 1333 |
{ |
| 1334 |
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) |
| 1335 |
QSKIP("Wayland: This fails. Figure out why."); |
| 1336 |
{ |
| 1337 |
// skip if we can't move mouse |
| 1338 |
const QPoint cursorPos = QCursor::pos() + QPoint(10, 10); |
| 1339 |
QCursor::setPos(cursorPos); |
| 1340 |
if (!QTest::qWaitFor([cursorPos] { return QCursor::pos() == cursorPos; }, 500)) |
| 1341 |
QSKIP("Can't move mouse"); |
| 1342 |
} |
| 1343 |
//we test that view will emit entered |
| 1344 |
//when the scrollbar move but not the mouse itself |
| 1345 |
QStandardItemModel model(1000, 1); |
| 1346 |
QListView view; |
| 1347 |
view.setModel(&model); |
| 1348 |
centerOnScreen(&view); |
| 1349 |
moveCursorAway(&view); |
| 1350 |
view.show(); |
| 1351 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1352 |
QCursor::setPos(view.geometry().center()); |
| 1353 |
QTRY_COMPARE(QCursor::pos(), view.geometry().center()); |
| 1354 |
QSignalSpy spy(&view, &QAbstractItemView::entered); |
| 1355 |
view.verticalScrollBar()->setValue(view.verticalScrollBar()->maximum()); |
| 1356 |
|
| 1357 |
QTRY_COMPARE(spy.size(), 1); |
| 1358 |
} |
| 1359 |
|
| 1360 |
void tst_QAbstractItemView::task257481_emptyEditor() |
| 1361 |
{ |
| 1362 |
QIcon icon = QApplication::style()->standardIcon(QStyle::SP_ComputerIcon); |
| 1363 |
|
| 1364 |
QStandardItemModel model; |
| 1365 |
|
| 1366 |
model.appendRow(new QStandardItem(icon, QString())); |
| 1367 |
model.appendRow(new QStandardItem(icon, "Editor works")); |
| 1368 |
model.appendRow(new QStandardItem(QString())); |
| 1369 |
|
| 1370 |
QTreeView treeView; |
| 1371 |
treeView.setRootIsDecorated(false); |
| 1372 |
treeView.setModel(&model); |
| 1373 |
centerOnScreen(&treeView); |
| 1374 |
moveCursorAway(&treeView); |
| 1375 |
treeView.show(); |
| 1376 |
QVERIFY(QTest::qWaitForWindowExposed(&treeView)); |
| 1377 |
|
| 1378 |
treeView.edit(model.index(0, 0)); |
| 1379 |
QList<QLineEdit *> lineEditors = treeView.viewport()->findChildren<QLineEdit *>(); |
| 1380 |
QCOMPARE(lineEditors.size(), 1); |
| 1381 |
QVERIFY(!lineEditors.constFirst()->size().isEmpty()); |
| 1382 |
|
| 1383 |
treeView.edit(model.index(1, 0)); |
| 1384 |
lineEditors = treeView.viewport()->findChildren<QLineEdit *>(); |
| 1385 |
QCOMPARE(lineEditors.size(), 1); |
| 1386 |
QVERIFY(!lineEditors.constFirst()->size().isEmpty()); |
| 1387 |
|
| 1388 |
treeView.edit(model.index(2, 0)); |
| 1389 |
lineEditors = treeView.viewport()->findChildren<QLineEdit *>(); |
| 1390 |
QCOMPARE(lineEditors.size(), 1); |
| 1391 |
QVERIFY(!lineEditors.constFirst()->size().isEmpty()); |
| 1392 |
} |
| 1393 |
|
| 1394 |
void tst_QAbstractItemView::shiftArrowSelectionAfterScrolling() |
| 1395 |
{ |
| 1396 |
QStandardItemModel model; |
| 1397 |
for (int i = 0; i < 10; ++i) |
| 1398 |
model.setItem(i, 0, new QStandardItem(QString::number(i))); |
| 1399 |
|
| 1400 |
QListView view; |
| 1401 |
view.setFixedSize(160, 250); // Minimum width for windows with frame on Windows 8 |
| 1402 |
view.setFlow(QListView::LeftToRight); |
| 1403 |
view.setGridSize(QSize(100, 100)); |
| 1404 |
view.setSelectionMode(QListView::ExtendedSelection); |
| 1405 |
view.setViewMode(QListView::IconMode); |
| 1406 |
view.setModel(&model); |
| 1407 |
centerOnScreen(&view); |
| 1408 |
moveCursorAway(&view); |
| 1409 |
view.show(); |
| 1410 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1411 |
|
| 1412 |
QModelIndex index0 = model.index(0, 0); |
| 1413 |
QModelIndex index1 = model.index(1, 0); |
| 1414 |
QModelIndex index9 = model.index(9, 0); |
| 1415 |
|
| 1416 |
view.selectionModel()->setCurrentIndex(index0, QItemSelectionModel::NoUpdate); |
| 1417 |
QCOMPARE(view.currentIndex(), index0); |
| 1418 |
|
| 1419 |
view.scrollTo(index9); |
| 1420 |
QTest::keyClick(&view, Qt::Key_Down, Qt::ShiftModifier); |
| 1421 |
|
| 1422 |
QCOMPARE(view.currentIndex(), index1); |
| 1423 |
QModelIndexList selected = view.selectionModel()->selectedIndexes(); |
| 1424 |
QCOMPARE(selected.size(), 2); |
| 1425 |
QVERIFY(selected.contains(index0)); |
| 1426 |
QVERIFY(selected.contains(index1)); |
| 1427 |
} |
| 1428 |
|
| 1429 |
void tst_QAbstractItemView::shiftSelectionAfterRubberbandSelection() |
| 1430 |
{ |
| 1431 |
QStandardItemModel model; |
| 1432 |
for (int i = 0; i < 3; ++i) |
| 1433 |
model.setItem(i, 0, new QStandardItem(QString::number(i))); |
| 1434 |
|
| 1435 |
QListView view; |
| 1436 |
view.setFixedSize(160, 450); // Minimum width for windows with frame on Windows 8 |
| 1437 |
view.setFlow(QListView::LeftToRight); |
| 1438 |
view.setGridSize(QSize(100, 100)); |
| 1439 |
view.setSelectionMode(QListView::ExtendedSelection); |
| 1440 |
view.setViewMode(QListView::IconMode); |
| 1441 |
view.setModel(&model); |
| 1442 |
centerOnScreen(&view); |
| 1443 |
moveCursorAway(&view); |
| 1444 |
view.show(); |
| 1445 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1446 |
|
| 1447 |
QModelIndex index0 = model.index(0, 0); |
| 1448 |
QModelIndex index1 = model.index(1, 0); |
| 1449 |
QModelIndex index2 = model.index(2, 0); |
| 1450 |
|
| 1451 |
view.setCurrentIndex(index0); |
| 1452 |
QCOMPARE(view.currentIndex(), index0); |
| 1453 |
|
| 1454 |
// Determine the points where the rubberband selection starts and ends |
| 1455 |
QPoint pressPos = view.visualRect(index1).bottomRight() + QPoint(1, 1); |
| 1456 |
QPoint releasePos = view.visualRect(index1).center(); |
| 1457 |
QVERIFY(!view.indexAt(pressPos).isValid()); |
| 1458 |
QCOMPARE(view.indexAt(releasePos), index1); |
| 1459 |
|
| 1460 |
// Select item 1 using a rubberband selection |
| 1461 |
// The mouse move event has to be created manually because the QTest framework does not |
| 1462 |
// contain a function for mouse moves with buttons pressed |
| 1463 |
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, pressPos); |
| 1464 |
QMouseEvent moveEvent(QEvent::MouseMove, releasePos, view.viewport()->mapToGlobal(releasePos), |
| 1465 |
Qt::NoButton, Qt::LeftButton, Qt::NoModifier); |
| 1466 |
bool moveEventReceived = qApp->notify(view.viewport(), &moveEvent); |
| 1467 |
QVERIFY(moveEventReceived); |
| 1468 |
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, releasePos); |
| 1469 |
QCOMPARE(view.currentIndex(), index1); |
| 1470 |
|
| 1471 |
// Shift-click item 2 |
| 1472 |
QPoint item2Pos = view.visualRect(index2).center(); |
| 1473 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, item2Pos); |
| 1474 |
QCOMPARE(view.currentIndex(), index2); |
| 1475 |
|
| 1476 |
// Verify that the selection worked OK |
| 1477 |
QModelIndexList selected = view.selectionModel()->selectedIndexes(); |
| 1478 |
QCOMPARE(selected.size(), 2); |
| 1479 |
QVERIFY(selected.contains(index1)); |
| 1480 |
QVERIFY(selected.contains(index2)); |
| 1481 |
|
| 1482 |
// Select item 0 to revert the selection |
| 1483 |
view.setCurrentIndex(index0); |
| 1484 |
QCOMPARE(view.currentIndex(), index0); |
| 1485 |
|
| 1486 |
// Repeat the same steps as above, but with a Shift-Arrow selection |
| 1487 |
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, pressPos); |
| 1488 |
QMouseEvent moveEvent2(QEvent::MouseMove, releasePos, view.viewport()->mapToGlobal(releasePos), |
| 1489 |
Qt::NoButton, Qt::LeftButton, Qt::NoModifier); |
| 1490 |
moveEventReceived = qApp->notify(view.viewport(), &moveEvent2); |
| 1491 |
QVERIFY(moveEventReceived); |
| 1492 |
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, releasePos); |
| 1493 |
QCOMPARE(view.currentIndex(), index1); |
| 1494 |
|
| 1495 |
// Press Shift-Down |
| 1496 |
QTest::keyClick(&view, Qt::Key_Down, Qt::ShiftModifier); |
| 1497 |
QCOMPARE(view.currentIndex(), index2); |
| 1498 |
|
| 1499 |
// Verify that the selection worked OK |
| 1500 |
selected = view.selectionModel()->selectedIndexes(); |
| 1501 |
QCOMPARE(selected.size(), 2); |
| 1502 |
QVERIFY(selected.contains(index1)); |
| 1503 |
QVERIFY(selected.contains(index2)); |
| 1504 |
} |
| 1505 |
|
| 1506 |
void tst_QAbstractItemView::ctrlRubberbandSelection() |
| 1507 |
{ |
| 1508 |
QStandardItemModel model; |
| 1509 |
for (int i = 0; i < 3; ++i) |
| 1510 |
model.setItem(i, 0, new QStandardItem(QString::number(i))); |
| 1511 |
|
| 1512 |
QListView view; |
| 1513 |
view.setFixedSize(160, 450); // Minimum width for windows with frame on Windows 8 |
| 1514 |
view.setFlow(QListView::LeftToRight); |
| 1515 |
view.setGridSize(QSize(100, 100)); |
| 1516 |
view.setSelectionMode(QListView::ExtendedSelection); |
| 1517 |
view.setViewMode(QListView::IconMode); |
| 1518 |
view.setModel(&model); |
| 1519 |
centerOnScreen(&view); |
| 1520 |
moveCursorAway(&view); |
| 1521 |
view.show(); |
| 1522 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1523 |
|
| 1524 |
QModelIndex index1 = model.index(1, 0); |
| 1525 |
QModelIndex index2 = model.index(2, 0); |
| 1526 |
|
| 1527 |
// Select item 1 |
| 1528 |
view.setCurrentIndex(index1); |
| 1529 |
QModelIndexList selected = view.selectionModel()->selectedIndexes(); |
| 1530 |
QCOMPARE(selected.size(), 1); |
| 1531 |
QVERIFY(selected.contains(index1)); |
| 1532 |
|
| 1533 |
// Now press control and draw a rubberband around items 1 and 2. |
| 1534 |
// The mouse move event has to be created manually because the QTest framework does not |
| 1535 |
// contain a function for mouse moves with buttons pressed. |
| 1536 |
QPoint pressPos = view.visualRect(index1).topLeft() - QPoint(1, 1); |
| 1537 |
QPoint releasePos = view.visualRect(index2).bottomRight() + QPoint(1, 1); |
| 1538 |
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::ControlModifier, pressPos); |
| 1539 |
QMouseEvent moveEvent(QEvent::MouseMove, releasePos, view.viewport()->mapToGlobal(releasePos), |
| 1540 |
Qt::NoButton, Qt::LeftButton, Qt::ControlModifier); |
| 1541 |
bool moveEventReceived = qApp->notify(view.viewport(), &moveEvent); |
| 1542 |
QVERIFY(moveEventReceived); |
| 1543 |
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::ControlModifier, releasePos); |
| 1544 |
|
| 1545 |
// Verify that item 2 is selected now |
| 1546 |
selected = view.selectionModel()->selectedIndexes(); |
| 1547 |
QCOMPARE(selected.size(), 1); |
| 1548 |
QVERIFY(selected.contains(index2)); |
| 1549 |
} |
| 1550 |
|
| 1551 |
void tst_QAbstractItemView::QTBUG6407_extendedSelection() |
| 1552 |
{ |
| 1553 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1554 |
QSKIP("Window activation is not supported"); |
| 1555 |
|
| 1556 |
QListWidget view; |
| 1557 |
view.setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 1558 |
for (int i = 0; i < 50; ++i) |
| 1559 |
view.addItem(QString::number(i)); |
| 1560 |
|
| 1561 |
QFont font = view.font(); |
| 1562 |
font.setPixelSize(10); |
| 1563 |
view.setFont(font); |
| 1564 |
view.resize(200,240); |
| 1565 |
centerOnScreen(&view); |
| 1566 |
moveCursorAway(&view); |
| 1567 |
|
| 1568 |
view.show(); |
| 1569 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 1570 |
QCOMPARE(&view, QApplication::activeWindow()); |
| 1571 |
|
| 1572 |
view.verticalScrollBar()->setValue(view.verticalScrollBar()->maximum()); |
| 1573 |
|
| 1574 |
QModelIndex index49 = view.model()->index(49,0); |
| 1575 |
QPoint p = view.visualRect(index49).center(); |
| 1576 |
QVERIFY(view.viewport()->rect().contains(p)); |
| 1577 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, {}, p); |
| 1578 |
QCOMPARE(view.currentIndex(), index49); |
| 1579 |
QCOMPARE(view.selectedItems().size(), 1); |
| 1580 |
|
| 1581 |
QModelIndex index47 = view.model()->index(47,0); |
| 1582 |
p = view.visualRect(index47).center(); |
| 1583 |
QVERIFY(view.viewport()->rect().contains(p)); |
| 1584 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, p); |
| 1585 |
QCOMPARE(view.currentIndex(), index47); |
| 1586 |
QCOMPARE(view.selectedItems().size(), 3); //49, 48, 47; |
| 1587 |
|
| 1588 |
QModelIndex index44 = view.model()->index(44,0); |
| 1589 |
p = view.visualRect(index44).center(); |
| 1590 |
QVERIFY(view.viewport()->rect().contains(p)); |
| 1591 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, p); |
| 1592 |
QCOMPARE(view.currentIndex(), index44); |
| 1593 |
QCOMPARE(view.selectedItems().size(), 6); //49 .. 44; |
| 1594 |
|
| 1595 |
} |
| 1596 |
|
| 1597 |
void tst_QAbstractItemView::QTBUG6753_selectOnSelection() |
| 1598 |
{ |
| 1599 |
QTableWidget table(5, 5); |
| 1600 |
for (int i = 0; i < table.rowCount(); ++i) { |
| 1601 |
for (int j = 0; j < table.columnCount(); ++j) |
| 1602 |
table.setItem(i, j, new QTableWidgetItem("choo-be-doo-wah")); |
| 1603 |
} |
| 1604 |
|
| 1605 |
centerOnScreen(&table); |
| 1606 |
moveCursorAway(&table); |
| 1607 |
table.show(); |
| 1608 |
QVERIFY(QTest::qWaitForWindowExposed(&table)); |
| 1609 |
|
| 1610 |
table.setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 1611 |
table.selectAll(); |
| 1612 |
QVERIFY(QTest::qWaitForWindowExposed(&table)); |
| 1613 |
QModelIndex item = table.model()->index(1,1); |
| 1614 |
QRect itemRect = table.visualRect(item); |
| 1615 |
QTest::mouseMove(table.viewport(), itemRect.center()); |
| 1616 |
QTest::mouseClick(table.viewport(), Qt::LeftButton, Qt::NoModifier, itemRect.center()); |
| 1617 |
|
| 1618 |
QCOMPARE(table.selectedItems().size(), 1); |
| 1619 |
QCOMPARE(table.selectedItems().first(), table.item(item.row(), item.column())); |
| 1620 |
} |
| 1621 |
|
| 1622 |
void tst_QAbstractItemView::testDelegateDestroyEditor() |
| 1623 |
{ |
| 1624 |
QTableWidget table(5, 5); |
| 1625 |
MyAbstractItemDelegate delegate; |
| 1626 |
table.setItemDelegate(&delegate); |
| 1627 |
table.edit(table.model()->index(1, 1)); |
| 1628 |
QAbstractItemView *tv = &table; |
| 1629 |
QCOMPARE(delegate.virtualDtorCallCount, 0); |
| 1630 |
tv->closeEditor(delegate.openedEditor, QAbstractItemDelegate::NoHint); |
| 1631 |
QCOMPARE(delegate.virtualDtorCallCount, 1); |
| 1632 |
} |
| 1633 |
|
| 1634 |
void tst_QAbstractItemView::testDelegateDestroyEditorChild() |
| 1635 |
{ |
| 1636 |
std::vector<std::unique_ptr<QTreeWidgetItem>> reaper; |
| 1637 |
QTreeWidget tree; |
| 1638 |
MyAbstractItemDelegate delegate; |
| 1639 |
tree.setItemDelegate(&delegate); |
| 1640 |
QTreeWidgetItem *topLevel = new QTreeWidgetItem; |
| 1641 |
QTreeWidgetItem *levelOne1 = new QTreeWidgetItem(topLevel); |
| 1642 |
QTreeWidgetItem *levelTwo1 = new QTreeWidgetItem(levelOne1); |
| 1643 |
QTreeWidgetItem *levelOne2 = new QTreeWidgetItem(topLevel); |
| 1644 |
QTreeWidgetItem *levelTwo2 = new QTreeWidgetItem(levelOne2); |
| 1645 |
tree.insertTopLevelItem(0, topLevel); |
| 1646 |
tree.openPersistentEditor(levelOne1); |
| 1647 |
tree.openPersistentEditor(levelTwo1); |
| 1648 |
tree.openPersistentEditor(levelOne2); |
| 1649 |
tree.openPersistentEditor(levelTwo2); |
| 1650 |
QCOMPARE(delegate.virtualCtorCallCount, 4); |
| 1651 |
levelOne1->removeChild(levelTwo1); |
| 1652 |
reaper.emplace_back(levelTwo1); |
| 1653 |
QCOMPARE(delegate.virtualDtorCallCount, 1); |
| 1654 |
topLevel->removeChild(levelOne2); |
| 1655 |
reaper.emplace_back(levelOne2); |
| 1656 |
QCOMPARE(delegate.virtualDtorCallCount, 3); |
| 1657 |
} |
| 1658 |
|
| 1659 |
void tst_QAbstractItemView::testClickedSignal() |
| 1660 |
{ |
| 1661 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1662 |
QSKIP("Window activation is not supported"); |
| 1663 |
|
| 1664 |
QTableWidget view(5, 5); |
| 1665 |
|
| 1666 |
centerOnScreen(&view); |
| 1667 |
moveCursorAway(&view); |
| 1668 |
view.showNormal(); |
| 1669 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 1670 |
QCOMPARE(&view, QApplication::activeWindow()); |
| 1671 |
|
| 1672 |
QModelIndex index49 = view.model()->index(49,0); |
| 1673 |
QPoint p = view.visualRect(index49).center(); |
| 1674 |
QVERIFY(view.viewport()->rect().contains(p)); |
| 1675 |
|
| 1676 |
QSignalSpy clickedSpy(&view, &QTableWidget::clicked); |
| 1677 |
|
| 1678 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, {}, p); |
| 1679 |
QCOMPARE(clickedSpy.size(), 1); |
| 1680 |
|
| 1681 |
QTest::mouseClick(view.viewport(), Qt::RightButton, {}, p); |
| 1682 |
// We expect that right-clicks do not cause the clicked() signal to |
| 1683 |
// be emitted. |
| 1684 |
QCOMPARE(clickedSpy.size(), 1); |
| 1685 |
|
| 1686 |
} |
| 1687 |
|
| 1688 |
class StateChangeDelegate : public QStyledItemDelegate |
| 1689 |
{ |
| 1690 |
Q_OBJECT |
| 1691 |
public: |
| 1692 |
using QStyledItemDelegate::QStyledItemDelegate; |
| 1693 |
void setEditorData(QWidget *editor, const QModelIndex &index) const override |
| 1694 |
{ |
| 1695 |
Q_UNUSED(index); |
| 1696 |
static bool w = true; |
| 1697 |
editor->setEnabled(w); |
| 1698 |
w = !w; |
| 1699 |
} |
| 1700 |
}; |
| 1701 |
|
| 1702 |
void tst_QAbstractItemView::testChangeEditorState() |
| 1703 |
{ |
| 1704 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1705 |
QSKIP("Window activation is not supported"); |
| 1706 |
|
| 1707 |
// Test for QTBUG-25370 |
| 1708 |
TestModel model; |
| 1709 |
model.setItem(0, 0, new QStandardItem("a")); |
| 1710 |
model.setItem(0, 1, new QStandardItem("b")); |
| 1711 |
|
| 1712 |
QTableView view; |
| 1713 |
view.setEditTriggers(QAbstractItemView::CurrentChanged); |
| 1714 |
view.setItemDelegate(new StateChangeDelegate(&view)); |
| 1715 |
view.setModel(&model); |
| 1716 |
centerOnScreen(&view); |
| 1717 |
moveCursorAway(&view); |
| 1718 |
view.show(); |
| 1719 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 1720 |
QCOMPARE(&view, QApplication::activeWindow()); |
| 1721 |
|
| 1722 |
model.emitDataChanged(); |
| 1723 |
// No segfault - the test passes. |
| 1724 |
} |
| 1725 |
|
| 1726 |
void tst_QAbstractItemView::deselectInSingleSelection() |
| 1727 |
{ |
| 1728 |
QTableView view; |
| 1729 |
QStandardItemModel s; |
| 1730 |
s.setRowCount(10); |
| 1731 |
s.setColumnCount(10); |
| 1732 |
view.setModel(&s); |
| 1733 |
centerOnScreen(&view); |
| 1734 |
moveCursorAway(&view); |
| 1735 |
view.show(); |
| 1736 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1737 |
view.setSelectionMode(QAbstractItemView::SingleSelection); |
| 1738 |
view.setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 1739 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1740 |
// mouse |
| 1741 |
QModelIndex index22 = s.index(2, 2); |
| 1742 |
QRect rect22 = view.visualRect(index22); |
| 1743 |
QPoint clickpos = rect22.center(); |
| 1744 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, clickpos); |
| 1745 |
QCOMPARE(view.currentIndex(), index22); |
| 1746 |
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 1); |
| 1747 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ControlModifier, clickpos); |
| 1748 |
QCOMPARE(view.currentIndex(), index22); |
| 1749 |
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 0); |
| 1750 |
|
| 1751 |
// second click with modifier however does select |
| 1752 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ControlModifier, clickpos); |
| 1753 |
QCOMPARE(view.currentIndex(), index22); |
| 1754 |
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 1); |
| 1755 |
|
| 1756 |
// keyboard |
| 1757 |
QTest::keyClick(&view, Qt::Key_Space, Qt::NoModifier); |
| 1758 |
QCOMPARE(view.currentIndex(), index22); |
| 1759 |
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 1); |
| 1760 |
QTest::keyClick(&view, Qt::Key_Space, Qt::ControlModifier); |
| 1761 |
QCOMPARE(view.currentIndex(), index22); |
| 1762 |
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 0); |
| 1763 |
|
| 1764 |
// second keypress with modifier however does select |
| 1765 |
QTest::keyClick(&view, Qt::Key_Space, Qt::ControlModifier); |
| 1766 |
QCOMPARE(view.currentIndex(), index22); |
| 1767 |
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 1); |
| 1768 |
} |
| 1769 |
|
| 1770 |
void tst_QAbstractItemView::testNoActivateOnDisabledItem() |
| 1771 |
{ |
| 1772 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1773 |
QSKIP("Window activation is not supported"); |
| 1774 |
|
| 1775 |
QTreeView treeView; |
| 1776 |
QStandardItemModel model(1, 1); |
| 1777 |
QStandardItem *item = new QStandardItem("item"); |
| 1778 |
model.setItem(0, 0, item); |
| 1779 |
item->setFlags(Qt::NoItemFlags); |
| 1780 |
treeView.setModel(&model); |
| 1781 |
centerOnScreen(&treeView); |
| 1782 |
moveCursorAway(&treeView); |
| 1783 |
treeView.show(); |
| 1784 |
|
| 1785 |
QVERIFY(QTest::qWaitForWindowActive(&treeView)); |
| 1786 |
|
| 1787 |
QSignalSpy activatedSpy(&treeView, &QAbstractItemView::activated); |
| 1788 |
|
| 1789 |
// Ensure clicking on a disabled item doesn't emit itemActivated. |
| 1790 |
QModelIndex itemIndex = treeView.model()->index(0, 0); |
| 1791 |
QPoint clickPos = treeView.visualRect(itemIndex).center(); |
| 1792 |
QTest::mouseClick(treeView.viewport(), Qt::LeftButton, {}, clickPos); |
| 1793 |
|
| 1794 |
QCOMPARE(activatedSpy.size(), 0); |
| 1795 |
} |
| 1796 |
|
| 1797 |
void tst_QAbstractItemView::testFocusPolicy_data() |
| 1798 |
{ |
| 1799 |
QTest::addColumn<QAbstractItemDelegate*>("delegate"); |
| 1800 |
|
| 1801 |
QAbstractItemDelegate *styledItemDelegate = new QStyledItemDelegate(this); |
| 1802 |
QAbstractItemDelegate *itemDelegate = new QItemDelegate(this); |
| 1803 |
|
| 1804 |
QTest::newRow("QStyledItemDelegate") << styledItemDelegate; |
| 1805 |
QTest::newRow("QItemDelegate") << itemDelegate; |
| 1806 |
} |
| 1807 |
|
| 1808 |
void tst_QAbstractItemView::testFocusPolicy() |
| 1809 |
{ |
| 1810 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 1811 |
QSKIP("Window activation is not supported"); |
| 1812 |
|
| 1813 |
QFETCH(QAbstractItemDelegate*, delegate); |
| 1814 |
|
| 1815 |
QWidget window; |
| 1816 |
QTableView *table = new QTableView(&window); |
| 1817 |
table->setItemDelegate(delegate); |
| 1818 |
QVBoxLayout *layout = new QVBoxLayout(&window); |
| 1819 |
layout->addWidget(table); |
| 1820 |
|
| 1821 |
QStandardItemModel model; |
| 1822 |
model.setRowCount(10); |
| 1823 |
model.setColumnCount(10); |
| 1824 |
table->setModel(&model); |
| 1825 |
table->setCurrentIndex(model.index(1, 1)); |
| 1826 |
|
| 1827 |
centerOnScreen(&window); |
| 1828 |
moveCursorAway(&window); |
| 1829 |
|
| 1830 |
window.show(); |
| 1831 |
QVERIFY(QTest::qWaitForWindowActive(&window)); |
| 1832 |
|
| 1833 |
// itemview accepts focus => editor is closed => return focus to the itemview |
| 1834 |
QPoint clickpos = table->visualRect(model.index(1, 1)).center(); |
| 1835 |
QTest::mouseDClick(table->viewport(), Qt::LeftButton, Qt::NoModifier, clickpos); |
| 1836 |
QWidget *editor = QApplication::focusWidget(); |
| 1837 |
QVERIFY(editor); |
| 1838 |
QTest::keyClick(editor, Qt::Key_Escape, Qt::NoModifier); |
| 1839 |
QCOMPARE(QApplication::focusWidget(), table); |
| 1840 |
|
| 1841 |
// itemview doesn't accept focus => editor is closed => clear the focus |
| 1842 |
table->setFocusPolicy(Qt::NoFocus); |
| 1843 |
QTest::mouseDClick(table->viewport(), Qt::LeftButton, Qt::NoModifier, clickpos); |
| 1844 |
editor = QApplication::focusWidget(); |
| 1845 |
QVERIFY(editor); |
| 1846 |
QTest::keyClick(editor, Qt::Key_Escape, Qt::NoModifier); |
| 1847 |
QVERIFY(!QApplication::focusWidget()); |
| 1848 |
} |
| 1849 |
|
| 1850 |
void tst_QAbstractItemView::QTBUG31411_noSelection() |
| 1851 |
{ |
| 1852 |
QWidget window; |
| 1853 |
QTableView *table = new QTableView(&window); |
| 1854 |
table->setSelectionMode(QAbstractItemView::NoSelection); |
| 1855 |
QVBoxLayout *layout = new QVBoxLayout(&window); |
| 1856 |
layout->addWidget(table); |
| 1857 |
|
| 1858 |
QStandardItemModel model; |
| 1859 |
model.setRowCount(10); |
| 1860 |
model.setColumnCount(10); |
| 1861 |
table->setModel(&model); |
| 1862 |
table->setCurrentIndex(model.index(1, 1)); |
| 1863 |
|
| 1864 |
centerOnScreen(&window); |
| 1865 |
moveCursorAway(&window); |
| 1866 |
|
| 1867 |
window.show(); |
| 1868 |
QVERIFY(QTest::qWaitForWindowFocused(&window)); |
| 1869 |
|
| 1870 |
qRegisterMetaType<QItemSelection>(); |
| 1871 |
QSignalSpy selectionChangeSpy(table->selectionModel(), &QItemSelectionModel::selectionChanged); |
| 1872 |
QVERIFY(selectionChangeSpy.isValid()); |
| 1873 |
|
| 1874 |
QPoint clickpos = table->visualRect(model.index(1, 1)).center(); |
| 1875 |
QTest::mouseClick(table->viewport(), Qt::LeftButton, Qt::NoModifier, clickpos); |
| 1876 |
QTest::mouseDClick(table->viewport(), Qt::LeftButton, Qt::NoModifier, clickpos); |
| 1877 |
|
| 1878 |
QPointer<QWidget> editor1 = QApplication::focusWidget(); |
| 1879 |
QVERIFY(editor1); |
| 1880 |
QTest::keyClick(editor1, Qt::Key_Tab, Qt::NoModifier); |
| 1881 |
|
| 1882 |
QPointer<QWidget> editor2 = QApplication::focusWidget(); |
| 1883 |
QVERIFY(editor2); |
| 1884 |
QTest::keyClick(editor2, Qt::Key_Escape, Qt::NoModifier); |
| 1885 |
|
| 1886 |
QCOMPARE(selectionChangeSpy.size(), 0); |
| 1887 |
} |
| 1888 |
|
| 1889 |
void tst_QAbstractItemView::QTBUG39324_settingSameInstanceOfIndexWidget() |
| 1890 |
{ |
| 1891 |
QStringListModel model({ "FOO", "bar" }); |
| 1892 |
|
| 1893 |
QScopedPointer<QTableView> table(new QTableView()); |
| 1894 |
table->setModel(&model); |
| 1895 |
|
| 1896 |
QModelIndex index = model.index(0,0); |
| 1897 |
QLineEdit *lineEdit = new QLineEdit(); |
| 1898 |
table->setIndexWidget(index, lineEdit); |
| 1899 |
table->setIndexWidget(index, lineEdit); |
| 1900 |
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); |
| 1901 |
table->show(); |
| 1902 |
} |
| 1903 |
|
| 1904 |
void tst_QAbstractItemView::shiftSelectionAfterChangingModelContents() |
| 1905 |
{ |
| 1906 |
QStringListModel model({ "A", "B", "C", "D", "E", "F" }); |
| 1907 |
QSortFilterProxyModel proxyModel; |
| 1908 |
proxyModel.setSourceModel(&model); |
| 1909 |
proxyModel.sort(0, Qt::AscendingOrder); |
| 1910 |
proxyModel.setDynamicSortFilter(true); |
| 1911 |
|
| 1912 |
QPersistentModelIndex indexA(proxyModel.index(0, 0)); |
| 1913 |
QPersistentModelIndex indexB(proxyModel.index(1, 0)); |
| 1914 |
QPersistentModelIndex indexC(proxyModel.index(2, 0)); |
| 1915 |
QPersistentModelIndex indexD(proxyModel.index(3, 0)); |
| 1916 |
QPersistentModelIndex indexE(proxyModel.index(4, 0)); |
| 1917 |
QPersistentModelIndex indexF(proxyModel.index(5, 0)); |
| 1918 |
|
| 1919 |
QListView view; |
| 1920 |
view.setModel(&proxyModel); |
| 1921 |
view.setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 1922 |
view.show(); |
| 1923 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 1924 |
|
| 1925 |
// Click "C" |
| 1926 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(indexC).center()); |
| 1927 |
QModelIndexList selected = view.selectionModel()->selectedIndexes(); |
| 1928 |
QCOMPARE(selected.size(), 1); |
| 1929 |
QVERIFY(selected.contains(indexC)); |
| 1930 |
|
| 1931 |
// Insert new item "B1" |
| 1932 |
model.insertRow(0); |
| 1933 |
model.setData(model.index(0, 0), "B1"); |
| 1934 |
|
| 1935 |
// Shift-click "D" -> we expect that "C" and "D" are selected |
| 1936 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, view.visualRect(indexD).center()); |
| 1937 |
selected = view.selectionModel()->selectedIndexes(); |
| 1938 |
QCOMPARE(selected.size(), 2); |
| 1939 |
QVERIFY(selected.contains(indexC)); |
| 1940 |
QVERIFY(selected.contains(indexD)); |
| 1941 |
|
| 1942 |
// Click "D" |
| 1943 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(indexD).center()); |
| 1944 |
selected = view.selectionModel()->selectedIndexes(); |
| 1945 |
QCOMPARE(selected.size(), 1); |
| 1946 |
QVERIFY(selected.contains(indexD)); |
| 1947 |
|
| 1948 |
// Remove items "B" and "C" |
| 1949 |
model.removeRows(proxyModel.mapToSource(indexB).row(), 1); |
| 1950 |
model.removeRows(proxyModel.mapToSource(indexC).row(), 1); |
| 1951 |
QVERIFY(!indexB.isValid()); |
| 1952 |
QVERIFY(!indexC.isValid()); |
| 1953 |
|
| 1954 |
// Shift-click "F" -> we expect that "D", "E", and "F" are selected |
| 1955 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, view.visualRect(indexF).center()); |
| 1956 |
selected = view.selectionModel()->selectedIndexes(); |
| 1957 |
QCOMPARE(selected.size(), 3); |
| 1958 |
QVERIFY(selected.contains(indexD)); |
| 1959 |
QVERIFY(selected.contains(indexE)); |
| 1960 |
QVERIFY(selected.contains(indexF)); |
| 1961 |
|
| 1962 |
// Move to "A" by pressing "Up" repeatedly |
| 1963 |
while (view.currentIndex() != indexA) |
| 1964 |
QTest::keyClick(&view, Qt::Key_Up); |
| 1965 |
selected = view.selectionModel()->selectedIndexes(); |
| 1966 |
QCOMPARE(selected.size(), 1); |
| 1967 |
QVERIFY(selected.contains(indexA)); |
| 1968 |
|
| 1969 |
// Change the sort order |
| 1970 |
proxyModel.sort(0, Qt::DescendingOrder); |
| 1971 |
|
| 1972 |
// Shift-click "F" -> All items should be selected |
| 1973 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, view.visualRect(indexF).center()); |
| 1974 |
selected = view.selectionModel()->selectedIndexes(); |
| 1975 |
QCOMPARE(selected.size(), model.rowCount()); |
| 1976 |
|
| 1977 |
// Restore the old sort order |
| 1978 |
proxyModel.sort(0, Qt::AscendingOrder); |
| 1979 |
|
| 1980 |
// Click "D" |
| 1981 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(indexD).center()); |
| 1982 |
selected = view.selectionModel()->selectedIndexes(); |
| 1983 |
QCOMPARE(selected.size(), 1); |
| 1984 |
QVERIFY(selected.contains(indexD)); |
| 1985 |
|
| 1986 |
// Insert new item "B2" |
| 1987 |
model.insertRow(0); |
| 1988 |
model.setData(model.index(0, 0), "B2"); |
| 1989 |
|
| 1990 |
// Press Shift+Down -> "D" and "E" should be selected. |
| 1991 |
QTest::keyClick(&view, Qt::Key_Down, Qt::ShiftModifier); |
| 1992 |
selected = view.selectionModel()->selectedIndexes(); |
| 1993 |
QCOMPARE(selected.size(), 2); |
| 1994 |
QVERIFY(selected.contains(indexD)); |
| 1995 |
QVERIFY(selected.contains(indexE)); |
| 1996 |
|
| 1997 |
// Click "A" to reset the current selection starting point; |
| 1998 |
//then select "D" via QAbstractItemView::setCurrentIndex(const QModelIndex&). |
| 1999 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(indexA).center()); |
| 2000 |
view.setCurrentIndex(indexD); |
| 2001 |
selected = view.selectionModel()->selectedIndexes(); |
| 2002 |
QCOMPARE(selected.size(), 1); |
| 2003 |
QVERIFY(selected.contains(indexD)); |
| 2004 |
|
| 2005 |
// Insert new item "B3" |
| 2006 |
model.insertRow(0); |
| 2007 |
model.setData(model.index(0, 0), "B3"); |
| 2008 |
|
| 2009 |
// Press Shift+Down -> "D" and "E" should be selected. |
| 2010 |
QTest::keyClick(&view, Qt::Key_Down, Qt::ShiftModifier); |
| 2011 |
selected = view.selectionModel()->selectedIndexes(); |
| 2012 |
QCOMPARE(selected.size(), 2); |
| 2013 |
QVERIFY(selected.contains(indexD)); |
| 2014 |
QVERIFY(selected.contains(indexE)); |
| 2015 |
} |
| 2016 |
|
| 2017 |
void tst_QAbstractItemView::QTBUG48968_reentrant_updateEditorGeometries() |
| 2018 |
{ |
| 2019 |
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) |
| 2020 |
QSKIP("Wayland: This fails. Figure out why."); |
| 2021 |
|
| 2022 |
QTreeView tree; |
| 2023 |
QStandardItemModel *m = new QStandardItemModel(&tree); |
| 2024 |
for (int i = 0; i < 10; ++i) { |
| 2025 |
QStandardItem *item = new QStandardItem(QString("Item number %1").arg(i)); |
| 2026 |
item->setEditable(true); |
| 2027 |
for (int j = 0; j < 5; ++j) { |
| 2028 |
QStandardItem *child = new QStandardItem(QString("Child Item number %1").arg(j)); |
| 2029 |
item->setChild(j, 0, child); |
| 2030 |
} |
| 2031 |
m->setItem(i, 0, item); |
| 2032 |
} |
| 2033 |
|
| 2034 |
tree.setModel(m); |
| 2035 |
tree.setRootIsDecorated(false); |
| 2036 |
connect(&tree, &QTreeView::doubleClicked, |
| 2037 |
&tree, &QTreeView::setRootIndex); |
| 2038 |
tree.show(); |
| 2039 |
QVERIFY(QTest::qWaitForWindowActive(&tree)); |
| 2040 |
|
| 2041 |
// Trigger editing idx |
| 2042 |
QModelIndex idx = m->index(1, 0); |
| 2043 |
const QPoint pos = tree.visualRect(idx).center(); |
| 2044 |
QTest::mouseClick(tree.viewport(), Qt::LeftButton, Qt::NoModifier, pos); |
| 2045 |
QTest::mouseDClick(tree.viewport(), Qt::LeftButton, Qt::NoModifier, pos); |
| 2046 |
|
| 2047 |
// Add more children to idx |
| 2048 |
QStandardItem *item = m->itemFromIndex(idx); |
| 2049 |
for (int j = 5; j < 10; ++j) { |
| 2050 |
QStandardItem *child = new QStandardItem(QString("Child Item number %1").arg(j)); |
| 2051 |
item->setChild(j, 0, child); |
| 2052 |
} |
| 2053 |
|
| 2054 |
// No crash, all fine. |
| 2055 |
} |
| 2056 |
|
| 2057 |
class ScrollModeProxyStyle: public QProxyStyle |
| 2058 |
{ |
| 2059 |
Q_OBJECT |
| 2060 |
public: |
| 2061 |
ScrollModeProxyStyle(QAbstractItemView::ScrollMode sm, QStyle *style = nullptr) |
| 2062 |
: QProxyStyle(style) |
| 2063 |
, scrollMode(sm == QAbstractItemView::ScrollPerItem ? |
| 2064 |
QAbstractItemView::ScrollPerPixel : QAbstractItemView::ScrollPerItem) |
| 2065 |
{ } |
| 2066 |
|
| 2067 |
int styleHint(QStyle::StyleHint hint, const QStyleOption *opt, |
| 2068 |
const QWidget *w, QStyleHintReturn *returnData) const override |
| 2069 |
{ |
| 2070 |
if (hint == SH_ItemView_ScrollMode) |
| 2071 |
return scrollMode; |
| 2072 |
|
| 2073 |
return baseStyle()->styleHint(hint, opt, w, returnData); |
| 2074 |
} |
| 2075 |
|
| 2076 |
QAbstractItemView::ScrollMode scrollMode; |
| 2077 |
}; |
| 2078 |
|
| 2079 |
void tst_QAbstractItemView::QTBUG50102_SH_ItemView_ScrollMode() |
| 2080 |
{ |
| 2081 |
QListView view; |
| 2082 |
|
| 2083 |
// Default comes from the style |
| 2084 |
auto styleScrollMode = static_cast<QAbstractItemView::ScrollMode>( |
| 2085 |
view.style()->styleHint(QStyle::SH_ItemView_ScrollMode, nullptr, &view, nullptr)); |
| 2086 |
QCOMPARE(view.verticalScrollMode(), styleScrollMode); |
| 2087 |
QCOMPARE(view.horizontalScrollMode(), styleScrollMode); |
| 2088 |
|
| 2089 |
// Change style, get new value |
| 2090 |
ScrollModeProxyStyle proxyStyle1(styleScrollMode); |
| 2091 |
view.setStyle(&proxyStyle1); |
| 2092 |
auto proxyScrollMode = static_cast<QAbstractItemView::ScrollMode>( |
| 2093 |
view.style()->styleHint(QStyle::SH_ItemView_ScrollMode, nullptr, &view, nullptr)); |
| 2094 |
QVERIFY(styleScrollMode != proxyScrollMode); |
| 2095 |
QCOMPARE(view.verticalScrollMode(), proxyScrollMode); |
| 2096 |
QCOMPARE(view.horizontalScrollMode(), proxyScrollMode); |
| 2097 |
|
| 2098 |
// Explicitly set vertical, same value |
| 2099 |
view.setVerticalScrollMode(proxyScrollMode); |
| 2100 |
QCOMPARE(view.verticalScrollMode(), proxyScrollMode); |
| 2101 |
QCOMPARE(view.horizontalScrollMode(), proxyScrollMode); |
| 2102 |
|
| 2103 |
// Change style, won't change value for vertical, will change for horizontal |
| 2104 |
ScrollModeProxyStyle proxyStyle2(proxyScrollMode); |
| 2105 |
view.setStyle(&proxyStyle2); |
| 2106 |
QCOMPARE(view.verticalScrollMode(), proxyScrollMode); |
| 2107 |
QCOMPARE(view.horizontalScrollMode(), styleScrollMode); |
| 2108 |
} |
| 2109 |
|
| 2110 |
void tst_QAbstractItemView::QTBUG50535_update_on_new_selection_model() |
| 2111 |
{ |
| 2112 |
QStandardItemModel model; |
| 2113 |
for (int i = 0; i < 10; ++i) |
| 2114 |
model.appendRow(new QStandardItem(QStringLiteral("%1").arg(i))); |
| 2115 |
|
| 2116 |
class ListView : public QListView |
| 2117 |
{ |
| 2118 |
public: |
| 2119 |
using QListView::QListView; |
| 2120 |
|
| 2121 |
void setSelectionModel(QItemSelectionModel *model) override |
| 2122 |
{ |
| 2123 |
m_deselectedMustBeEmpty = !selectionModel() || !model || selectionModel()->model() != model->model(); |
| 2124 |
QListView::setSelectionModel(model); |
| 2125 |
m_deselectedMustBeEmpty = false; |
| 2126 |
} |
| 2127 |
int m_paintEventsCount = 0; |
| 2128 |
bool selectionChangedOk() const { return m_selectionChangedOk; } |
| 2129 |
|
| 2130 |
protected: |
| 2131 |
bool viewportEvent(QEvent *event) override |
| 2132 |
{ |
| 2133 |
if (event->type() == QEvent::Paint) |
| 2134 |
++m_paintEventsCount; |
| 2135 |
return QListView::viewportEvent(event); |
| 2136 |
} |
| 2137 |
|
| 2138 |
void selectionChanged(const QItemSelection &selected, |
| 2139 |
const QItemSelection &deselected) override |
| 2140 |
{ |
| 2141 |
if (m_deselectedMustBeEmpty && !deselected.isEmpty()) |
| 2142 |
m_selectionChangedOk = false; |
| 2143 |
|
| 2144 |
// Make sure both selections belong to the same model |
| 2145 |
const auto selIndexes = selected.indexes(); |
| 2146 |
const auto deselIndexes = deselected.indexes(); |
| 2147 |
for (const QModelIndex &nmi : selIndexes) { |
| 2148 |
for (const QModelIndex &omi : deselIndexes) |
| 2149 |
m_selectionChangedOk = m_selectionChangedOk && (nmi.model() == omi.model()); |
| 2150 |
} |
| 2151 |
QListView::selectionChanged(selected, deselected); |
| 2152 |
} |
| 2153 |
private: |
| 2154 |
bool m_deselectedMustBeEmpty = false; |
| 2155 |
bool m_selectionChangedOk = true; |
| 2156 |
}; |
| 2157 |
|
| 2158 |
// keep the current/selected row in the "low range", i.e. be sure it's visible, otherwise we |
| 2159 |
// don't get updates and the test fails. |
| 2160 |
|
| 2161 |
ListView view; |
| 2162 |
view.setModel(&model); |
| 2163 |
view.selectionModel()->setCurrentIndex(model.index(1, 0), QItemSelectionModel::SelectCurrent); |
| 2164 |
view.show(); |
| 2165 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2166 |
QVERIFY(view.selectionChangedOk()); |
| 2167 |
|
| 2168 |
QItemSelectionModel selectionModel(&model); |
| 2169 |
selectionModel.setCurrentIndex(model.index(2, 0), QItemSelectionModel::Current); |
| 2170 |
|
| 2171 |
int oldPaintEventsCount = view.m_paintEventsCount; |
| 2172 |
view.setSelectionModel(&selectionModel); |
| 2173 |
QTRY_VERIFY(view.m_paintEventsCount > oldPaintEventsCount); |
| 2174 |
QVERIFY(view.selectionChangedOk()); |
| 2175 |
|
| 2176 |
|
| 2177 |
QItemSelectionModel selectionModel2(&model); |
| 2178 |
selectionModel2.select(model.index(0, 0), QItemSelectionModel::ClearAndSelect); |
| 2179 |
selectionModel2.setCurrentIndex(model.index(1, 0), QItemSelectionModel::Current); |
| 2180 |
|
| 2181 |
oldPaintEventsCount = view.m_paintEventsCount; |
| 2182 |
view.setSelectionModel(&selectionModel2); |
| 2183 |
QTRY_VERIFY(view.m_paintEventsCount > oldPaintEventsCount); |
| 2184 |
QVERIFY(view.selectionChangedOk()); |
| 2185 |
|
| 2186 |
// Tests QAbstractItemView::selectionChanged |
| 2187 |
QStandardItemModel model1; |
| 2188 |
for (int i = 0; i < 10; ++i) |
| 2189 |
model1.appendRow(new QStandardItem(QString::number(i))); |
| 2190 |
view.setModel(&model1); |
| 2191 |
|
| 2192 |
QItemSelectionModel selectionModel1(&model1); |
| 2193 |
selectionModel1.select(model1.index(0, 0), QItemSelectionModel::ClearAndSelect); |
| 2194 |
selectionModel1.setCurrentIndex(model1.index(1, 0), QItemSelectionModel::Current); |
| 2195 |
view.setSelectionModel(&selectionModel1); |
| 2196 |
QVERIFY(view.selectionChangedOk()); |
| 2197 |
} |
| 2198 |
|
| 2199 |
void tst_QAbstractItemView::testSelectionModelInSyncWithView() |
| 2200 |
{ |
| 2201 |
QStandardItemModel model; |
| 2202 |
for (int i = 0; i < 10; ++i) |
| 2203 |
model.appendRow(new QStandardItem(QString::number(i))); |
| 2204 |
|
| 2205 |
class ListView : public QListView |
| 2206 |
{ |
| 2207 |
public: |
| 2208 |
using QListView::selectedIndexes; |
| 2209 |
}; |
| 2210 |
|
| 2211 |
ListView view; |
| 2212 |
QVERIFY(!view.selectionModel()); |
| 2213 |
|
| 2214 |
view.setModel(&model); |
| 2215 |
QVERIFY(view.selectionModel()); |
| 2216 |
QVERIFY(view.selectedIndexes().isEmpty()); |
| 2217 |
QVERIFY(view.selectionModel()->selection().isEmpty()); |
| 2218 |
|
| 2219 |
view.setCurrentIndex(model.index(0, 0)); |
| 2220 |
QCOMPARE(view.currentIndex(), model.index(0, 0)); |
| 2221 |
QCOMPARE(view.selectionModel()->currentIndex(), model.index(0, 0)); |
| 2222 |
|
| 2223 |
view.selectionModel()->setCurrentIndex(model.index(1, 0), QItemSelectionModel::SelectCurrent); |
| 2224 |
QCOMPARE(view.currentIndex(), model.index(1, 0)); |
| 2225 |
QCOMPARE(view.selectedIndexes(), QModelIndexList() << model.index(1, 0)); |
| 2226 |
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0)); |
| 2227 |
QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(1, 0)); |
| 2228 |
|
| 2229 |
view.show(); |
| 2230 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2231 |
|
| 2232 |
QItemSelectionModel selectionModel(&model); |
| 2233 |
selectionModel.setCurrentIndex(model.index(2, 0), QItemSelectionModel::Current); |
| 2234 |
|
| 2235 |
view.setSelectionModel(&selectionModel); |
| 2236 |
QCOMPARE(view.currentIndex(), model.index(2, 0)); |
| 2237 |
QCOMPARE(view.selectedIndexes(), QModelIndexList()); |
| 2238 |
QCOMPARE(view.selectionModel()->currentIndex(), model.index(2, 0)); |
| 2239 |
QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList()); |
| 2240 |
|
| 2241 |
|
| 2242 |
QItemSelectionModel selectionModel2(&model); |
| 2243 |
selectionModel2.select(model.index(0, 0), QItemSelectionModel::ClearAndSelect); |
| 2244 |
selectionModel2.setCurrentIndex(model.index(1, 0), QItemSelectionModel::Current); |
| 2245 |
|
| 2246 |
view.setSelectionModel(&selectionModel2); |
| 2247 |
QCOMPARE(view.currentIndex(), model.index(1, 0)); |
| 2248 |
QCOMPARE(view.selectedIndexes(), QModelIndexList() << model.index(0, 0)); |
| 2249 |
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0)); |
| 2250 |
QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(0, 0)); |
| 2251 |
} |
| 2252 |
|
| 2253 |
class SetSelectionTestView : public QListView |
| 2254 |
{ |
| 2255 |
Q_OBJECT |
| 2256 |
public: |
| 2257 |
using QListView::QListView; |
| 2258 |
signals: |
| 2259 |
void setSelectionCalled(const QRect &rect); |
| 2260 |
|
| 2261 |
protected: |
| 2262 |
void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) override |
| 2263 |
{ |
| 2264 |
emit setSelectionCalled(rect); |
| 2265 |
QListView::setSelection(rect, flags); |
| 2266 |
} |
| 2267 |
}; |
| 2268 |
|
| 2269 |
void tst_QAbstractItemView::testClickToSelect() |
| 2270 |
{ |
| 2271 |
// This test verifies that the QRect that is passed from QAbstractItemView::mousePressEvent |
| 2272 |
// to the virtual method QAbstractItemView::setSelection(const QRect &, SelectionFlags) |
| 2273 |
// is the 1x1 rect which conains exactly the clicked pixel if no modifiers are pressed. |
| 2274 |
|
| 2275 |
QStringListModel model({ "A", "B", "C" }); |
| 2276 |
|
| 2277 |
SetSelectionTestView view; |
| 2278 |
view.setModel(&model); |
| 2279 |
view.show(); |
| 2280 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2281 |
|
| 2282 |
QSignalSpy spy(&view, &SetSelectionTestView::setSelectionCalled); |
| 2283 |
|
| 2284 |
const QModelIndex indexA(model.index(0, 0)); |
| 2285 |
const QRect visualRectA = view.visualRect(indexA); |
| 2286 |
const QPoint centerA = visualRectA.center(); |
| 2287 |
|
| 2288 |
// Click the center of the visualRect of item "A" |
| 2289 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, centerA); |
| 2290 |
QCOMPARE(spy.size(), 1); |
| 2291 |
QCOMPARE(spy.back().front().value<QRect>(), QRect(centerA, QSize(1, 1))); |
| 2292 |
|
| 2293 |
// Click a point slightly away from the center |
| 2294 |
const QPoint nearCenterA = centerA + QPoint(1, 1); |
| 2295 |
QVERIFY(visualRectA.contains(nearCenterA)); |
| 2296 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, nearCenterA); |
| 2297 |
QCOMPARE(spy.size(), 2); |
| 2298 |
QCOMPARE(spy.back().front().value<QRect>(), QRect(nearCenterA, QSize(1, 1))); |
| 2299 |
} |
| 2300 |
|
| 2301 |
void tst_QAbstractItemView::testDialogAsEditor() |
| 2302 |
{ |
| 2303 |
DialogItemDelegate delegate; |
| 2304 |
|
| 2305 |
QStandardItemModel model; |
| 2306 |
model.appendRow(new QStandardItem(QStringLiteral("editme"))); |
| 2307 |
|
| 2308 |
QListView view; |
| 2309 |
view.setItemDelegate(&delegate); |
| 2310 |
view.setModel(&model); |
| 2311 |
view.show(); |
| 2312 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2313 |
|
| 2314 |
view.edit(model.index(0,0)); |
| 2315 |
|
| 2316 |
QVERIFY(QTest::qWaitForWindowExposed(delegate.openedEditor)); |
| 2317 |
|
| 2318 |
delegate.openedEditor->reject(); |
| 2319 |
QApplication::processEvents(); |
| 2320 |
|
| 2321 |
QCOMPARE(delegate.result, QDialog::Rejected); |
| 2322 |
|
| 2323 |
view.edit(model.index(0,0)); |
| 2324 |
|
| 2325 |
QVERIFY(QTest::qWaitForWindowExposed(delegate.openedEditor)); |
| 2326 |
|
| 2327 |
delegate.openedEditor->accept(); |
| 2328 |
QApplication::processEvents(); |
| 2329 |
|
| 2330 |
QCOMPARE(delegate.result, QDialog::Accepted); |
| 2331 |
} |
| 2332 |
|
| 2333 |
class HoverItemDelegate : public QStyledItemDelegate |
| 2334 |
{ |
| 2335 |
public: |
| 2336 |
using QStyledItemDelegate::QStyledItemDelegate; |
| 2337 |
void paint(QPainter *painter, const QStyleOptionViewItem &opt, |
| 2338 |
const QModelIndex &index) const override |
| 2339 |
{ |
| 2340 |
Q_UNUSED(painter); |
| 2341 |
|
| 2342 |
if (!(opt.state & QStyle::State_MouseOver)) { |
| 2343 |
|
| 2344 |
// We don't want to set m_paintedWithoutHover for any item so check for the item at 0,0 |
| 2345 |
if (index.row() == 0 && index.column() == 0) |
| 2346 |
m_paintedWithoutHover = true; |
| 2347 |
} |
| 2348 |
} |
| 2349 |
|
| 2350 |
mutable bool m_paintedWithoutHover = false; |
| 2351 |
}; |
| 2352 |
|
| 2353 |
void tst_QAbstractItemView::QTBUG46785_mouseout_hover_state() |
| 2354 |
{ |
| 2355 |
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) |
| 2356 |
QSKIP("Wayland: This fails. Figure out why."); |
| 2357 |
|
| 2358 |
HoverItemDelegate delegate; |
| 2359 |
|
| 2360 |
QTableWidget table(5, 5); |
| 2361 |
table.verticalHeader()->hide(); |
| 2362 |
table.horizontalHeader()->hide(); |
| 2363 |
table.setMouseTracking(true); |
| 2364 |
table.setItemDelegate(&delegate); |
| 2365 |
centerOnScreen(&table); |
| 2366 |
table.show(); |
| 2367 |
QVERIFY(QTest::qWaitForWindowActive(&table)); |
| 2368 |
|
| 2369 |
QModelIndex item = table.model()->index(0, 0); |
| 2370 |
QRect itemRect = table.visualRect(item); |
| 2371 |
|
| 2372 |
// Move the mouse into the center of the item at 0,0 to cause a paint event to occur |
| 2373 |
QTest::mouseMove(table.viewport(), itemRect.center()); |
| 2374 |
QTest::mouseClick(table.viewport(), Qt::LeftButton, {}, itemRect.center()); |
| 2375 |
|
| 2376 |
delegate.m_paintedWithoutHover = false; |
| 2377 |
|
| 2378 |
QTest::mouseMove(table.viewport(), QPoint(-50, 0)); |
| 2379 |
|
| 2380 |
QTRY_VERIFY(delegate.m_paintedWithoutHover); |
| 2381 |
} |
| 2382 |
|
| 2383 |
void tst_QAbstractItemView::testClearModelInClickedSignal() |
| 2384 |
{ |
| 2385 |
QStringListModel model({"A", "B"}); |
| 2386 |
|
| 2387 |
QListView view; |
| 2388 |
view.setModel(&model); |
| 2389 |
view.show(); |
| 2390 |
|
| 2391 |
connect(&view, &QListView::clicked, [&view](const QModelIndex &index) |
| 2392 |
{ |
| 2393 |
view.setModel(nullptr); |
| 2394 |
QCOMPARE(index.data().toString(), QStringLiteral("B")); |
| 2395 |
}); |
| 2396 |
|
| 2397 |
QModelIndex index = view.model()->index(1, 0); |
| 2398 |
QVERIFY(index.isValid()); |
| 2399 |
QPoint p = view.visualRect(index).center(); |
| 2400 |
|
| 2401 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, p); |
| 2402 |
|
| 2403 |
QCOMPARE(view.model(), nullptr); |
| 2404 |
} |
| 2405 |
|
| 2406 |
void tst_QAbstractItemView::inputMethodEnabled_data() |
| 2407 |
{ |
| 2408 |
QTest::addColumn<QByteArray>("viewType"); |
| 2409 |
QTest::addColumn<Qt::ItemFlags>("itemFlags"); |
| 2410 |
QTest::addColumn<bool>("result"); |
| 2411 |
|
| 2412 |
const QList<QByteArray> widgets { "QListView", "QTreeView", "QTableView" }; |
| 2413 |
for (const QByteArray &widget : widgets) { |
| 2414 |
QTest::newRow(widget + ": no flags") |
| 2415 |
<< widget << Qt::ItemFlags(Qt::NoItemFlags) << false; |
| 2416 |
QTest::newRow(widget + ": checkable") |
| 2417 |
<< widget << Qt::ItemFlags(Qt::ItemIsUserCheckable) << false; |
| 2418 |
QTest::newRow(widget + ": selectable") |
| 2419 |
<< widget << Qt::ItemFlags(Qt::ItemIsSelectable) << false; |
| 2420 |
QTest::newRow(widget + ": enabled") |
| 2421 |
<< widget << Qt::ItemFlags(Qt::ItemIsEnabled) << false; |
| 2422 |
QTest::newRow(widget + ": selectable|enabled") |
| 2423 |
<< widget << Qt::ItemFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled) << false; |
| 2424 |
QTest::newRow(widget + ": editable|enabled") |
| 2425 |
<< widget << Qt::ItemFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled) << true; |
| 2426 |
QTest::newRow(widget + ": editable|enabled|selectable") |
| 2427 |
<< widget << Qt::ItemFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable) << true; |
| 2428 |
} |
| 2429 |
} |
| 2430 |
|
| 2431 |
void tst_QAbstractItemView::inputMethodEnabled() |
| 2432 |
{ |
| 2433 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 2434 |
QSKIP("Window activation is not supported"); |
| 2435 |
|
| 2436 |
QFETCH(QByteArray, viewType); |
| 2437 |
QFETCH(Qt::ItemFlags, itemFlags); |
| 2438 |
QFETCH(bool, result); |
| 2439 |
|
| 2440 |
QScopedPointer<QAbstractItemView> view(viewFromString(viewType)); |
| 2441 |
|
| 2442 |
centerOnScreen(view.data()); |
| 2443 |
view->show(); |
| 2444 |
QVERIFY(QTest::qWaitForWindowExposed(view.data())); |
| 2445 |
|
| 2446 |
QStandardItemModel *model = new QStandardItemModel(view.data()); |
| 2447 |
QStandardItem *item = new QStandardItem("first item"); |
| 2448 |
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
| 2449 |
model->appendRow(item); |
| 2450 |
|
| 2451 |
QStandardItem *secondItem = new QStandardItem("test item"); |
| 2452 |
secondItem->setFlags(Qt::ItemFlags(itemFlags)); |
| 2453 |
model->appendRow(secondItem); |
| 2454 |
|
| 2455 |
view->setModel(model); |
| 2456 |
|
| 2457 |
// Check current changed |
| 2458 |
view->setCurrentIndex(model->index(0, 0)); |
| 2459 |
QVERIFY(!view->testAttribute(Qt::WA_InputMethodEnabled)); |
| 2460 |
view->setCurrentIndex(model->index(1, 0)); |
| 2461 |
QCOMPARE(view->testAttribute(Qt::WA_InputMethodEnabled), result); |
| 2462 |
view->setCurrentIndex(model->index(0, 0)); |
| 2463 |
QVERIFY(!view->testAttribute(Qt::WA_InputMethodEnabled)); |
| 2464 |
|
| 2465 |
// Check focus by switching the activation of the window to force a focus in |
| 2466 |
view->setCurrentIndex(model->index(1, 0)); |
| 2467 |
QVERIFY(QTest::qWaitForWindowActive(view.data())); |
| 2468 |
QCOMPARE(view->testAttribute(Qt::WA_InputMethodEnabled), result); |
| 2469 |
|
| 2470 |
view->setCurrentIndex(QModelIndex()); |
| 2471 |
QVERIFY(!view->testAttribute(Qt::WA_InputMethodEnabled)); |
| 2472 |
QVERIFY(QTest::qWaitForWindowActive(view.data())); |
| 2473 |
QModelIndex index = model->index(1, 0); |
| 2474 |
QPoint p = view->visualRect(index).center(); |
| 2475 |
QTest::mouseClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, p); |
| 2476 |
if (itemFlags & Qt::ItemIsEnabled) |
| 2477 |
QCOMPARE(view->currentIndex(), index); |
| 2478 |
QCOMPARE(view->testAttribute(Qt::WA_InputMethodEnabled), result); |
| 2479 |
|
| 2480 |
index = model->index(0, 0); |
| 2481 |
QVERIFY(QTest::qWaitForWindowActive(view.data())); |
| 2482 |
p = view->visualRect(index).center(); |
| 2483 |
QTest::mouseClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, p); |
| 2484 |
QCOMPARE(view->currentIndex(), index); |
| 2485 |
QVERIFY(!view->testAttribute(Qt::WA_InputMethodEnabled)); |
| 2486 |
|
| 2487 |
// There is a case when it goes to the first visible item so we |
| 2488 |
// make the flags of the first item match the ones we are testing |
| 2489 |
// to check the attribute correctly |
| 2490 |
QApplicationPrivate::setActiveWindow(nullptr); |
| 2491 |
view->setCurrentIndex(QModelIndex()); |
| 2492 |
view->reset(); |
| 2493 |
item->setFlags(Qt::ItemFlags(itemFlags)); |
| 2494 |
QApplicationPrivate::setActiveWindow(view.data()); |
| 2495 |
QVERIFY(QTest::qWaitForWindowActive(view.data())); |
| 2496 |
QCOMPARE(view->testAttribute(Qt::WA_InputMethodEnabled), result); |
| 2497 |
} |
| 2498 |
|
| 2499 |
void tst_QAbstractItemView::currentFollowsIndexWidget_data() |
| 2500 |
{ |
| 2501 |
QTest::addColumn<QByteArray>("viewType"); |
| 2502 |
|
| 2503 |
const QList<QByteArray> widgets { "QListView", "QTreeView", "QTableView" }; |
| 2504 |
for (const QByteArray &widget : widgets) |
| 2505 |
QTest::newRow(widget) << widget; |
| 2506 |
} |
| 2507 |
|
| 2508 |
void tst_QAbstractItemView::currentFollowsIndexWidget() |
| 2509 |
{ |
| 2510 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 2511 |
QSKIP("Window activation is not supported"); |
| 2512 |
|
| 2513 |
QFETCH(QByteArray, viewType); |
| 2514 |
|
| 2515 |
QScopedPointer<QAbstractItemView> view(viewFromString(viewType)); |
| 2516 |
|
| 2517 |
centerOnScreen(view.data()); |
| 2518 |
view->show(); |
| 2519 |
QVERIFY(QTest::qWaitForWindowExposed(view.data())); |
| 2520 |
|
| 2521 |
QStandardItemModel *model = new QStandardItemModel(view.data()); |
| 2522 |
QStandardItem *item1 = new QStandardItem("first item"); |
| 2523 |
item1->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
| 2524 |
model->appendRow(item1); |
| 2525 |
|
| 2526 |
QStandardItem *item2 = new QStandardItem("test item"); |
| 2527 |
item2->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
| 2528 |
model->appendRow(item2); |
| 2529 |
|
| 2530 |
view->setModel(model); |
| 2531 |
QLineEdit *lineEdit1 = new QLineEdit; |
| 2532 |
QLineEdit *lineEdit2 = new QLineEdit; |
| 2533 |
view->setIndexWidget(item1->index(), lineEdit1); |
| 2534 |
view->setIndexWidget(item2->index(), lineEdit2); |
| 2535 |
|
| 2536 |
lineEdit2->setFocus(); |
| 2537 |
QTRY_VERIFY(lineEdit2->hasFocus()); |
| 2538 |
QCOMPARE(view->currentIndex(), item2->index()); |
| 2539 |
lineEdit1->setFocus(); |
| 2540 |
QTRY_VERIFY(lineEdit1->hasFocus()); |
| 2541 |
QCOMPARE(view->currentIndex(), item1->index()); |
| 2542 |
} |
| 2543 |
|
| 2544 |
class EditorItemDelegate : public QStyledItemDelegate |
| 2545 |
{ |
| 2546 |
Q_OBJECT |
| 2547 |
public: |
| 2548 |
using QStyledItemDelegate::QStyledItemDelegate; |
| 2549 |
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, |
| 2550 |
const QModelIndex &) const override |
| 2551 |
{ |
| 2552 |
openedEditor = new QLineEdit(parent); |
| 2553 |
return openedEditor; |
| 2554 |
} |
| 2555 |
mutable QPointer<QWidget> openedEditor = nullptr; |
| 2556 |
}; |
| 2557 |
|
| 2558 |
// Testing the case reported in QTBUG-62253. |
| 2559 |
// When an itemview with an editor that has focus loses focus |
| 2560 |
// due to a change in the active window then we need to check |
| 2561 |
// that the itemview gets focus once the activation is back |
| 2562 |
// on the original window. |
| 2563 |
void tst_QAbstractItemView::checkFocusAfterActivationChanges_data() |
| 2564 |
{ |
| 2565 |
currentFollowsIndexWidget_data(); |
| 2566 |
} |
| 2567 |
|
| 2568 |
void tst_QAbstractItemView::checkFocusAfterActivationChanges() |
| 2569 |
{ |
| 2570 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 2571 |
QSKIP("Window activation is not supported"); |
| 2572 |
|
| 2573 |
QFETCH(QByteArray, viewType); |
| 2574 |
|
| 2575 |
const QRect availableGeo = QGuiApplication::primaryScreen()->availableGeometry(); |
| 2576 |
const int halfWidth = availableGeo.width() / 2; |
| 2577 |
QWidget otherTopLevel; |
| 2578 |
otherTopLevel.setGeometry(availableGeo.x(), availableGeo.y(), |
| 2579 |
halfWidth, availableGeo.height()); |
| 2580 |
otherTopLevel.show(); |
| 2581 |
|
| 2582 |
QWidget w; |
| 2583 |
w.setGeometry(availableGeo.x() + halfWidth, availableGeo.y(), |
| 2584 |
halfWidth, availableGeo.height()); |
| 2585 |
QLineEdit *le = new QLineEdit(&w); |
| 2586 |
QAbstractItemView *view = viewFromString(viewType, &w); |
| 2587 |
|
| 2588 |
QStandardItemModel model(5, 5); |
| 2589 |
view->setModel(&model); |
| 2590 |
view->move(0, 50); |
| 2591 |
EditorItemDelegate delegate; |
| 2592 |
view->setItemDelegate(&delegate); |
| 2593 |
w.show(); |
| 2594 |
|
| 2595 |
QVERIFY(QTest::qWaitForWindowActive(&w)); |
| 2596 |
QVERIFY(le->hasFocus()); |
| 2597 |
|
| 2598 |
view->setFocus(); |
| 2599 |
QVERIFY(view->hasFocus()); |
| 2600 |
|
| 2601 |
view->edit(model.index(0,0)); |
| 2602 |
QVERIFY(QTest::qWaitForWindowExposed(delegate.openedEditor)); |
| 2603 |
QVERIFY(delegate.openedEditor->hasFocus()); |
| 2604 |
|
| 2605 |
QApplicationPrivate::setActiveWindow(&otherTopLevel); |
| 2606 |
otherTopLevel.setFocus(); |
| 2607 |
QTRY_VERIFY(!delegate.openedEditor); |
| 2608 |
|
| 2609 |
QApplicationPrivate::setActiveWindow(&w); |
| 2610 |
QVERIFY(QTest::qWaitForWindowActive(&w)); |
| 2611 |
QVERIFY(view->hasFocus()); |
| 2612 |
} |
| 2613 |
|
| 2614 |
void tst_QAbstractItemView::dragSelectAfterNewPress() |
| 2615 |
{ |
| 2616 |
QStandardItemModel model; |
| 2617 |
for (int i = 0; i < 10; ++i) { |
| 2618 |
QStandardItem *item = new QStandardItem(QString::number(i)); |
| 2619 |
model.setItem(i, 0, item); |
| 2620 |
} |
| 2621 |
|
| 2622 |
QListView view; |
| 2623 |
view.setFixedSize(160, 650); // Minimum width for windows with frame on Windows 8 |
| 2624 |
view.setSelectionMode(QListView::ExtendedSelection); |
| 2625 |
view.setModel(&model); |
| 2626 |
centerOnScreen(&view); |
| 2627 |
moveCursorAway(&view); |
| 2628 |
view.show(); |
| 2629 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2630 |
|
| 2631 |
QModelIndex index0 = model.index(0, 0); |
| 2632 |
QModelIndex index2 = model.index(2, 0); |
| 2633 |
|
| 2634 |
view.setCurrentIndex(index0); |
| 2635 |
QCOMPARE(view.currentIndex(), index0); |
| 2636 |
|
| 2637 |
// Select item 0 using a single click |
| 2638 |
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2639 |
view.visualRect(index0).center()); |
| 2640 |
QCOMPARE(view.currentIndex(), index0); |
| 2641 |
|
| 2642 |
// Press to select item 2 |
| 2643 |
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, |
| 2644 |
view.visualRect(index2).center()); |
| 2645 |
QCOMPARE(view.currentIndex(), index2); |
| 2646 |
|
| 2647 |
// Verify that the selection worked OK |
| 2648 |
QModelIndexList selected = view.selectionModel()->selectedIndexes(); |
| 2649 |
QCOMPARE(selected.size(), 3); |
| 2650 |
for (int i = 0; i < 2; ++i) |
| 2651 |
QVERIFY(selected.contains(model.index(i, 0))); |
| 2652 |
|
| 2653 |
QModelIndex index5 = model.index(5, 0); |
| 2654 |
const QPoint releasePos = view.visualRect(index5).center(); |
| 2655 |
// The mouse move event has to be created manually because the QTest framework does not |
| 2656 |
// contain a function for mouse moves with buttons pressed |
| 2657 |
QMouseEvent moveEvent2(QEvent::MouseMove, releasePos, view.viewport()->mapToGlobal(releasePos), |
| 2658 |
Qt::NoButton, Qt::LeftButton, Qt::ShiftModifier); |
| 2659 |
const bool moveEventReceived = qApp->notify(view.viewport(), &moveEvent2); |
| 2660 |
QVERIFY(moveEventReceived); |
| 2661 |
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, releasePos); |
| 2662 |
QCOMPARE(view.currentIndex(), index5); |
| 2663 |
|
| 2664 |
// Verify that the selection worked OK |
| 2665 |
selected = view.selectionModel()->selectedIndexes(); |
| 2666 |
QCOMPARE(selected.size(), 6); |
| 2667 |
for (int i = 0; i < 5; ++i) |
| 2668 |
QVERIFY(selected.contains(model.index(i, 0))); |
| 2669 |
} |
| 2670 |
|
| 2671 |
void tst_QAbstractItemView::dragWithSecondClick_data() |
| 2672 |
{ |
| 2673 |
QTest::addColumn<QByteArray>("viewClass"); |
| 2674 |
QTest::addColumn<bool>("doubleClick"); |
| 2675 |
const QList<QByteArray> widgets { "QListView", "QTreeView" }; |
| 2676 |
for (const QByteArray &widget : widgets) { |
| 2677 |
QTest::newRow(widget + ": DoubleClick") << widget << true; |
| 2678 |
QTest::newRow(widget + ": Two Single Clicks") << widget << false; |
| 2679 |
} |
| 2680 |
} |
| 2681 |
|
| 2682 |
// inject the ability to record which indexes get dragged into any QAbstractItemView class |
| 2683 |
struct DragRecorder |
| 2684 |
{ |
| 2685 |
virtual ~DragRecorder() = default; |
| 2686 |
bool dragStarted = false; |
| 2687 |
QModelIndexList draggedIndexes; |
| 2688 |
QAbstractItemView *view; |
| 2689 |
}; |
| 2690 |
|
| 2691 |
template<class ViewClass> |
| 2692 |
class DragRecorderView : public ViewClass, public DragRecorder |
| 2693 |
{ |
| 2694 |
public: |
| 2695 |
DragRecorderView() |
| 2696 |
{ view = this; } |
| 2697 |
protected: |
| 2698 |
void startDrag(Qt::DropActions) override |
| 2699 |
{ |
| 2700 |
draggedIndexes = ViewClass::selectedIndexes(); |
| 2701 |
dragStarted = true; |
| 2702 |
} |
| 2703 |
}; |
| 2704 |
|
| 2705 |
void tst_QAbstractItemView::dragWithSecondClick() |
| 2706 |
{ |
| 2707 |
QFETCH(QByteArray, viewClass); |
| 2708 |
QFETCH(bool, doubleClick); |
| 2709 |
|
| 2710 |
QStandardItemModel model; |
| 2711 |
QStandardItem *parentItem = model.invisibleRootItem(); |
| 2712 |
for (int i = 0; i < 10; ++i) { |
| 2713 |
QStandardItem *item = new QStandardItem(QString("item %0").arg(i)); |
| 2714 |
item->setDragEnabled(true); |
| 2715 |
item->setEditable(false); |
| 2716 |
parentItem->appendRow(item); |
| 2717 |
} |
| 2718 |
|
| 2719 |
std::unique_ptr<DragRecorder> dragRecorder; |
| 2720 |
if (viewClass == "QTreeView") |
| 2721 |
dragRecorder.reset(new DragRecorderView<QTreeView>); |
| 2722 |
else if (viewClass == "QListView") |
| 2723 |
dragRecorder.reset(new DragRecorderView<QListView>); |
| 2724 |
|
| 2725 |
QAbstractItemView *view = dragRecorder->view; |
| 2726 |
view->setModel(&model); |
| 2727 |
view->setFixedSize(160, 650); // Minimum width for windows with frame on Windows 8 |
| 2728 |
view->setSelectionMode(QAbstractItemView::MultiSelection); |
| 2729 |
view->setDragDropMode(QAbstractItemView::InternalMove); |
| 2730 |
centerOnScreen(view); |
| 2731 |
moveCursorAway(view); |
| 2732 |
view->show(); |
| 2733 |
QVERIFY(QTest::qWaitForWindowExposed(view)); |
| 2734 |
|
| 2735 |
QModelIndex index0 = model.index(0, 0); |
| 2736 |
QModelIndex index1 = model.index(1, 0); |
| 2737 |
// Select item 0 using a single click |
| 2738 |
QTest::mouseClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2739 |
view->visualRect(index0).center()); |
| 2740 |
QCOMPARE(view->currentIndex(), index0); |
| 2741 |
|
| 2742 |
if (doubleClick) { |
| 2743 |
// press on same item within the double click interval |
| 2744 |
QTest::mouseDClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2745 |
view->visualRect(index0).center()); |
| 2746 |
} else { |
| 2747 |
// or on different item with a slow second press |
| 2748 |
QTest::mousePress(view->viewport(), Qt::LeftButton, Qt::NoModifier, |
| 2749 |
view->visualRect(index1).center()); |
| 2750 |
} |
| 2751 |
// then drag far enough with left button held |
| 2752 |
const QPoint dragTo = view->visualRect(index1).center() |
| 2753 |
+ QPoint(2 * QApplication::startDragDistance(), |
| 2754 |
2 * QApplication::startDragDistance()); |
| 2755 |
QMouseEvent mouseMoveEvent(QEvent::MouseMove, dragTo, view->viewport()->mapToGlobal(dragTo), |
| 2756 |
Qt::NoButton, Qt::LeftButton, Qt::NoModifier); |
| 2757 |
QVERIFY(QApplication::sendEvent(view->viewport(), &mouseMoveEvent)); |
| 2758 |
// twice since the view will first enter dragging state, then start the drag |
| 2759 |
// (not necessary to actually move the mouse) |
| 2760 |
QVERIFY(QApplication::sendEvent(view->viewport(), &mouseMoveEvent)); |
| 2761 |
QVERIFY(dragRecorder->dragStarted); |
| 2762 |
QTest::mouseRelease(view->viewport(), Qt::LeftButton, Qt::NoModifier, dragTo); |
| 2763 |
} |
| 2764 |
|
| 2765 |
void tst_QAbstractItemView::clickAfterDoubleClick() |
| 2766 |
{ |
| 2767 |
QTableWidget view(5, 5); |
| 2768 |
view.horizontalHeader()->hide(); |
| 2769 |
view.verticalHeader()->hide(); |
| 2770 |
view.setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 2771 |
view.show(); |
| 2772 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 2773 |
const QModelIndex index = view.model()->index(1, 1); |
| 2774 |
QVERIFY(index.isValid()); |
| 2775 |
const QPoint clickPoint = view.visualRect(index).center(); |
| 2776 |
|
| 2777 |
// must use the QWindow overloads so that modality is respected |
| 2778 |
QWindow *window = view.window()->windowHandle(); |
| 2779 |
int clickCount = 0; |
| 2780 |
|
| 2781 |
connect(&view, &QAbstractItemView::doubleClicked, [&]{ |
| 2782 |
QDialog dialog(&view); |
| 2783 |
dialog.setModal(true); |
| 2784 |
QTimer::singleShot(0, [&]{ dialog.close(); }); |
| 2785 |
dialog.exec(); |
| 2786 |
}); |
| 2787 |
connect(&view, &QAbstractItemView::clicked, [&]{ |
| 2788 |
++clickCount; |
| 2789 |
}); |
| 2790 |
|
| 2791 |
QTest::mouseClick(window, Qt::LeftButton, {}, clickPoint); |
| 2792 |
QCOMPARE(clickCount, 1); |
| 2793 |
// generates a click followed by a double click; double click opens |
| 2794 |
// dialog that eats second release |
| 2795 |
QTest::mouseDClick(window, Qt::LeftButton, {}, clickPoint); |
| 2796 |
QCOMPARE(clickCount, 2); |
| 2797 |
QTest::mouseClick(window, Qt::LeftButton, {}, clickPoint); |
| 2798 |
QCOMPARE(clickCount, 3); |
| 2799 |
} |
| 2800 |
|
| 2801 |
void tst_QAbstractItemView::selectionCommand_data() |
| 2802 |
{ |
| 2803 |
QTest::addColumn<QAbstractItemView::SelectionMode>("selectionMode"); |
| 2804 |
QTest::addColumn<Qt::KeyboardModifier>("keyboardModifier"); |
| 2805 |
QTest::addColumn<QItemSelectionModel::SelectionFlag>("selectionFlag"); |
| 2806 |
|
| 2807 |
QTest::newRow("NoSelection - NoModifier") << QAbstractItemView::NoSelection << Qt::NoModifier << QItemSelectionModel::NoUpdate; |
| 2808 |
QTest::newRow("NoSelection - ShiftModifier") << QAbstractItemView::NoSelection << Qt::ShiftModifier << QItemSelectionModel::NoUpdate; |
| 2809 |
QTest::newRow("NoSelection - ControlModifier") << QAbstractItemView::NoSelection << Qt::ControlModifier << QItemSelectionModel::NoUpdate; |
| 2810 |
QTest::newRow("NoSelection - AltModifier") << QAbstractItemView::NoSelection << Qt::AltModifier << QItemSelectionModel::NoUpdate; |
| 2811 |
|
| 2812 |
QTest::newRow("SingleSelection - NoModifier") << QAbstractItemView::SingleSelection << Qt::NoModifier << QItemSelectionModel::ClearAndSelect; |
| 2813 |
QTest::newRow("SingleSelection - ShiftModifier") << QAbstractItemView::SingleSelection << Qt::ShiftModifier << QItemSelectionModel::ClearAndSelect; |
| 2814 |
QTest::newRow("SingleSelection - ControlModifier") << QAbstractItemView::SingleSelection << Qt::ControlModifier << QItemSelectionModel::ClearAndSelect; |
| 2815 |
QTest::newRow("SingleSelection - AltModifier") << QAbstractItemView::SingleSelection << Qt::AltModifier << QItemSelectionModel::ClearAndSelect; |
| 2816 |
|
| 2817 |
QTest::newRow("MultiSelection - NoModifier") << QAbstractItemView::MultiSelection << Qt::NoModifier << QItemSelectionModel::Toggle; |
| 2818 |
QTest::newRow("MultiSelection - ShiftModifier") << QAbstractItemView::MultiSelection << Qt::ShiftModifier << QItemSelectionModel::Toggle; |
| 2819 |
QTest::newRow("MultiSelection - ControlModifier") << QAbstractItemView::MultiSelection << Qt::ControlModifier << QItemSelectionModel::Toggle; |
| 2820 |
QTest::newRow("MultiSelection - AltModifier") << QAbstractItemView::MultiSelection << Qt::AltModifier << QItemSelectionModel::Toggle; |
| 2821 |
|
| 2822 |
QTest::newRow("ExtendedSelection - NoModifier") << QAbstractItemView::ExtendedSelection << Qt::NoModifier << QItemSelectionModel::ClearAndSelect; |
| 2823 |
QTest::newRow("ExtendedSelection - ShiftModifier") << QAbstractItemView::ExtendedSelection << Qt::ShiftModifier << QItemSelectionModel::SelectCurrent; |
| 2824 |
QTest::newRow("ExtendedSelection - ControlModifier") << QAbstractItemView::ExtendedSelection << Qt::ControlModifier << QItemSelectionModel::Toggle; |
| 2825 |
QTest::newRow("ExtendedSelection - AltModifier") << QAbstractItemView::ExtendedSelection << Qt::AltModifier << QItemSelectionModel::ClearAndSelect; |
| 2826 |
|
| 2827 |
QTest::newRow("ContiguousSelection - NoModifier") << QAbstractItemView::ContiguousSelection << Qt::NoModifier << QItemSelectionModel::ClearAndSelect; |
| 2828 |
QTest::newRow("ContiguousSelection - ShiftModifier") << QAbstractItemView::ContiguousSelection << Qt::ShiftModifier << QItemSelectionModel::SelectCurrent; |
| 2829 |
QTest::newRow("ContiguousSelection - ControlModifier") << QAbstractItemView::ContiguousSelection << Qt::ControlModifier << QItemSelectionModel::SelectCurrent; |
| 2830 |
QTest::newRow("ContiguousSelection - AltModifier") << QAbstractItemView::ContiguousSelection << Qt::AltModifier << QItemSelectionModel::ClearAndSelect; |
| 2831 |
} |
| 2832 |
|
| 2833 |
void tst_QAbstractItemView::selectionCommand() |
| 2834 |
{ |
| 2835 |
QFETCH(QAbstractItemView::SelectionMode, selectionMode); |
| 2836 |
QFETCH(Qt::KeyboardModifier, keyboardModifier); |
| 2837 |
QFETCH(QItemSelectionModel::SelectionFlag, selectionFlag); |
| 2838 |
|
| 2839 |
QTableView view; |
| 2840 |
view.setSelectionMode(selectionMode); |
| 2841 |
QTest::keyPress(&view, Qt::Key_A, keyboardModifier); |
| 2842 |
QCOMPARE(selectionFlag, view.selectionCommand(QModelIndex(), nullptr)); |
| 2843 |
} |
| 2844 |
|
| 2845 |
struct SelectionEvent |
| 2846 |
{ |
| 2847 |
enum MouseEvent |
| 2848 |
{ Press, Release, Click, Move }; |
| 2849 |
constexpr SelectionEvent(MouseEvent type, int r = -1) noexcept |
| 2850 |
: eventType(type), row(r) {} |
| 2851 |
constexpr SelectionEvent(MouseEvent type, Qt::KeyboardModifiers mod, int r = -1) noexcept |
| 2852 |
: eventType(type), keyboardModifiers(mod), row(r) {} |
| 2853 |
MouseEvent eventType = Press; |
| 2854 |
Qt::KeyboardModifiers keyboardModifiers = Qt::NoModifier; |
| 2855 |
int row = -1; |
| 2856 |
}; |
| 2857 |
|
| 2858 |
void tst_QAbstractItemView::mouseSelection_data() |
| 2859 |
{ |
| 2860 |
QTest::addColumn<QAbstractItemView::SelectionMode>("selectionMode"); |
| 2861 |
QTest::addColumn<bool>("dragEnabled"); |
| 2862 |
QTest::addColumn<QAbstractItemView::EditTrigger>("editTrigger"); |
| 2863 |
QTest::addColumn<QList<SelectionEvent>>("selectionEvents"); |
| 2864 |
QTest::addColumn<QList<int>>("selectedRows"); |
| 2865 |
|
| 2866 |
// single selection mode - always one row selected, modifiers ignored |
| 2867 |
QTest::addRow("Single:Press") << QAbstractItemView::SingleSelection << false |
| 2868 |
<< QAbstractItemView::NoEditTriggers |
| 2869 |
<< QList{SelectionEvent(SelectionEvent::Press, 1)} |
| 2870 |
<< QList{1}; |
| 2871 |
QTest::addRow("Single:Click") << QAbstractItemView::SingleSelection << false |
| 2872 |
<< QAbstractItemView::NoEditTriggers |
| 2873 |
<< QList{SelectionEvent{SelectionEvent::Click, 1}} |
| 2874 |
<< QList{1}; |
| 2875 |
QTest::addRow("Single:Press+Drag") << QAbstractItemView::SingleSelection << false |
| 2876 |
<< QAbstractItemView::NoEditTriggers |
| 2877 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 2878 |
SelectionEvent{SelectionEvent::Move, 2}, |
| 2879 |
SelectionEvent{SelectionEvent::Release}} |
| 2880 |
<< QList{2}; |
| 2881 |
QTest::addRow("Single:Shift+Click") << QAbstractItemView::SingleSelection << false |
| 2882 |
<< QAbstractItemView::NoEditTriggers |
| 2883 |
<< QList{SelectionEvent{SelectionEvent::Click, Qt::ShiftModifier, 2}} |
| 2884 |
<< QList{2}; |
| 2885 |
QTest::addRow("Single:Press;Ctrl+Press") << QAbstractItemView::SingleSelection << false |
| 2886 |
<< QAbstractItemView::NoEditTriggers |
| 2887 |
<< QList{SelectionEvent{SelectionEvent::Press, 3}, |
| 2888 |
SelectionEvent{SelectionEvent::Press, Qt::ControlModifier, 3}} |
| 2889 |
<< QList{3}; |
| 2890 |
QTest::addRow("Single:Ctrl+Click") << QAbstractItemView::SingleSelection << false |
| 2891 |
<< QAbstractItemView::NoEditTriggers |
| 2892 |
<< QList{SelectionEvent{SelectionEvent::Click, Qt::ControlModifier, 3}} |
| 2893 |
<< QList{3}; |
| 2894 |
QTest::addRow("Single:Click;Ctrl+Click") << QAbstractItemView::SingleSelection << false |
| 2895 |
<< QAbstractItemView::NoEditTriggers |
| 2896 |
<< QList{SelectionEvent{SelectionEvent::Click, 3}, |
| 2897 |
SelectionEvent{SelectionEvent::Click, Qt::ControlModifier, 3}} |
| 2898 |
<< QList<int>{}; |
| 2899 |
|
| 2900 |
// multi selection mode - selection toggles on press, selection can be drag-extended |
| 2901 |
// modifiers ignored |
| 2902 |
QTest::addRow("Multi:Press") << QAbstractItemView::MultiSelection << false |
| 2903 |
<< QAbstractItemView::NoEditTriggers |
| 2904 |
<< QList{SelectionEvent(SelectionEvent::Press, 1)} |
| 2905 |
<< QList{1}; |
| 2906 |
QTest::addRow("Multi:Press twice") << QAbstractItemView::MultiSelection << false |
| 2907 |
<< QAbstractItemView::NoEditTriggers |
| 2908 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 2909 |
SelectionEvent(SelectionEvent::Press, 1)} |
| 2910 |
<< QList<int>{}; |
| 2911 |
QTest::addRow("Multi:Press twice with drag enabled") << QAbstractItemView::MultiSelection << true |
| 2912 |
<< QAbstractItemView::NoEditTriggers |
| 2913 |
<< QList{SelectionEvent(SelectionEvent::Click, 1), |
| 2914 |
SelectionEvent(SelectionEvent::Press, 1)} |
| 2915 |
<< QList{1}; |
| 2916 |
QTest::addRow("Multi:Press and click with drag enabled") << QAbstractItemView::MultiSelection << true |
| 2917 |
<< QAbstractItemView::NoEditTriggers |
| 2918 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 2919 |
SelectionEvent(SelectionEvent::Click, 1)} |
| 2920 |
<< QList<int>{}; |
| 2921 |
QTest::addRow("Multi:Press,Press") << QAbstractItemView::MultiSelection << false |
| 2922 |
<< QAbstractItemView::NoEditTriggers |
| 2923 |
<< QList{SelectionEvent(SelectionEvent::Press, 2), |
| 2924 |
SelectionEvent(SelectionEvent::Press, 3)} |
| 2925 |
<< QList{2, 3}; |
| 2926 |
QTest::addRow("Multi:Press,Drag") << QAbstractItemView::MultiSelection << false |
| 2927 |
<< QAbstractItemView::NoEditTriggers |
| 2928 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 2929 |
SelectionEvent(SelectionEvent::Move, 5), |
| 2930 |
SelectionEvent(SelectionEvent::Release)} |
| 2931 |
<< QList{1, 2, 3, 4, 5}; |
| 2932 |
QTest::addRow("Multi:Press,Drag,Deselect") << QAbstractItemView::MultiSelection << false |
| 2933 |
<< QAbstractItemView::NoEditTriggers |
| 2934 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 2935 |
SelectionEvent(SelectionEvent::Move, 5), |
| 2936 |
SelectionEvent(SelectionEvent::Release), |
| 2937 |
SelectionEvent(SelectionEvent::Press, 3)} |
| 2938 |
<< QList{1, 2, 4, 5}; |
| 2939 |
// drag-select a few indices; then drag-select a larger area that includes the first |
| 2940 |
QTest::addRow("Multi:Press,Drag;Surround") << QAbstractItemView::MultiSelection << false |
| 2941 |
<< QAbstractItemView::NoEditTriggers |
| 2942 |
<< QList{SelectionEvent(SelectionEvent::Press, 3), |
| 2943 |
SelectionEvent(SelectionEvent::Move, 5), |
| 2944 |
SelectionEvent(SelectionEvent::Release), |
| 2945 |
SelectionEvent(SelectionEvent::Press, 1), |
| 2946 |
SelectionEvent(SelectionEvent::Move, 8), |
| 2947 |
SelectionEvent(SelectionEvent::Release)} |
| 2948 |
<< QList{1, 2, 3, 4, 5, 6, 7, 8}; |
| 2949 |
// drag-select a few indices; then try to select more starting with the last -> not working |
| 2950 |
QTest::addRow("Multi:Press,Drag;Expand") << QAbstractItemView::MultiSelection << false |
| 2951 |
<< QAbstractItemView::NoEditTriggers |
| 2952 |
<< QList{SelectionEvent(SelectionEvent::Press, 3), |
| 2953 |
SelectionEvent(SelectionEvent::Move, 5), |
| 2954 |
SelectionEvent(SelectionEvent::Release), |
| 2955 |
SelectionEvent(SelectionEvent::Press, 5), // this will deselect #5 and not select 6/7/8 |
| 2956 |
SelectionEvent(SelectionEvent::Move, 8), |
| 2957 |
SelectionEvent(SelectionEvent::Release)} |
| 2958 |
<< QList{3, 4}; |
| 2959 |
// Multi: Press-dragging a selection should not deselect #QTBUG-59888 |
| 2960 |
QTest::addRow("Multi:Press-Drag selection") << QAbstractItemView::MultiSelection << true |
| 2961 |
// with drag'n'drop enabled, we cannot drag a selection |
| 2962 |
<< QAbstractItemView::NoEditTriggers |
| 2963 |
<< QList{SelectionEvent(SelectionEvent::Click, 2), |
| 2964 |
SelectionEvent(SelectionEvent::Click, 3), |
| 2965 |
SelectionEvent(SelectionEvent::Click, 4), |
| 2966 |
SelectionEvent(SelectionEvent::Click, 5), |
| 2967 |
SelectionEvent(SelectionEvent::Press, 3), |
| 2968 |
// two moves needed because of distance and state logic in QAbstractItemView |
| 2969 |
SelectionEvent(SelectionEvent::Move, 5), |
| 2970 |
SelectionEvent(SelectionEvent::Move, 6)} |
| 2971 |
<< QList{2, 3, 4, 5}; |
| 2972 |
|
| 2973 |
// Extended selection: Press selects a single item |
| 2974 |
QTest::addRow("Extended:Press") << QAbstractItemView::ExtendedSelection << false |
| 2975 |
<< QAbstractItemView::NoEditTriggers |
| 2976 |
<< QList{SelectionEvent(SelectionEvent::Press, 3)} |
| 2977 |
<< QList{3}; |
| 2978 |
QTest::addRow("Extended:Press twice") << QAbstractItemView::ExtendedSelection << false |
| 2979 |
<< QAbstractItemView::NoEditTriggers |
| 2980 |
<< QList{SelectionEvent(SelectionEvent::Press, 3), |
| 2981 |
SelectionEvent(SelectionEvent::Press, 3)} |
| 2982 |
<< QList{3}; |
| 2983 |
QTest::addRow("Extended:Press,Press") << QAbstractItemView::ExtendedSelection << false |
| 2984 |
<< QAbstractItemView::NoEditTriggers |
| 2985 |
<< QList{SelectionEvent(SelectionEvent::Press, 2), |
| 2986 |
SelectionEvent(SelectionEvent::Press, 3)} |
| 2987 |
<< QList{3}; |
| 2988 |
// Extended selection: press with Ctrl toggles item |
| 2989 |
QTest::addRow("Extended:Press,Toggle") << QAbstractItemView::ExtendedSelection << false |
| 2990 |
<< QAbstractItemView::NoEditTriggers |
| 2991 |
<< QList{SelectionEvent(SelectionEvent::Press, 3), |
| 2992 |
SelectionEvent(SelectionEvent::Click, Qt::ControlModifier, 3)} |
| 2993 |
<< QList<int>{}; |
| 2994 |
QTest::addRow("Extended:Press,Add") << QAbstractItemView::ExtendedSelection << false |
| 2995 |
<< QAbstractItemView::NoEditTriggers |
| 2996 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 2997 |
SelectionEvent(SelectionEvent::Click, Qt::ControlModifier, 3)} |
| 2998 |
<< QList{1, 3}; |
| 2999 |
// Extended selection: Shift creates a range between first and last pressed |
| 3000 |
QTest::addRow("Extended:Press,Range") << QAbstractItemView::ExtendedSelection << false |
| 3001 |
<< QAbstractItemView::NoEditTriggers |
| 3002 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 3003 |
SelectionEvent(SelectionEvent::Press, Qt::ShiftModifier, 5)} |
| 3004 |
<< QList{1, 2, 3, 4, 5}; |
| 3005 |
QTest::addRow("Extended:Press,Range,Fix Range") << QAbstractItemView::ExtendedSelection << false |
| 3006 |
<< QAbstractItemView::NoEditTriggers |
| 3007 |
<< QList{SelectionEvent(SelectionEvent::Press, 1), |
| 3008 |
SelectionEvent(SelectionEvent::Press, Qt::ShiftModifier, 5), |
| 3009 |
SelectionEvent(SelectionEvent::Press, Qt::ShiftModifier, 3)} |
| 3010 |
<< QList{1, 2, 3}; |
| 3011 |
// Extended: dragging extends the selection |
| 3012 |
QTest::addRow("Extended:Press,Drag") << QAbstractItemView::ExtendedSelection << false |
| 3013 |
<< QAbstractItemView::NoEditTriggers |
| 3014 |
<< QList{SelectionEvent(SelectionEvent::Press, 2), |
| 3015 |
SelectionEvent(SelectionEvent::Move, 5)} |
| 3016 |
<< QList{2, 3, 4, 5}; |
| 3017 |
// Extended: Ctrl+Press-dragging extends the selection |
| 3018 |
QTest::addRow("Extended:Press,Drag;Ctrl-Press,Drag") << QAbstractItemView::ExtendedSelection << false |
| 3019 |
<< QAbstractItemView::NoEditTriggers |
| 3020 |
<< QList{SelectionEvent(SelectionEvent::Press, 2), |
| 3021 |
SelectionEvent(SelectionEvent::Move, 5), |
| 3022 |
SelectionEvent(SelectionEvent::Release), |
| 3023 |
SelectionEvent(SelectionEvent::Press, Qt::ControlModifier, 6), |
| 3024 |
SelectionEvent(SelectionEvent::Move, Qt::ControlModifier, 8), |
| 3025 |
SelectionEvent(SelectionEvent::Release, Qt::ControlModifier, 8)} |
| 3026 |
<< QList{2, 3, 4, 5, 6, 7, 8}; |
| 3027 |
// Extended: Ctrl+Press-dragging in a selection should not deselect #QTBUG-59888 |
| 3028 |
QTest::addRow("Extended:Ctrl-Drag selection,no deselect") << QAbstractItemView::ExtendedSelection << true |
| 3029 |
<< QAbstractItemView::NoEditTriggers |
| 3030 |
<< QList{SelectionEvent(SelectionEvent::Click, 2), |
| 3031 |
SelectionEvent(SelectionEvent::Click, Qt::ShiftModifier, 5), |
| 3032 |
SelectionEvent(SelectionEvent::Press, Qt::ControlModifier, 3), |
| 3033 |
SelectionEvent(SelectionEvent::Move, Qt::ControlModifier, 5), |
| 3034 |
// two moves needed because of distance and state logic in QAbstractItemView |
| 3035 |
SelectionEvent(SelectionEvent::Move, Qt::ControlModifier, 6)} |
| 3036 |
<< QList{2, 3, 4, 5}; |
| 3037 |
// Extended: Ctrl+Press-dragging with a selection extends, then drags #QTBUG-59888 |
| 3038 |
QTest::addRow("Extended:Ctrl-Drag selection") << QAbstractItemView::ExtendedSelection << true |
| 3039 |
<< QAbstractItemView::NoEditTriggers |
| 3040 |
<< QList{SelectionEvent(SelectionEvent::Click, 2), |
| 3041 |
SelectionEvent(SelectionEvent::Click, Qt::ShiftModifier, 5), |
| 3042 |
SelectionEvent(SelectionEvent::Press, Qt::ControlModifier, 6), |
| 3043 |
SelectionEvent(SelectionEvent::Move, Qt::ControlModifier, 7), |
| 3044 |
// two moves needed because of distance and state logic in 7QAbstractItemView |
| 3045 |
SelectionEvent(SelectionEvent::Move, Qt::ControlModifier, 8)} |
| 3046 |
<< QList{2, 3, 4, 5, 6}; |
| 3047 |
// Extended: when drag is enabled, click with Ctrl toggles item instead of editing # QTBUG-111131 |
| 3048 |
QTest::addRow("Extended:Click,Toggle,editable") << QAbstractItemView::ExtendedSelection << false |
| 3049 |
<< QAbstractItemView::SelectedClicked |
| 3050 |
<< QList{SelectionEvent(SelectionEvent::Click, 3), |
| 3051 |
SelectionEvent(SelectionEvent::Click, Qt::ControlModifier, 3)} |
| 3052 |
<< QList<int>{}; |
| 3053 |
QTest::addRow("Extended:Click,Toggle,dragable,editable") << QAbstractItemView::ExtendedSelection << true |
| 3054 |
<< QAbstractItemView::SelectedClicked |
| 3055 |
<< QList{SelectionEvent(SelectionEvent::Click, 3), |
| 3056 |
SelectionEvent(SelectionEvent::Click, Qt::ControlModifier, 3)} |
| 3057 |
<< QList<int>{}; |
| 3058 |
// Extended: when drag is enabled, click on selected without Ctrl clears before editing |
| 3059 |
QTest::addRow("Extended:Range,Click,editable") << QAbstractItemView::ExtendedSelection << false |
| 3060 |
<< QAbstractItemView::SelectedClicked |
| 3061 |
<< QList{SelectionEvent(SelectionEvent::Click, 1), |
| 3062 |
SelectionEvent(SelectionEvent::Click, Qt::ShiftModifier, 3), |
| 3063 |
SelectionEvent(SelectionEvent::Click, 2)} |
| 3064 |
<< QList<int>{2}; |
| 3065 |
QTest::addRow("Extended:Range,Click,dragable,editable") << QAbstractItemView::ExtendedSelection << true |
| 3066 |
<< QAbstractItemView::SelectedClicked |
| 3067 |
<< QList{SelectionEvent(SelectionEvent::Click, 1), |
| 3068 |
SelectionEvent(SelectionEvent::Click, Qt::ShiftModifier, 3), |
| 3069 |
SelectionEvent(SelectionEvent::Click, 2)} |
| 3070 |
<< QList<int>{2}; |
| 3071 |
} |
| 3072 |
|
| 3073 |
void tst_QAbstractItemView::mouseSelection() |
| 3074 |
{ |
| 3075 |
QFETCH(QAbstractItemView::SelectionMode, selectionMode); |
| 3076 |
QFETCH(bool, dragEnabled); |
| 3077 |
QFETCH(QAbstractItemView::EditTrigger, editTrigger); |
| 3078 |
QFETCH(QList<SelectionEvent>, selectionEvents); |
| 3079 |
QFETCH(QList<int>, selectedRows); |
| 3080 |
|
| 3081 |
QStandardItemModel model; |
| 3082 |
QStandardItem *parentItem = model.invisibleRootItem(); |
| 3083 |
for (int i = 0; i < 10; ++i) { |
| 3084 |
QStandardItem *item = new QStandardItem(QString("item %0").arg(i)); |
| 3085 |
item->setDragEnabled(dragEnabled); |
| 3086 |
item->setEditable(editTrigger != QAbstractItemView::NoEditTriggers); |
| 3087 |
parentItem->appendRow(item); |
| 3088 |
} |
| 3089 |
|
| 3090 |
std::unique_ptr<DragRecorder> dragRecorder(new DragRecorderView<QTreeView>); |
| 3091 |
QAbstractItemView *view = dragRecorder->view; |
| 3092 |
QVERIFY(view); |
| 3093 |
view->setModel(&model); |
| 3094 |
view->setDragEnabled(dragEnabled); |
| 3095 |
view->setSelectionMode(selectionMode); |
| 3096 |
view->setEditTriggers(editTrigger); |
| 3097 |
view->show(); |
| 3098 |
QVERIFY(QTest::qWaitForWindowActive(view)); |
| 3099 |
|
| 3100 |
Qt::MouseButton buttonDown = Qt::NoButton; |
| 3101 |
int targetRow = -1; |
| 3102 |
QModelIndex pressedIndex; |
| 3103 |
for (const auto &event : std::as_const(selectionEvents)) { |
| 3104 |
if (event.row != -1) |
| 3105 |
targetRow = event.row; |
| 3106 |
const QModelIndex targetIndex = model.index(targetRow, 0); |
| 3107 |
const QPoint targetPoint = view->visualRect(targetIndex).center(); |
| 3108 |
switch (event.eventType) { |
| 3109 |
case SelectionEvent::Press: |
| 3110 |
if (buttonDown != Qt::NoButton) { |
| 3111 |
QTest::mouseRelease(view->viewport(), buttonDown, event.keyboardModifiers, |
| 3112 |
view->visualRect(pressedIndex).center()); |
| 3113 |
} |
| 3114 |
buttonDown = Qt::LeftButton; |
| 3115 |
pressedIndex = model.index(targetRow, 0); |
| 3116 |
QTest::mousePress(view->viewport(), buttonDown, event.keyboardModifiers, targetPoint); |
| 3117 |
break; |
| 3118 |
case SelectionEvent::Release: |
| 3119 |
QTest::mouseRelease(view->viewport(), buttonDown, event.keyboardModifiers, targetPoint); |
| 3120 |
buttonDown = Qt::NoButton; |
| 3121 |
pressedIndex = QModelIndex(); |
| 3122 |
break; |
| 3123 |
case SelectionEvent::Click: |
| 3124 |
QTest::mouseClick(view->viewport(), Qt::LeftButton, event.keyboardModifiers, targetPoint); |
| 3125 |
buttonDown = Qt::NoButton; |
| 3126 |
break; |
| 3127 |
case SelectionEvent::Move: { |
| 3128 |
QMouseEvent mouseMoveEvent(QEvent::MouseMove, targetPoint, |
| 3129 |
view->viewport()->mapToGlobal(targetPoint), |
| 3130 |
Qt::NoButton, buttonDown, event.keyboardModifiers); |
| 3131 |
QApplication::sendEvent(view->viewport(), &mouseMoveEvent); |
| 3132 |
} |
| 3133 |
break; |
| 3134 |
} |
| 3135 |
} |
| 3136 |
|
| 3137 |
QList<int> actualSelected; |
| 3138 |
const auto selectedIndexes = dragRecorder->dragStarted |
| 3139 |
? dragRecorder->draggedIndexes |
| 3140 |
: view->selectionModel()->selectedIndexes(); |
| 3141 |
for (auto index : selectedIndexes) |
| 3142 |
actualSelected << index.row(); |
| 3143 |
|
| 3144 |
QCOMPARE(actualSelected, selectedRows); |
| 3145 |
} |
| 3146 |
|
| 3147 |
/*! |
| 3148 |
Make sure that when clicking on empty space in the view, we don't |
| 3149 |
unselect the current row. |
| 3150 |
QTBUG-105870 |
| 3151 |
*/ |
| 3152 |
void tst_QAbstractItemView::keepSingleSelectionOnEmptyAreaClick() |
| 3153 |
{ |
| 3154 |
QListWidget view; |
| 3155 |
view.setSelectionMode(QAbstractItemView::SingleSelection); |
| 3156 |
QListWidgetItem *lastItem; |
| 3157 |
for (int i = 0; i < 5; i++) |
| 3158 |
lastItem = new QListWidgetItem("item " + QString::number(i), &view); |
| 3159 |
|
| 3160 |
// Make widget large enough so that there is empty area below the last item |
| 3161 |
view.setFixedSize(300, 500); |
| 3162 |
view.show(); |
| 3163 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 3164 |
|
| 3165 |
// Select third row |
| 3166 |
view.setCurrentRow(2); |
| 3167 |
|
| 3168 |
// Click below the last row |
| 3169 |
QPoint targetPoint = view.visualItemRect(lastItem).bottomLeft(); |
| 3170 |
targetPoint += QPoint(10, 10); |
| 3171 |
|
| 3172 |
QTest::mouseClick(view.viewport(), Qt::MouseButton::LeftButton, Qt::NoModifier, targetPoint); |
| 3173 |
|
| 3174 |
QCOMPARE(view.currentRow(), 2); |
| 3175 |
QVERIFY(view.currentItem()->isSelected()); |
| 3176 |
} |
| 3177 |
|
| 3178 |
/*! |
| 3179 |
Verify that scrolling an autoScroll enabled itemview with a QScroller |
| 3180 |
produces a continuous, smooth scroll without any jumping around due to |
| 3181 |
the currentItem negotiation between QAbstractItemView and QScroller. |
| 3182 |
QTBUG-64543. |
| 3183 |
*/ |
| 3184 |
void tst_QAbstractItemView::scrollerSmoothScroll() |
| 3185 |
{ |
| 3186 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 3187 |
QSKIP("QWindow::requestActivate() is not supported."); |
| 3188 |
|
| 3189 |
QListWidget view; |
| 3190 |
view.setAutoScroll(true); |
| 3191 |
view.setVerticalScrollMode(QListView::ScrollPerPixel); |
| 3192 |
|
| 3193 |
QScroller::grabGesture(view.viewport(), QScroller::TouchGesture); |
| 3194 |
QScroller::grabGesture(view.viewport(), QScroller::LeftMouseButtonGesture); |
| 3195 |
|
| 3196 |
for (int i = 0; i < 50; i++) { |
| 3197 |
QListWidgetItem* item = new QListWidgetItem("item " + QString::number(i), &view); |
| 3198 |
// gives items a touch friendly size so that only a few fit into the viewport |
| 3199 |
item->setSizeHint(QSize(100,50)); |
| 3200 |
} |
| 3201 |
|
| 3202 |
// make sure we have space for only a few items |
| 3203 |
view.setFixedSize(120, 200); |
| 3204 |
view.show(); |
| 3205 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 3206 |
|
| 3207 |
// we flick up, so we should never scroll back |
| 3208 |
int lastScrollPosition = 0; |
| 3209 |
bool scrollBack = false; |
| 3210 |
connect(view.verticalScrollBar(), &QScrollBar::valueChanged, [&](int value){ |
| 3211 |
scrollBack |= (value < lastScrollPosition); |
| 3212 |
lastScrollPosition = value; |
| 3213 |
}); |
| 3214 |
|
| 3215 |
// start in the middle |
| 3216 |
view.scrollToItem(view.item(25)); |
| 3217 |
QCOMPARE(view.currentItem(), view.item(0)); |
| 3218 |
QListWidgetItem *pressItem = view.item(23); |
| 3219 |
QPoint dragPosition = view.visualRect(view.indexFromItem(pressItem)).center(); |
| 3220 |
// the mouse press changes the current item temporarily, but the press is delayed |
| 3221 |
// by the gesture machinery. this is not what we are testing here, so skip the test |
| 3222 |
// if this fails within a reasonable amount of time. |
| 3223 |
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, dragPosition); |
| 3224 |
if (!(QTest::qWaitFor([&]{ return view.currentItem() == pressItem; }))) |
| 3225 |
QSKIP("Current item didn't change on press, skipping test"); |
| 3226 |
|
| 3227 |
// QAIV will reset the current item when the scroller changes state to Dragging |
| 3228 |
for (int y = 0; y < QApplication::startDragDistance() * 2; ++y) { |
| 3229 |
// gesture recognizer needs some throttling |
| 3230 |
QTest::qWait(10); |
| 3231 |
dragPosition -= QPoint(0, 10); |
| 3232 |
const QPoint globalPos = view.viewport()->mapToGlobal(dragPosition); |
| 3233 |
QMouseEvent mouseMoveEvent(QEvent::MouseMove, dragPosition, dragPosition, globalPos, |
| 3234 |
Qt::NoButton, Qt::LeftButton, Qt::NoModifier); |
| 3235 |
QApplication::sendEvent(view.viewport(), &mouseMoveEvent); |
| 3236 |
QVERIFY(!scrollBack); |
| 3237 |
} |
| 3238 |
|
| 3239 |
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, dragPosition); |
| 3240 |
} |
| 3241 |
|
| 3242 |
/*! |
| 3243 |
Verify that starting the editing of an item with a key press while a composing |
| 3244 |
input method is active doesn't break the input method. See QTBUG-54848. |
| 3245 |
*/ |
| 3246 |
void tst_QAbstractItemView::inputMethodOpensEditor_data() |
| 3247 |
{ |
| 3248 |
QTest::addColumn<QPoint>("editItem"); |
| 3249 |
QTest::addColumn<QString>("preedit"); |
| 3250 |
QTest::addColumn<QString>("commit"); |
| 3251 |
|
| 3252 |
QTest::addRow("IM accepted") << QPoint(1, 1) << "chang" << QString("长"); |
| 3253 |
QTest::addRow("IM cancelled") << QPoint(25, 25) << "chang" << QString(); |
| 3254 |
} |
| 3255 |
|
| 3256 |
void tst_QAbstractItemView::inputMethodOpensEditor() |
| 3257 |
{ |
| 3258 |
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) |
| 3259 |
QSKIP("QWindow::requestActivate() is not supported."); |
| 3260 |
|
| 3261 |
QTableWidget tableWidget(50, 50); |
| 3262 |
tableWidget.setEditTriggers(QAbstractItemView::AnyKeyPressed); |
| 3263 |
for (int r = 0; r < 50; ++r) { |
| 3264 |
for (int c = 0; c < 50; ++c ) |
| 3265 |
tableWidget.setItem(r, c, new QTableWidgetItem(QString("Item %1:%2").arg(r).arg(c))); |
| 3266 |
} |
| 3267 |
|
| 3268 |
tableWidget.show(); |
| 3269 |
QVERIFY(QTest::qWaitForWindowActive(&tableWidget)); |
| 3270 |
|
| 3271 |
const auto sendInputMethodEvent = [](const QString &preeditText, const QString &commitString = {}){ |
| 3272 |
QInputMethodEvent imEvent(preeditText, {}); |
| 3273 |
imEvent.setCommitString(commitString); |
| 3274 |
QApplication::sendEvent(QApplication::focusWidget(), &imEvent); |
| 3275 |
}; |
| 3276 |
|
| 3277 |
QCOMPARE(QApplication::focusWidget(), &tableWidget); |
| 3278 |
|
| 3279 |
QFETCH(QPoint, editItem); |
| 3280 |
QFETCH(QString, preedit); |
| 3281 |
QFETCH(QString, commit); |
| 3282 |
|
| 3283 |
tableWidget.setCurrentCell(editItem.y(), editItem.x()); |
| 3284 |
const QString orgText = tableWidget.currentItem()->text(); |
| 3285 |
const QModelIndex currentIndex = tableWidget.currentIndex(); |
| 3286 |
QCOMPARE(tableWidget.inputMethodQuery(Qt::ImCursorRectangle), tableWidget.visualRect(currentIndex)); |
| 3287 |
|
| 3288 |
// simulate the start of input via a composing input method |
| 3289 |
sendInputMethodEvent(preedit.left(1)); |
| 3290 |
QCOMPARE(tableWidget.state(), QAbstractItemView::EditingState); |
| 3291 |
QLineEdit *editor = tableWidget.findChild<QLineEdit*>(); |
| 3292 |
QVERIFY(editor); |
| 3293 |
QCOMPARE(editor->text(), QString()); |
| 3294 |
// the focus must remain with the tableWidget, as otherwise the compositing is interrupted |
| 3295 |
QCOMPARE(QApplication::focusWidget(), &tableWidget); |
| 3296 |
// the item view delegates input method queries to the editor |
| 3297 |
const QRect cursorRect = tableWidget.inputMethodQuery(Qt::ImCursorRectangle).toRect(); |
| 3298 |
QVERIFY(cursorRect.isValid()); |
| 3299 |
QVERIFY(tableWidget.visualRect(currentIndex).intersects(cursorRect)); |
| 3300 |
|
| 3301 |
// finish preediting, then commit or cancel the input |
| 3302 |
sendInputMethodEvent(preedit); |
| 3303 |
sendInputMethodEvent(QString(), commit); |
| 3304 |
// editing continues, the editor now has focus |
| 3305 |
QCOMPARE(tableWidget.state(), QAbstractItemView::EditingState); |
| 3306 |
QVERIFY(editor->hasFocus()); |
| 3307 |
// finish editing |
| 3308 |
QTest::keyClick(editor, Qt::Key_Return); |
| 3309 |
if (commit.isEmpty()) { |
| 3310 |
// if composition was cancelled, then the item's value is unchanged |
| 3311 |
QCOMPARE(tableWidget.currentItem()->text(), orgText); |
| 3312 |
} else { |
| 3313 |
// otherwise, the item's value is now the commit string |
| 3314 |
QTRY_COMPARE(tableWidget.currentItem()->text(), commit); |
| 3315 |
} |
| 3316 |
} |
| 3317 |
|
| 3318 |
void tst_QAbstractItemView::selectionAutoScrolling_data() |
| 3319 |
{ |
| 3320 |
QTest::addColumn<Qt::Orientation>("orientation"); |
| 3321 |
QTest::addColumn<int>("direction"); // negative or positive |
| 3322 |
|
| 3323 |
QTest::addRow("scroll up") << Qt::Vertical << -1; |
| 3324 |
QTest::addRow("scroll left") << Qt::Horizontal << -1; |
| 3325 |
QTest::addRow("scroll down") << Qt::Vertical << +1; |
| 3326 |
QTest::addRow("scroll right") << Qt::Horizontal << +1; |
| 3327 |
} |
| 3328 |
|
| 3329 |
void tst_QAbstractItemView::selectionAutoScrolling() |
| 3330 |
{ |
| 3331 |
QFETCH(Qt::Orientation, orientation); |
| 3332 |
QFETCH(int, direction); |
| 3333 |
|
| 3334 |
QListView listview; |
| 3335 |
listview.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 3336 |
listview.setResizeMode(QListView::Fixed); |
| 3337 |
listview.setAutoScroll(true); |
| 3338 |
listview.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 3339 |
listview.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 3340 |
listview.setSpacing(10); |
| 3341 |
listview.setGeometry(0, 0, 500, 500); |
| 3342 |
listview.setFrameShape(QFrame::Shape::NoFrame); |
| 3343 |
listview.setEditTriggers(QListView::NoEditTriggers); |
| 3344 |
|
| 3345 |
QStandardItemModel *listModel = new QStandardItemModel(&listview); |
| 3346 |
listview.setModel(listModel); |
| 3347 |
listview.setViewMode(QListView::IconMode); |
| 3348 |
listview.setSelectionMode(QListView::ExtendedSelection); |
| 3349 |
|
| 3350 |
QPixmap pm(50, 50); |
| 3351 |
pm.fill(Qt::red); |
| 3352 |
for (int i = 0; i < 80; i++) { |
| 3353 |
QStandardItem *item = new QStandardItem(pm, QString::number(i)); |
| 3354 |
listModel->appendRow(item); |
| 3355 |
} |
| 3356 |
|
| 3357 |
listview.show(); |
| 3358 |
QVERIFY(QTest::qWaitForWindowExposed(&listview)); |
| 3359 |
|
| 3360 |
listview.resize(200, 200); |
| 3361 |
// scroll to the middle |
| 3362 |
listview.verticalScrollBar()->setValue(listview.verticalScrollBar()->maximum() / 2); |
| 3363 |
listview.horizontalScrollBar()->setValue(listview.horizontalScrollBar()->maximum() / 2); |
| 3364 |
|
| 3365 |
// remove all visible items so that we don't select any items at the edges, as that |
| 3366 |
// would scroll the view already |
| 3367 |
for (int x = 0; x < listview.viewport()->width(); x += 5) { |
| 3368 |
for (int y = 0; y < listview.viewport()->height(); y += 5) { |
| 3369 |
const QModelIndex index = listview.indexAt(QPoint(x, y)); |
| 3370 |
if (index.isValid()) |
| 3371 |
delete listModel->itemFromIndex(index); |
| 3372 |
} |
| 3373 |
} |
| 3374 |
// remove all items around the edges of the model |
| 3375 |
QRect topLeftRect = listview.visualRect(listModel->index(0, 0)); |
| 3376 |
const QPoint topLeftCenter(topLeftRect.center()); |
| 3377 |
QPoint bottomRightCenter; |
| 3378 |
for (int x = 0; x < listview.horizontalScrollBar()->maximum() + listview.viewport()->width(); x += 5) { |
| 3379 |
const QModelIndex index = listview.indexAt(topLeftCenter + QPoint(x, 0)); |
| 3380 |
if (index.isValid()) { |
| 3381 |
delete listModel->itemFromIndex(index); |
| 3382 |
bottomRightCenter.rx() = x; |
| 3383 |
} |
| 3384 |
} |
| 3385 |
for (int y = 0; y < listview.verticalScrollBar()->maximum() + listview.viewport()->height(); y += 5) { |
| 3386 |
const QModelIndex index = listview.indexAt(topLeftCenter + QPoint(0, y)); |
| 3387 |
if (index.isValid()) { |
| 3388 |
delete listModel->itemFromIndex(index); |
| 3389 |
bottomRightCenter.ry() = y; |
| 3390 |
} |
| 3391 |
} |
| 3392 |
for (int x = 0; x < bottomRightCenter.x(); x += 5) { |
| 3393 |
const QModelIndex index = listview.indexAt(topLeftCenter + QPoint(x, bottomRightCenter.y())); |
| 3394 |
if (index.isValid()) |
| 3395 |
delete listModel->itemFromIndex(index); |
| 3396 |
} |
| 3397 |
for (int y = 0; y < bottomRightCenter.y(); y += 5) { |
| 3398 |
const QModelIndex index = listview.indexAt(topLeftCenter + QPoint(bottomRightCenter.x(), y)); |
| 3399 |
if (index.isValid()) |
| 3400 |
delete listModel->itemFromIndex(index); |
| 3401 |
} |
| 3402 |
|
| 3403 |
|
| 3404 |
// Simulate multiple select behavior; start in the middle, drag to the edge |
| 3405 |
const QPoint pressPoint(listview.viewport()->width() / 2, listview.viewport()->height() / 2); |
| 3406 |
QPoint dragPoint = pressPoint; |
| 3407 |
if (orientation == Qt::Vertical) { |
| 3408 |
dragPoint.rx() += 50; |
| 3409 |
dragPoint.ry() = direction > 0 ? listview.viewport()->height() : 0; |
| 3410 |
} else { |
| 3411 |
dragPoint.rx() = direction > 0 ? listview.viewport()->width() : 0; |
| 3412 |
dragPoint.ry() += 50; |
| 3413 |
} |
| 3414 |
|
| 3415 |
QTest::mousePress(listview.viewport(), Qt::LeftButton, Qt::NoModifier, pressPoint); |
| 3416 |
QMouseEvent mmEvent(QEvent::MouseMove, dragPoint, listview.viewport()->mapToGlobal(dragPoint), |
| 3417 |
Qt::NoButton, Qt::LeftButton, Qt::NoModifier); |
| 3418 |
QApplication::sendEvent(listview.viewport(), &mmEvent); // QTest::mouseMove is useless |
| 3419 |
|
| 3420 |
// check that we scrolled to the end |
| 3421 |
QScrollBar *scrollBar = orientation == Qt::Vertical |
| 3422 |
? listview.verticalScrollBar() |
| 3423 |
: listview.horizontalScrollBar(); |
| 3424 |
|
| 3425 |
if (direction < 0) |
| 3426 |
QTRY_COMPARE(scrollBar->value(), 0); |
| 3427 |
else |
| 3428 |
QTRY_COMPARE(scrollBar->value(), scrollBar->maximum()); |
| 3429 |
QVERIFY(listview.selectionModel()->selectedIndexes().size() > 0); |
| 3430 |
|
| 3431 |
QTest::mouseRelease(listview.viewport(), Qt::LeftButton, Qt::NoModifier, dragPoint); |
| 3432 |
} |
| 3433 |
class SpinBoxDelegate : public QStyledItemDelegate |
| 3434 |
{ |
| 3435 |
public: |
| 3436 |
using QStyledItemDelegate::QStyledItemDelegate; |
| 3437 |
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override |
| 3438 |
{ |
| 3439 |
QSpinBox *spinboxEditor = new QSpinBox(parent); |
| 3440 |
return spinboxEditor; |
| 3441 |
} |
| 3442 |
|
| 3443 |
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override |
| 3444 |
{ |
| 3445 |
if (QSpinBox *spin = qobject_cast<QSpinBox *>(editor)) { |
| 3446 |
model->setData(index, spin->value()); |
| 3447 |
} |
| 3448 |
} |
| 3449 |
}; |
| 3450 |
|
| 3451 |
void tst_QAbstractItemView::testSpinBoxAsEditor_data() |
| 3452 |
{ |
| 3453 |
QTest::addColumn<bool>("keyboardTracking"); |
| 3454 |
QTest::newRow("true") << true; |
| 3455 |
QTest::newRow("false")<< false; |
| 3456 |
} |
| 3457 |
|
| 3458 |
void tst_QAbstractItemView::testSpinBoxAsEditor() |
| 3459 |
{ |
| 3460 |
QFETCH(bool, keyboardTracking); |
| 3461 |
|
| 3462 |
QStandardItemModel model(2, 2); |
| 3463 |
SpinBoxDelegate delegate; |
| 3464 |
|
| 3465 |
QTableView view; |
| 3466 |
view.setModel(&model); |
| 3467 |
view.setItemDelegate(&delegate); |
| 3468 |
|
| 3469 |
view.setCurrentIndex(model.index(0, 1)); |
| 3470 |
view.openPersistentEditor(model.index(0, 1)); |
| 3471 |
const QList<QSpinBox *> list = view.viewport()->findChildren<QSpinBox *>(); |
| 3472 |
QCOMPARE(list.size(), 1); |
| 3473 |
QSpinBox *sb = list.first(); |
| 3474 |
QVERIFY(sb); |
| 3475 |
|
| 3476 |
sb->setKeyboardTracking(keyboardTracking); |
| 3477 |
|
| 3478 |
centerOnScreen(&view); |
| 3479 |
view.show(); |
| 3480 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 3481 |
QTRY_COMPARE(QApplication::focusWidget(), sb); |
| 3482 |
|
| 3483 |
QTest::keyClick(sb, Qt::Key_1, Qt::NoModifier); |
| 3484 |
QPoint clickpos = view.visualRect(model.index(0, 0)).center(); |
| 3485 |
QTest::mouseDClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, clickpos); |
| 3486 |
|
| 3487 |
QCOMPARE(model.data(model.index(0, 1)).toInt(), 1); |
| 3488 |
} |
| 3489 |
|
| 3490 |
void tst_QAbstractItemView::removeIndexWhileEditing() |
| 3491 |
{ |
| 3492 |
QTreeView view; |
| 3493 |
QStandardItemModel treeModel; |
| 3494 |
auto editableItem1 = new QStandardItem("aa"); |
| 3495 |
auto editableItem2 = new QStandardItem("ab"); |
| 3496 |
auto editableItem3 = new QStandardItem("ac"); |
| 3497 |
auto item = new QStandardItem("a"); |
| 3498 |
item->appendRow(editableItem1); |
| 3499 |
item->appendRow(editableItem2); |
| 3500 |
item->appendRow(editableItem3); |
| 3501 |
treeModel.setItem(0, 0, item); |
| 3502 |
QSortFilterProxyModel filterModel; |
| 3503 |
filterModel.setSourceModel(&treeModel); |
| 3504 |
view.setModel(&filterModel); |
| 3505 |
view.show(); |
| 3506 |
|
| 3507 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 3508 |
|
| 3509 |
view.setExpanded(item->index(), true); |
| 3510 |
|
| 3511 |
filterModel.setFilterRegularExpression("a.*"); |
| 3512 |
|
| 3513 |
QTest::failOnWarning(QRegularExpression("QAbstractItemView::closeEditor called with an editor " |
| 3514 |
"that does not belong to this view")); |
| 3515 |
|
| 3516 |
// Verify that we shut editing down cleanly if the index we are editing is |
| 3517 |
// filtered out after committing |
| 3518 |
{ |
| 3519 |
const QModelIndex filteredIndex = filterModel.mapFromSource(editableItem1->index()); |
| 3520 |
QVERIFY(filteredIndex.isValid()); |
| 3521 |
view.edit(filteredIndex); |
| 3522 |
QCOMPARE(view.state(), QAbstractItemView::EditingState); |
| 3523 |
QTRY_VERIFY(QApplication::focusWidget()); |
| 3524 |
QPointer<QLineEdit> lineEdit = qobject_cast<QLineEdit *>(QApplication::focusWidget()); |
| 3525 |
QVERIFY(lineEdit); |
| 3526 |
lineEdit->setText("c"); |
| 3527 |
QTest::keyClick(lineEdit, Qt::Key_Enter); |
| 3528 |
QTRY_VERIFY(!lineEdit); |
| 3529 |
QCOMPARE(editableItem1->data(Qt::DisplayRole), "c"); |
| 3530 |
QCOMPARE(view.state(), QAbstractItemView::NoState); |
| 3531 |
} |
| 3532 |
|
| 3533 |
// If we change the filter while we edit, then we should clean up state as well |
| 3534 |
{ |
| 3535 |
const QModelIndex filteredIndex = filterModel.mapFromSource(editableItem2->index()); |
| 3536 |
QVERIFY(filteredIndex.isValid()); |
| 3537 |
view.edit(filteredIndex); |
| 3538 |
QCOMPARE(view.state(), QAbstractItemView::EditingState); |
| 3539 |
QTRY_VERIFY(QApplication::focusWidget()); |
| 3540 |
QPointer<QLineEdit> lineEdit = qobject_cast<QLineEdit *>(QApplication::focusWidget()); |
| 3541 |
QVERIFY(lineEdit); |
| 3542 |
filterModel.setFilterFixedString("c"); |
| 3543 |
QVERIFY(!filterModel.mapFromSource(editableItem2->index()).isValid()); |
| 3544 |
QTRY_VERIFY(!lineEdit); |
| 3545 |
QCOMPARE(view.state(), QAbstractItemView::NoState); |
| 3546 |
} |
| 3547 |
} |
| 3548 |
|
| 3549 |
void tst_QAbstractItemView::focusNextOnHide() |
| 3550 |
{ |
| 3551 |
QWidget widget; |
| 3552 |
QTableWidget table(10, 10); |
| 3553 |
table.setTabKeyNavigation(true); |
| 3554 |
QLineEdit lineEdit; |
| 3555 |
|
| 3556 |
QHBoxLayout layout; |
| 3557 |
layout.addWidget(&table); |
| 3558 |
layout.addWidget(&lineEdit); |
| 3559 |
widget.setLayout(&layout); |
| 3560 |
|
| 3561 |
widget.setTabOrder({&table, &lineEdit}); |
| 3562 |
|
| 3563 |
widget.show(); |
| 3564 |
QVERIFY(QTest::qWaitForWindowExposed(&widget)); |
| 3565 |
|
| 3566 |
QTRY_VERIFY(table.hasFocus()); |
| 3567 |
QCOMPARE(table.currentIndex(), table.model()->index(0, 0)); |
| 3568 |
QTest::keyPress(&table, Qt::Key_Tab); |
| 3569 |
QCOMPARE(table.currentIndex(), table.model()->index(0, 1)); |
| 3570 |
|
| 3571 |
table.hide(); |
| 3572 |
QCOMPARE(table.currentIndex(), table.model()->index(0, 1)); |
| 3573 |
QVERIFY(lineEdit.hasFocus()); |
| 3574 |
} |
| 3575 |
|
| 3576 |
// Tests for QTBUG-127381, where shift-selecting after calling |
| 3577 |
// QItemSelectionModel::setCurrentIndex() would produce unexpected results. |
| 3578 |
void tst_QAbstractItemView::shiftSelectionAfterModelSetCurrentIndex() |
| 3579 |
{ |
| 3580 |
QStandardItemModel model(5, 1); |
| 3581 |
QTreeView view; |
| 3582 |
view.setSelectionBehavior(QAbstractItemView::SelectRows); |
| 3583 |
view.setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 3584 |
view.setModel(&model); |
| 3585 |
|
| 3586 |
view.show(); |
| 3587 |
QVERIFY(QTest::qWaitForWindowExposed(&view)); |
| 3588 |
|
| 3589 |
QModelIndex index = model.index(0, 0); |
| 3590 |
view.setCurrentIndex(index); |
| 3591 |
index = model.index(2, 0); |
| 3592 |
view.selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); |
| 3593 |
|
| 3594 |
QTest::keyPress(&view, Qt::Key_Down, Qt::ShiftModifier); |
| 3595 |
|
| 3596 |
QItemSelection selection = view.selectionModel()->selection(); |
| 3597 |
QCOMPARE(selection.size(), 1); |
| 3598 |
QCOMPARE(selection.first().top(), 2); |
| 3599 |
QCOMPARE(selection.first().bottom(), 3); |
| 3600 |
|
| 3601 |
QTest::keyPress(&view, Qt::Key_Up, Qt::ShiftModifier); |
| 3602 |
|
| 3603 |
selection = view.selectionModel()->selection(); |
| 3604 |
QCOMPARE(selection.first().top(), 2); |
| 3605 |
QCOMPARE(selection.first().bottom(), 2); |
| 3606 |
} |
| 3607 |
|
| 3608 |
void tst_QAbstractItemView::QTBUG72333_isPersistentEditorOpen() |
| 3609 |
{ |
| 3610 |
QStandardItemModel model(1, 1); |
| 3611 |
model.setData(model.index(0, 0), QStringLiteral("Test")); |
| 3612 |
|
| 3613 |
QTableView view; |
| 3614 |
view.setModel(&model); |
| 3615 |
view.show(); |
| 3616 |
|
| 3617 |
const QModelIndex index = model.index(0, 0); |
| 3618 |
|
| 3619 |
view.edit(index); |
| 3620 |
QVERIFY(view.state() == QAbstractItemView::EditingState); |
| 3621 |
QVERIFY(!view.isPersistentEditorOpen(index)); |
| 3622 |
|
| 3623 |
view.closeEditor(view.indexWidget(index), QAbstractItemDelegate::RevertModelCache); |
| 3624 |
|
| 3625 |
view.openPersistentEditor(index); |
| 3626 |
QVERIFY(view.isPersistentEditorOpen(index)); |
| 3627 |
|
| 3628 |
view.closePersistentEditor(index); |
| 3629 |
QVERIFY(!view.isPersistentEditorOpen(index)); |
| 3630 |
} |
| 3631 |
|
| 3632 |
QTEST_MAIN(tst_QAbstractItemView) |
| 3633 |
#include "tst_qabstractitemview.moc" |