1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtGui/QtGui>
44 #include <QtWidgets/QtWidgets>
45 #include <private/qtablewidget_p.h>
46 #include <QtTest/QtTest>
47 #include "private/qapplication_p.h"
49 #ifdef QT_BUILD_INTERNAL
50 #define VERIFY_SPANS_CONSISTENCY(TEST_VIEW_) \
51 QVERIFY(static_cast<QTableViewPrivate*>(QObjectPrivate::get(TEST_VIEW_))->spans.checkConsistency())
53 #define VERIFY_SPANS_CONSISTENCY(TEST_VIEW_) (void)false
56 typedef QList<int> IntList;
57 Q_DECLARE_METATYPE(IntList)
59 typedef QList<bool> BoolList;
60 Q_DECLARE_METATYPE(BoolList)
62 class tst_QTableView : public QObject
68 virtual ~tst_QTableView();
72 void cleanupTestCase();
83 void removeRows_data();
86 void removeColumns_data();
89 void keyboardNavigation_data();
90 void keyboardNavigation();
92 void headerSections_data();
93 void headerSections();
95 void moveCursor_data();
98 void moveCursorStrikesBack_data();
99 void moveCursorStrikesBack();
101 void hideRows_data();
104 void hideColumns_data();
107 void selection_data();
110 void selectRow_data();
113 void selectColumn_data();
116 void visualRect_data();
122 void resizeRowsToContents_data();
123 void resizeRowsToContents();
125 void resizeColumnsToContents_data();
126 void resizeColumnsToContents();
128 void rowViewportPosition_data();
129 void rowViewportPosition();
134 void rowHeight_data();
137 void columnViewportPosition_data();
138 void columnViewportPosition();
140 void columnAt_data();
143 void columnWidth_data();
146 void hiddenRow_data();
149 void hiddenColumn_data();
152 void sortingEnabled_data();
153 void sortingEnabled();
155 void scrollTo_data();
165 void spansAfterRowInsertion();
166 void spansAfterColumnInsertion();
167 void spansAfterRowRemoval();
168 void spansAfterColumnRemoval();
170 void checkHeaderReset();
171 void checkHeaderMinSize();
173 void resizeToContents();
177 void selectionSignal();
178 void setCurrentIndex();
180 // task-specific tests:
181 void task173773_updateVerticalHeader();
182 void task227953_setRootIndex();
183 void task240266_veryBigColumn();
184 void task248688_autoScrollNavigation();
185 void task259308_scrollVerticalHeaderSwappedSections();
186 void task191545_dragSelectRows();
187 void taskQTBUG_5062_spansInconsistency();
188 void taskQTBUG_4516_clickOnRichTextLabel();
189 void taskQTBUG_5237_wheelEventOnHeader();
190 void taskQTBUG_8585_crashForNoGoodReason();
191 void taskQTBUG_7774_RtoLVisualRegionForSelection();
192 void taskQTBUG_8777_scrollToSpans();
193 void taskQTBUG_10169_sizeHintForRow();
195 void mouseWheel_data();
198 void addColumnWhileEditing();
199 void task234926_setHeaderSorting();
201 void changeHeaderData();
204 // Testing get/set functions
205 void tst_QTableView::getSetCheck()
209 obj1.setSortingEnabled(false);
210 QCOMPARE(false, obj1.isSortingEnabled());
211 obj1.setSortingEnabled(true);
212 QCOMPARE(true, obj1.isSortingEnabled());
214 obj1.setShowGrid(false);
215 QCOMPARE(false, obj1.showGrid());
216 obj1.setShowGrid(true);
217 QCOMPARE(true, obj1.showGrid());
219 obj1.setGridStyle(Qt::NoPen);
220 QCOMPARE(Qt::NoPen, obj1.gridStyle());
221 obj1.setGridStyle(Qt::SolidLine);
222 QCOMPARE(Qt::SolidLine, obj1.gridStyle());
224 obj1.setRootIndex(QModelIndex());
225 QCOMPARE(QModelIndex(), obj1.rootIndex());
226 QStandardItemModel model(10, 10);
227 obj1.setModel(&model);
228 QModelIndex index = model.index(0, 0);
229 obj1.setRootIndex(index);
230 QCOMPARE(index, obj1.rootIndex());
232 QHeaderView *var1 = new QHeaderView(Qt::Horizontal);
233 obj1.setHorizontalHeader(var1);
234 QCOMPARE(var1, obj1.horizontalHeader());
235 obj1.setHorizontalHeader((QHeaderView *)0);
236 QCOMPARE(var1, obj1.horizontalHeader());
239 QHeaderView *var2 = new QHeaderView(Qt::Vertical);
240 obj1.setVerticalHeader(var2);
241 QCOMPARE(var2, obj1.verticalHeader());
242 obj1.setVerticalHeader((QHeaderView *)0);
243 QCOMPARE(var2, obj1.verticalHeader());
246 QCOMPARE(obj1.isCornerButtonEnabled(), true);
247 obj1.setCornerButtonEnabled(false);
248 QCOMPARE(obj1.isCornerButtonEnabled(), false);
251 class QtTestTableModel: public QAbstractTableModel
256 void invalidIndexEncountered() const;
259 bool submit() { ++submit_count; return QAbstractTableModel::submit(); }
262 QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
263 : QAbstractTableModel(parent),
265 column_count(columns),
267 can_fetch_more(false),
270 disabled_columns() {}
272 int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
273 int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
274 bool isEditable(const QModelIndex &) const { return true; }
276 Qt::ItemFlags flags(const QModelIndex &index) const
278 Qt::ItemFlags index_flags = QAbstractTableModel::flags(index);
279 if (disabled_rows.contains(index.row())
280 || disabled_columns.contains(index.column()))
281 index_flags &= ~Qt::ItemIsEnabled;
285 void disableRow(int row)
287 disabled_rows.insert(row);
290 void enableRow(int row)
292 disabled_rows.remove(row);
295 void disableColumn(int column)
297 disabled_columns.insert(column);
300 void enableColumn(int column)
302 disabled_columns.remove(column);
305 QVariant data(const QModelIndex &idx, int role) const
307 if (!idx.isValid() || idx.row() >= row_count || idx.column() >= column_count) {
308 qWarning() << "Invalid modelIndex [%d,%d,%p]" << idx;
309 emit invalidIndexEncountered();
313 if (role == Qt::DisplayRole || role == Qt::EditRole)
314 return QString("[%1,%2,%3]").arg(idx.row()).arg(idx.column()).arg(0);
319 bool insertRows(int start, int count, const QModelIndex &parent = QModelIndex())
321 if (start < 0 || start > row_count)
324 beginInsertRows(parent, start, start + count - 1);
330 bool removeRows(int start, int count, const QModelIndex &parent = QModelIndex())
332 if (start < 0 || start >= row_count || row_count < count)
335 beginRemoveRows(parent, start, start + count - 1);
343 beginRemoveRows(QModelIndex(), row_count - 1, row_count - 1);
350 beginRemoveRows(QModelIndex(), 0, row_count - 1);
355 bool insertColumns(int start, int count, const QModelIndex &parent = QModelIndex())
357 if (start < 0 || start > column_count)
360 beginInsertColumns(parent, start, start + count - 1);
361 column_count += count;
366 bool removeColumns(int start, int count, const QModelIndex &parent = QModelIndex())
368 if (start < 0 || start >= column_count || column_count < count)
371 beginRemoveColumns(parent, start, start + count - 1);
372 column_count -= count;
377 void removeLastColumn()
379 beginRemoveColumns(QModelIndex(), column_count - 1, column_count - 1);
384 void removeAllColumns()
386 beginRemoveColumns(QModelIndex(), 0, column_count - 1);
391 bool canFetchMore(const QModelIndex &) const
393 return can_fetch_more;
396 void fetchMore(const QModelIndex &)
403 QAbstractTableModel::reset();
410 int fetch_more_count;
411 QSet<int> disabled_rows;
412 QSet<int> disabled_columns;
415 class QtTestTableView : public QTableView
420 QtTestTableView(QWidget *parent = 0) : QTableView(parent), checkSignalOrder(false), hasCurrentChanged(0), hasSelectionChanged(0) {}
422 void setModel(QAbstractItemModel *model)
424 QTableView::setModel(model);
425 connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
426 this, SLOT(currentChanged(QModelIndex,QModelIndex)));
427 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
428 this, SLOT(itemSelectionChanged(QItemSelection,QItemSelection)));
432 MoveUp = QAbstractItemView::MoveUp,
433 MoveDown = QAbstractItemView::MoveDown,
434 MoveLeft = QAbstractItemView::MoveLeft,
435 MoveRight = QAbstractItemView::MoveRight,
436 MoveHome = QAbstractItemView::MoveHome,
437 MoveEnd = QAbstractItemView::MoveEnd,
438 MovePageUp = QAbstractItemView::MovePageUp,
439 MovePageDown = QAbstractItemView::MovePageDown,
440 MoveNext = QAbstractItemView::MoveNext,
441 MovePrevious = QAbstractItemView::MovePrevious
444 QModelIndex moveCursor(QtTestTableView::CursorAction cursorAction,
445 Qt::KeyboardModifiers modifiers)
447 return QTableView::moveCursor((QAbstractItemView::CursorAction)cursorAction, modifiers);
450 int columnWidthHint(int column) const
452 return sizeHintForColumn(column);
455 int rowHeightHint(int row) const
457 return sizeHintForRow(row);
460 bool isIndexHidden(const QModelIndex &index) const
462 return QTableView::isIndexHidden(index);
465 void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
467 QTableView::setSelection(rect, command);
470 QModelIndexList selectedIndexes() const
472 return QTableView::selectedIndexes();
475 int sizeHintForRow(int row) const
477 return QTableView::sizeHintForRow(row);
480 bool checkSignalOrder;
482 void currentChanged(QModelIndex , QModelIndex ) {
484 if (checkSignalOrder)
485 QVERIFY(hasCurrentChanged > hasSelectionChanged);
488 void itemSelectionChanged(QItemSelection , QItemSelection ) {
489 hasSelectionChanged++;
490 if (checkSignalOrder)
491 QVERIFY(hasCurrentChanged >= hasSelectionChanged);
494 int hasCurrentChanged;
495 int hasSelectionChanged;
499 class QtTestItemDelegate : public QItemDelegate
502 QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const
510 tst_QTableView::tst_QTableView()
514 tst_QTableView::~tst_QTableView()
518 void tst_QTableView::initTestCase()
520 #ifdef Q_OS_WINCE //disable magic for WindowsCE
521 qApp->setAutoMaximizeThreshold(-1);
525 void tst_QTableView::cleanupTestCase()
529 void tst_QTableView::init()
533 void tst_QTableView::cleanup()
537 void tst_QTableView::noDelegate()
539 QtTestTableModel model(3, 3);
541 view.setModel(&model);
542 view.setItemDelegate(0);
546 void tst_QTableView::noModel()
552 void tst_QTableView::emptyModel()
554 QtTestTableModel model;
556 QSignalSpy spy(&model, SIGNAL(invalidIndexEncountered()));
557 view.setModel(&model);
559 QCOMPARE(spy.count(), 0);
562 void tst_QTableView::removeRows_data()
564 QTest::addColumn<int>("rowCount");
565 QTest::addColumn<int>("columnCount");
567 QTest::newRow("2x2") << 2 << 2;
568 QTest::newRow("10x10") << 10 << 10;
571 void tst_QTableView::removeRows()
573 QFETCH(int, rowCount);
574 QFETCH(int, columnCount);
576 QtTestTableModel model(rowCount, columnCount);
577 QSignalSpy spy(&model, SIGNAL(invalidIndexEncountered()));
580 view.setModel(&model);
583 model.removeLastRow();
584 QCOMPARE(spy.count(), 0);
586 model.removeAllRows();
587 QCOMPARE(spy.count(), 0);
590 void tst_QTableView::removeColumns_data()
592 QTest::addColumn<int>("rowCount");
593 QTest::addColumn<int>("columnCount");
595 QTest::newRow("2x2") << 2 << 2;
596 QTest::newRow("10x10") << 10 << 10;
599 void tst_QTableView::removeColumns()
601 QFETCH(int, rowCount);
602 QFETCH(int, columnCount);
604 QtTestTableModel model(rowCount, columnCount);
605 QSignalSpy spy(&model, SIGNAL(invalidIndexEncountered()));
608 view.setModel(&model);
611 model.removeLastColumn();
612 QCOMPARE(spy.count(), 0);
614 model.removeAllColumns();
615 QCOMPARE(spy.count(), 0);
618 void tst_QTableView::keyboardNavigation_data()
620 QTest::addColumn<int>("rowCount");
621 QTest::addColumn<int>("columnCount");
622 QTest::addColumn<bool>("tabKeyNavigation");
623 QTest::addColumn<IntList>("keyPresses");
625 QTest::newRow("16x16 model") << 16 << 16 << true
651 QTest::newRow("no tab") << 8 << 8 << false
677 void tst_QTableView::keyboardNavigation()
679 QFETCH(int, rowCount);
680 QFETCH(int, columnCount);
681 QFETCH(bool, tabKeyNavigation);
682 QFETCH(IntList, keyPresses);
684 QtTestTableModel model(rowCount, columnCount);
686 view.setModel(&model);
688 view.setTabKeyNavigation(tabKeyNavigation);
689 QModelIndex index = model.index(rowCount - 1, columnCount - 1);
690 view.setCurrentIndex(index);
693 qApp->setActiveWindow(&view);
694 QVERIFY(QTest::qWaitForWindowActive(&view));
696 int row = rowCount - 1;
697 int column = columnCount - 1;
698 for (int i = 0; i < keyPresses.count(); ++i) {
700 Qt::Key key = (Qt::Key)keyPresses.at(i);
704 row = qMax(0, row - 1);
707 row = qMin(rowCount - 1, row + 1);
709 case Qt::Key_Backtab:
710 if (!tabKeyNavigation)
713 column = qMax(0, column - 1);
716 if (!tabKeyNavigation)
719 column = qMin(columnCount - 1, column + 1);
725 QTest::keyClick(&view, key);
726 QApplication::processEvents();
728 QModelIndex index = model.index(row, column);
729 QCOMPARE(view.currentIndex(), index);
733 void tst_QTableView::headerSections_data()
735 QTest::addColumn<int>("rowCount");
736 QTest::addColumn<int>("columnCount");
737 QTest::addColumn<int>("row");
738 QTest::addColumn<int>("column");
739 QTest::addColumn<int>("rowHeight");
740 QTest::addColumn<int>("columnWidth");
742 QTest::newRow("") << 10 << 10 << 5 << 5 << 30 << 30;
745 void tst_QTableView::headerSections()
747 QFETCH(int, rowCount);
748 QFETCH(int, columnCount);
751 QFETCH(int, rowHeight);
752 QFETCH(int, columnWidth);
754 QtTestTableModel model(rowCount, columnCount);
757 QHeaderView *hheader = view.horizontalHeader();
758 QHeaderView *vheader = view.verticalHeader();
760 view.setModel(&model);
763 hheader->doItemsLayout();
764 vheader->doItemsLayout();
766 QCOMPARE(hheader->count(), model.columnCount());
767 QCOMPARE(vheader->count(), model.rowCount());
769 view.setRowHeight(row, rowHeight);
770 QCOMPARE(view.rowHeight(row), rowHeight);
772 QCOMPARE(view.rowHeight(row), 0);
774 QCOMPARE(view.rowHeight(row), rowHeight);
776 view.setColumnWidth(column, columnWidth);
777 QCOMPARE(view.columnWidth(column), columnWidth);
778 view.hideColumn(column);
779 QCOMPARE(view.columnWidth(column), 0);
780 view.showColumn(column);
781 QCOMPARE(view.columnWidth(column), columnWidth);
784 typedef QPair<int,int> IntPair;
785 Q_DECLARE_METATYPE(IntPair)
787 void tst_QTableView::moveCursor_data()
789 QTest::addColumn<int>("rowCount");
790 QTest::addColumn<int>("columnCount");
791 QTest::addColumn<int>("hideRow");
792 QTest::addColumn<int>("hideColumn");
794 QTest::addColumn<int>("startRow");
795 QTest::addColumn<int>("startColumn");
797 QTest::addColumn<int>("cursorMoveAction");
798 QTest::addColumn<int>("modifier");
800 QTest::addColumn<int>("expectedRow");
801 QTest::addColumn<int>("expectedColumn");
802 QTest::addColumn<IntPair>("moveRow");
803 QTest::addColumn<IntPair>("moveColumn");
806 QTest::newRow("MoveRight (0,0)")
807 << 4 << 4 << -1 << -1
809 << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
810 << 0 << 1 << IntPair(0,0) << IntPair(0,0);
812 QTest::newRow("MoveRight (3,0)")
813 << 4 << 4 << -1 << -1
815 << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
816 << 3 << 1 << IntPair(0,0) << IntPair(0,0);
818 QTest::newRow("MoveRight (3,3)")
819 << 4 << 4 << -1 << -1
821 << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
822 << 3 << 3 << IntPair(0,0) << IntPair(0,0); // ###
824 QTest::newRow("MoveRight, hidden column 1 (0,0)")
827 << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
828 << 0 << 2 << IntPair(0,0) << IntPair(0,0);
830 QTest::newRow("MoveRight, hidden column 3 (0,2)")
833 << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
834 << 0 << 2 << IntPair(0,0) << IntPair(0,0); // ###
836 // MoveNext should in addition wrap
837 QTest::newRow("MoveNext (0,0)")
838 << 4 << 4 << -1 << -1
840 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
841 << 0 << 1 << IntPair(0,0) << IntPair(0,0);
843 QTest::newRow("MoveNext (0,2)")
844 << 4 << 4 << -1 << -1
846 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
847 << 0 << 3 << IntPair(0,0) << IntPair(0,0);
849 QTest::newRow("MoveNext, wrap (0,3)")
850 << 4 << 4 << -1 << -1
852 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
853 << 1 << 0 << IntPair(0,0) << IntPair(0,0);
855 QTest::newRow("MoveNext, wrap (3,3)")
856 << 4 << 4 << -1 << -1
858 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
859 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
861 QTest::newRow("MoveNext, hidden column 1 (0,0)")
864 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
865 << 0 << 2 << IntPair(0,0) << IntPair(0,0);
867 QTest::newRow("MoveNext, wrap, hidden column 3 (0,2)")
870 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
871 << 1 << 0 << IntPair(0,0) << IntPair(0,0);
873 QTest::newRow("MoveNext, wrap, hidden column 3 (3,2)")
876 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
877 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
879 QTest::newRow("MoveNext, wrapy, wrapx, hidden column 3, hidden row 3 (2,2)")
882 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
883 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
885 QTest::newRow("MoveNext, wrap, hidden column 2, moved column from 3 to 0. (0,2)")
888 << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
889 << 1 << 3 << IntPair(0,0) << IntPair(3,0);
892 QTest::newRow("MoveLeft (0,0)")
893 << 4 << 4 << -1 << -1
895 << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
896 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
898 QTest::newRow("MoveLeft (0,3)")
899 << 4 << 4 << -1 << -1
901 << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
902 << 0 << 2 << IntPair(0,0) << IntPair(0,0);
904 QTest::newRow("MoveLeft (1,0)")
905 << 4 << 4 << -1 << -1
907 << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
908 << 1 << 0 << IntPair(0,0) << IntPair(0,0);
910 QTest::newRow("MoveLeft, hidden column 0 (0,2)")
913 << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
914 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
916 QTest::newRow("MoveLeft, hidden column 0 (0,1)")
919 << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
920 << 0 << 1 << IntPair(0,0) << IntPair(0,0);
922 // MovePrevious should in addition wrap
923 QTest::newRow("MovePrevious (0,3)")
924 << 4 << 4 << -1 << -1
926 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
927 << 0 << 2 << IntPair(0,0) << IntPair(0,0);
929 QTest::newRow("MovePrevious (0,1)")
930 << 4 << 4 << -1 << -1
932 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
933 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
935 QTest::newRow("MovePrevious, wrap (1,0)")
936 << 4 << 4 << -1 << -1
938 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
939 << 0 << 3 << IntPair(0,0) << IntPair(0,0);
941 QTest::newRow("MovePrevious, wrap, (0,0)")
942 << 4 << 4 << -1 << -1
944 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
945 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
947 QTest::newRow("MovePrevious, hidden column 1 (0,2)")
950 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
951 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
953 QTest::newRow("MovePrevious, wrap, hidden column 3 (0,2)")
956 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
957 << 0 << 1 << IntPair(0,0) << IntPair(0,0);
959 QTest::newRow("MovePrevious, wrapy, hidden column 0 (0,1)")
962 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
963 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
965 QTest::newRow("MovePrevious, wrap, hidden column 0, hidden row 0 (1,1)")
968 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
969 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
971 QTest::newRow("MovePrevious, wrap, hidden column 1, moved column from 0 to 3. (1,2)")
974 << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
975 << 0 << 0 << IntPair(0,0) << IntPair(0,3);
978 QTest::newRow("MoveDown (0,0)")
979 << 4 << 4 << -1 << -1
981 << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
982 << 1 << 0 << IntPair(0,0) << IntPair(0,0);
984 QTest::newRow("MoveDown (3,0)")
985 << 4 << 4 << -1 << -1
987 << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
988 << 3 << 0 << IntPair(0,0) << IntPair(0,0);
990 QTest::newRow("MoveDown (3,3)")
991 << 4 << 4 << -1 << -1
993 << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
994 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
996 QTest::newRow("MoveDown, hidden row 1 (0,0)")
999 << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
1000 << 2 << 0 << IntPair(0,0) << IntPair(0,0);
1002 QTest::newRow("MoveDown, hidden row 3 (2,0)")
1003 << 4 << 4 << 3 << -1
1005 << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
1006 << 2 << 0 << IntPair(0,0) << IntPair(0,0);
1008 QTest::newRow("MoveDown, hidden row 0 hidden column 0 (0,0)")
1011 << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
1012 << 1 << 1 << IntPair(0,0) << IntPair(0,0);
1015 QTest::newRow("MoveUp (0,0)")
1016 << 4 << 4 << -1 << -1
1018 << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
1019 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
1021 QTest::newRow("MoveUp (3, 0)")
1022 << 4 << 4 << -1 << -1
1024 << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
1025 << 2 << 0 << IntPair(0,0) << IntPair(0,0);
1027 QTest::newRow("MoveUp (0,1)")
1028 << 4 << 4 << -1 << -1
1030 << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
1031 << 0 << 1 << IntPair(0,0) << IntPair(0,0);
1033 QTest::newRow("MoveUp, hidden row 1 (2,0)")
1034 << 4 << 4 << 1 << -1
1036 << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
1037 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
1039 QTest::newRow("MoveUp, hidden row (1,0)")
1040 << 4 << 4 << 0 << -1
1042 << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
1043 << 1 << 0 << IntPair(0,0) << IntPair(0,0);
1046 QTest::newRow("MoveHome (0,0)")
1047 << 4 << 4 << -1 << -1
1049 << int(QtTestTableView::MoveHome) << int(Qt::NoModifier)
1050 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
1052 QTest::newRow("MoveHome (3,3)")
1053 << 4 << 4 << -1 << -1
1055 << int(QtTestTableView::MoveHome) << int(Qt::NoModifier)
1056 << 3 << 0 << IntPair(0,0) << IntPair(0,0);
1058 QTest::newRow("MoveHome, hidden column 0 (3,3)")
1059 << 4 << 4 << -1 << 0
1061 << int(QtTestTableView::MoveHome) << int(Qt::NoModifier)
1062 << 3 << 1 << IntPair(0,0) << IntPair(0,0);
1064 // Use Ctrl modifier
1065 QTest::newRow("MoveHome + Ctrl (0,0)")
1066 << 4 << 4 << -1 << -1
1068 << int(QtTestTableView::MoveHome) << int(Qt::ControlModifier)
1069 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
1071 QTest::newRow("MoveHome + Ctrl (3,3)")
1072 << 4 << 4 << -1 << -1
1074 << int(QtTestTableView::MoveHome) << int(Qt::ControlModifier)
1075 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
1077 QTest::newRow("MoveHome + Ctrl, hidden column 0, hidden row 0 (3,3)")
1080 << int(QtTestTableView::MoveHome) << int(Qt::ControlModifier)
1081 << 1 << 1 << IntPair(0,0) << IntPair(0,0);
1084 QTest::newRow("MoveEnd (0,0)")
1085 << 4 << 4 << -1 << -1
1087 << int(QtTestTableView::MoveEnd) << int(Qt::NoModifier)
1088 << 0 << 3 << IntPair(0,0) << IntPair(0,0);
1090 QTest::newRow("MoveEnd (3,3)")
1091 << 4 << 4 << -1 << -1
1093 << int(QtTestTableView::MoveEnd) << int(Qt::NoModifier)
1094 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
1096 QTest::newRow("MoveEnd, hidden column (0,0)")
1097 << 4 << 4 << -1 << 3
1099 << int(QtTestTableView::MoveEnd) << int(Qt::NoModifier)
1100 << 0<< 2 << IntPair(0,0) << IntPair(0,0);
1102 // Use Ctrl modifier
1103 QTest::newRow("MoveEnd + Ctrl (0,0)")
1104 << 4 << 4 << -1 << -1
1106 << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
1107 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
1109 QTest::newRow("MoveEnd + Ctrl (3,3)")
1110 << 4 << 4 << -1 << -1
1112 << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
1113 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
1115 QTest::newRow("MoveEnd + Ctrl, hidden column 3 (0,0)")
1116 << 4 << 4 << -1 << 3
1118 << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
1119 << 3 << 2 << IntPair(0,0) << IntPair(0,0);
1121 QTest::newRow("MoveEnd + Ctrl, hidden column 3, hidden row 3 (0,0)")
1124 << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
1125 << 2 << 2 << IntPair(0,0) << IntPair(0,0);
1127 QTest::newRow("MovePageUp (0,0)")
1128 << 4 << 4 << -1 << -1
1130 << int(QtTestTableView::MovePageUp) << 0
1131 << 0 << 0 << IntPair(0,0) << IntPair(0,0);
1133 QTest::newRow("MovePageUp (3,3)")
1134 << 4 << 4 << -1 << -1
1136 << int(QtTestTableView::MovePageUp) << 0
1137 << 0 << 3 << IntPair(0,0) << IntPair(0,0);
1139 QTest::newRow("MovePageDown (3, 3)")
1140 << 4 << 4 << -1 << -1
1142 << int(QtTestTableView::MovePageDown) << 0
1143 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
1145 QTest::newRow("MovePageDown (0, 3)")
1146 << 4 << 4 << -1 << -1
1148 << int(QtTestTableView::MovePageDown) << 0
1149 << 3 << 3 << IntPair(0,0) << IntPair(0,0);
1152 void tst_QTableView::moveCursor()
1154 QFETCH(int, rowCount);
1155 QFETCH(int, columnCount);
1156 QFETCH(int, hideRow);
1157 QFETCH(int, hideColumn);
1158 QFETCH(int, startRow);
1159 QFETCH(int, startColumn);
1160 QFETCH(int, cursorMoveAction);
1161 QFETCH(int, modifier);
1162 QFETCH(int, expectedRow);
1163 QFETCH(int, expectedColumn);
1164 QFETCH(IntPair, moveRow);
1165 QFETCH(IntPair, moveColumn);
1167 QtTestTableModel model(rowCount, columnCount);
1168 QtTestTableView view;
1170 view.setModel(&model);
1171 view.hideRow(hideRow);
1172 view.hideColumn(hideColumn);
1173 if (moveColumn.first != moveColumn.second)
1174 view.horizontalHeader()->moveSection(moveColumn.first, moveColumn.second);
1175 if (moveRow.first != moveRow.second)
1176 view.verticalHeader()->moveSection(moveRow.first, moveRow.second);
1180 QModelIndex index = model.index(startRow, startColumn);
1181 view.setCurrentIndex(index);
1183 QModelIndex newIndex = view.moveCursor((QtTestTableView::CursorAction)cursorMoveAction,
1184 (Qt::KeyboardModifiers)modifier);
1185 // expected fails, task 119433
1186 if(newIndex.row() == -1)
1188 QCOMPARE(newIndex.row(), expectedRow);
1189 QCOMPARE(newIndex.column(), expectedColumn);
1192 void tst_QTableView::moveCursorStrikesBack_data()
1194 QTest::addColumn<int>("hideRow");
1195 QTest::addColumn<int>("hideColumn");
1196 QTest::addColumn<IntList>("disableRows");
1197 QTest::addColumn<IntList>("disableColumns");
1198 QTest::addColumn<QRect>("span");
1200 QTest::addColumn<int>("startRow");
1201 QTest::addColumn<int>("startColumn");
1202 QTest::addColumn<IntList>("cursorMoveActions");
1203 QTest::addColumn<int>("expectedRow");
1204 QTest::addColumn<int>("expectedColumn");
1206 QTest::newRow("Last column disabled. Task QTBUG-3878") << -1 << -1
1210 << 0 << 5 << (IntList() << int(QtTestTableView::MoveNext))
1213 QTest::newRow("Last column disabled 2. Task QTBUG-3878") << -1 << -1
1217 << 1 << 0 << (IntList() << int(QtTestTableView::MovePrevious))
1220 QTest::newRow("Span, anchor column hidden") << -1 << 1
1223 << QRect(1, 2, 2, 3)
1224 << 2 << 0 << (IntList() << int(QtTestTableView::MoveNext))
1227 QTest::newRow("Span, anchor column disabled") << -1 << -1
1230 << QRect(1, 2, 2, 3)
1231 << 2 << 0 << (IntList() << int(QtTestTableView::MoveNext))
1234 QTest::newRow("Span, anchor row hidden") << 2 << -1
1237 << QRect(1, 2, 2, 3)
1238 << 1 << 2 << (IntList() << int(QtTestTableView::MoveDown))
1241 QTest::newRow("Span, anchor row disabled") << -1 << -1
1244 << QRect(1, 2, 2, 3)
1245 << 1 << 2 << (IntList() << int(QtTestTableView::MoveDown))
1248 QTest::newRow("Move through span right") << -1 << -1
1251 << QRect(1, 2, 2, 3)
1252 << 3 << 0 << (IntList() << int(QtTestTableView::MoveRight) << int(QtTestTableView::MoveRight))
1255 QTest::newRow("Move through span left") << -1 << -1
1258 << QRect(1, 2, 2, 3)
1259 << 3 << 3 << (IntList() << int(QtTestTableView::MoveLeft) << int(QtTestTableView::MoveLeft))
1262 QTest::newRow("Move through span down") << -1 << -1
1265 << QRect(1, 2, 2, 3)
1266 << 1 << 2 << (IntList() << int(QtTestTableView::MoveDown) << int(QtTestTableView::MoveDown))
1269 QTest::newRow("Move through span up") << -1 << -1
1272 << QRect(1, 2, 2, 3)
1273 << 5 << 2 << (IntList() << int(QtTestTableView::MoveUp) << int(QtTestTableView::MoveUp))
1277 for (int i = 0; i < 7; ++i)
1280 QTest::newRow("All disabled, wrap forward. Timeout => FAIL") << -1 << -1
1284 << 1 << 0 << (IntList() << int(QtTestTableView::MoveNext))
1287 QTest::newRow("All disabled, wrap backwards. Timeout => FAIL") << -1 << -1
1291 << 1 << 0 << (IntList() << int(QtTestTableView::MovePrevious))
1295 void tst_QTableView::moveCursorStrikesBack()
1297 QFETCH(int, hideRow);
1298 QFETCH(int, hideColumn);
1299 QFETCH(IntList, disableRows);
1300 QFETCH(IntList, disableColumns);
1301 QFETCH(QRect, span);
1303 QFETCH(int, startRow);
1304 QFETCH(int, startColumn);
1305 QFETCH(IntList, cursorMoveActions);
1306 QFETCH(int, expectedRow);
1307 QFETCH(int, expectedColumn);
1309 QtTestTableModel model(7, 7);
1310 QtTestTableView view;
1311 view.setModel(&model);
1312 view.hideRow(hideRow);
1313 view.hideColumn(hideColumn);
1315 if (span.height() && span.width())
1316 view.setSpan(span.top(), span.left(), span.height(), span.width());
1319 QModelIndex index = model.index(startRow, startColumn);
1320 view.setCurrentIndex(index);
1322 foreach (int row, disableRows)
1323 model.disableRow(row);
1324 foreach (int column, disableColumns)
1325 model.disableColumn(column);
1329 foreach (int cursorMoveAction, cursorMoveActions) {
1330 QModelIndex newIndex = view.moveCursor((QtTestTableView::CursorAction)cursorMoveAction, 0);
1331 view.setCurrentIndex(newIndex);
1332 newRow = newIndex.row();
1333 newColumn = newIndex.column();
1336 // expected fails, task 119433
1339 QCOMPARE(newRow, expectedRow);
1340 QCOMPARE(newColumn, expectedColumn);
1343 void tst_QTableView::hideRows_data()
1345 QTest::addColumn<int>("rowCount");
1346 QTest::addColumn<int>("columnCount");
1347 QTest::addColumn<int>("showRow"); // hide, then show
1348 QTest::addColumn<int>("hideRow"); // hide only
1349 QTest::addColumn<int>("row");
1350 QTest::addColumn<int>("column");
1351 QTest::addColumn<int>("rowSpan");
1352 QTest::addColumn<int>("columnSpan");
1354 QTest::newRow("show row 0, hide row 3, no span")
1361 QTest::newRow("show row 0, hide row 3, span")
1369 void tst_QTableView::hideRows()
1371 QFETCH(int, rowCount);
1372 QFETCH(int, columnCount);
1373 QFETCH(int, showRow);
1374 QFETCH(int, hideRow);
1376 QFETCH(int, column);
1377 QFETCH(int, rowSpan);
1378 QFETCH(int, columnSpan);
1380 QtTestTableModel model(rowCount, columnCount);
1383 view.setModel(&model);
1384 view.setSpan(row, column, rowSpan, columnSpan);
1386 view.hideRow(showRow);
1387 QVERIFY(view.isRowHidden(showRow));
1389 view.hideRow(hideRow);
1390 QVERIFY(view.isRowHidden(hideRow));
1392 view.showRow(showRow);
1393 QVERIFY(!view.isRowHidden(showRow));
1394 QVERIFY(view.isRowHidden(hideRow));
1397 void tst_QTableView::hideColumns_data()
1399 QTest::addColumn<int>("rowCount");
1400 QTest::addColumn<int>("columnCount");
1401 QTest::addColumn<int>("showColumn"); // hide, then show
1402 QTest::addColumn<int>("hideColumn"); // hide only
1403 QTest::addColumn<int>("row");
1404 QTest::addColumn<int>("column");
1405 QTest::addColumn<int>("rowSpan");
1406 QTest::addColumn<int>("columnSpan");
1408 QTest::newRow("show col 0, hide col 3, no span")
1415 QTest::newRow("show col 0, hide col 3, span")
1423 void tst_QTableView::hideColumns()
1425 QFETCH(int, rowCount);
1426 QFETCH(int, columnCount);
1427 QFETCH(int, showColumn);
1428 QFETCH(int, hideColumn);
1430 QFETCH(int, column);
1431 QFETCH(int, rowSpan);
1432 QFETCH(int, columnSpan);
1434 QtTestTableModel model(rowCount, columnCount);
1437 view.setModel(&model);
1438 view.setSpan(row, column, rowSpan, columnSpan);
1440 view.hideColumn(showColumn);
1441 QVERIFY(view.isColumnHidden(showColumn));
1443 view.hideColumn(hideColumn);
1444 QVERIFY(view.isColumnHidden(hideColumn));
1446 view.showColumn(showColumn);
1447 QVERIFY(!view.isColumnHidden(showColumn));
1448 QVERIFY(view.isColumnHidden(hideColumn));
1451 void tst_QTableView::selection_data()
1453 QTest::addColumn<int>("rowCount");
1454 QTest::addColumn<int>("columnCount");
1455 QTest::addColumn<int>("row");
1456 QTest::addColumn<int>("column");
1457 QTest::addColumn<int>("rowSpan");
1458 QTest::addColumn<int>("columnSpan");
1459 QTest::addColumn<int>("hideRow");
1460 QTest::addColumn<int>("hideColumn");
1461 QTest::addColumn<int>("moveRowFrom");
1462 QTest::addColumn<int>("moveRowTo");
1463 QTest::addColumn<int>("moveColumnFrom");
1464 QTest::addColumn<int>("moveColumnTo");
1465 QTest::addColumn<int>("rowHeight");
1466 QTest::addColumn<int>("columnWidth");
1467 QTest::addColumn<int>("x");
1468 QTest::addColumn<int>("y");
1469 QTest::addColumn<int>("width");
1470 QTest::addColumn<int>("height");
1471 QTest::addColumn<int>("command");
1472 QTest::addColumn<int>("selectedCount"); // ### make this more detailed
1474 QTest::newRow("no span, no hidden, no moved, 3x3 select")
1479 << -1 << -1 // move row
1480 << -1 << -1 // move col
1481 << 40 << 40 // cell size
1482 << 20 << 20 << 80 << 80 // rect
1483 << int(QItemSelectionModel::Select) // command
1484 << 9; // selected count
1486 QTest::newRow("row span, no hidden, no moved, 3x3 select")
1491 << -1 << -1 // move row
1492 << -1 << -1 // move col
1493 << 40 << 40 // cell size
1494 << 20 << 20 << 80 << 80 // rect
1495 << int(QItemSelectionModel::Select) // command
1496 << 8; // selected count
1498 QTest::newRow("col span, no hidden, no moved, 3x3 select")
1503 << -1 << -1 // move row
1504 << -1 << -1 // move col
1505 << 40 << 40 // cell size
1506 << 20 << 20 << 80 << 80 // rect
1507 << int(QItemSelectionModel::Select) // command
1508 << 8; // selected count
1510 QTest::newRow("no span, row hidden, no moved, 3x3 select")
1515 << -1 << -1 // move row
1516 << -1 << -1 // move col
1517 << 40 << 40 // cell size
1518 << 20 << 20 << 80 << 80 // rect
1519 << int(QItemSelectionModel::Select) // command
1520 << 9; // selected count
1522 QTest::newRow("no span, col hidden, no moved, 3x3 select")
1527 << -1 << -1 // move row
1528 << -1 << -1 // move col
1529 << 40 << 40 // cell size
1530 << 20 << 20 << 80 << 80 // rect
1531 << int(QItemSelectionModel::Select) // command
1532 << 9; // selected count
1534 QTest::newRow("no span, no hidden, row moved, 3x3 select")
1539 << 1 << 3 // move row
1540 << -1 << -1 // move col
1541 << 40 << 40 // cell size
1542 << 20 << 20 << 80 << 80 // rect
1543 << int(QItemSelectionModel::Select) // command
1544 << 9; // selected count
1546 QTest::newRow("no span, no hidden, col moved, 3x3 select")
1551 << -1 << -1 // move row
1552 << 1 << 3 // move col
1553 << 40 << 40 // cell size
1554 << 20 << 20 << 80 << 80 // rect
1555 << int(QItemSelectionModel::Select) // command
1556 << 9; // selected count
1559 void tst_QTableView::selection()
1561 QFETCH(int, rowCount);
1562 QFETCH(int, columnCount);
1564 QFETCH(int, column);
1565 QFETCH(int, rowSpan);
1566 QFETCH(int, columnSpan);
1567 QFETCH(int, hideRow);
1568 QFETCH(int, hideColumn);
1569 QFETCH(int, moveRowFrom);
1570 QFETCH(int, moveRowTo);
1571 QFETCH(int, moveColumnFrom);
1572 QFETCH(int, moveColumnTo);
1573 QFETCH(int, rowHeight);
1574 QFETCH(int, columnWidth);
1578 QFETCH(int, height);
1579 QFETCH(int, command);
1580 QFETCH(int, selectedCount);
1582 QtTestTableModel model(rowCount, columnCount);
1584 QtTestTableView view;
1586 view.setModel(&model);
1588 view.setSpan(row, column, rowSpan, columnSpan);
1590 view.hideRow(hideRow);
1591 view.hideColumn(hideColumn);
1593 view.verticalHeader()->moveSection(moveRowFrom, moveRowTo);
1594 view.horizontalHeader()->moveSection(moveColumnFrom, moveColumnTo);
1596 for (int r = 0; r < rowCount; ++r)
1597 view.setRowHeight(r, rowHeight);
1598 for (int c = 0; c < columnCount; ++c)
1599 view.setColumnWidth(c, columnWidth);
1601 view.setSelection(QRect(x, y, width, height),
1602 QItemSelectionModel::SelectionFlags(command));
1604 QCOMPARE(view.selectedIndexes().count(), selectedCount);
1607 void tst_QTableView::selectRow_data()
1609 QTest::addColumn<int>("rowCount");
1610 QTest::addColumn<int>("columnCount");
1611 QTest::addColumn<int>("row");
1612 QTest::addColumn<int>("mode");
1613 QTest::addColumn<int>("behavior");
1614 QTest::addColumn<int>("selectedItems");
1616 QTest::newRow("SingleSelection and SelectItems")
1619 << (int)QAbstractItemView::SingleSelection
1620 << (int)QAbstractItemView::SelectItems
1623 QTest::newRow("SingleSelection and SelectRows")
1626 << (int)QAbstractItemView::SingleSelection
1627 << (int)QAbstractItemView::SelectRows
1630 QTest::newRow("SingleSelection and SelectColumns")
1633 << (int)QAbstractItemView::SingleSelection
1634 << (int)QAbstractItemView::SelectColumns
1637 QTest::newRow("MultiSelection and SelectItems")
1640 << (int)QAbstractItemView::MultiSelection
1641 << (int)QAbstractItemView::SelectItems
1644 QTest::newRow("MultiSelection and SelectRows")
1647 << (int)QAbstractItemView::MultiSelection
1648 << (int)QAbstractItemView::SelectRows
1651 QTest::newRow("MultiSelection and SelectColumns")
1654 << (int)QAbstractItemView::MultiSelection
1655 << (int)QAbstractItemView::SelectColumns
1658 QTest::newRow("ExtendedSelection and SelectItems")
1661 << (int)QAbstractItemView::ExtendedSelection
1662 << (int)QAbstractItemView::SelectItems
1665 QTest::newRow("ExtendedSelection and SelectRows")
1668 << (int)QAbstractItemView::ExtendedSelection
1669 << (int)QAbstractItemView::SelectRows
1672 QTest::newRow("ExtendedSelection and SelectColumns")
1675 << (int)QAbstractItemView::ExtendedSelection
1676 << (int)QAbstractItemView::SelectColumns
1679 QTest::newRow("ContiguousSelection and SelectItems")
1682 << (int)QAbstractItemView::ContiguousSelection
1683 << (int)QAbstractItemView::SelectItems
1686 QTest::newRow("ContiguousSelection and SelectRows")
1689 << (int)QAbstractItemView::ContiguousSelection
1690 << (int)QAbstractItemView::SelectRows
1693 QTest::newRow("ContiguousSelection and SelectColumns")
1696 << (int)QAbstractItemView::ContiguousSelection
1697 << (int)QAbstractItemView::SelectColumns
1701 void tst_QTableView::selectRow()
1703 QFETCH(int, rowCount);
1704 QFETCH(int, columnCount);
1707 QFETCH(int, behavior);
1708 QFETCH(int, selectedItems);
1710 QtTestTableModel model(rowCount, columnCount);
1713 view.setModel(&model);
1714 view.setSelectionMode((QAbstractItemView::SelectionMode)mode);
1715 view.setSelectionBehavior((QAbstractItemView::SelectionBehavior)behavior);
1717 QCOMPARE(view.selectionModel()->selectedIndexes().count(), 0);
1719 view.selectRow(row);
1721 //test we have 10 items selected
1722 QCOMPARE(view.selectionModel()->selectedIndexes().count(), selectedItems);
1723 //test that all 10 items are in the same row
1724 for (int i = 0; selectedItems > 0 && i < rowCount; ++i)
1725 QCOMPARE(view.selectionModel()->selectedIndexes().at(i).row(), row);
1728 void tst_QTableView::selectColumn_data()
1730 QTest::addColumn<int>("rowCount");
1731 QTest::addColumn<int>("columnCount");
1732 QTest::addColumn<int>("column");
1733 QTest::addColumn<int>("mode");
1734 QTest::addColumn<int>("behavior");
1735 QTest::addColumn<int>("selectedItems");
1737 QTest::newRow("SingleSelection and SelectItems")
1740 << (int)QAbstractItemView::SingleSelection
1741 << (int)QAbstractItemView::SelectItems
1744 QTest::newRow("SingleSelection and SelectRows")
1747 << (int)QAbstractItemView::SingleSelection
1748 << (int)QAbstractItemView::SelectRows
1751 QTest::newRow("SingleSelection and SelectColumns")
1754 << (int)QAbstractItemView::SingleSelection
1755 << (int)QAbstractItemView::SelectColumns
1758 QTest::newRow("MultiSelection and SelectItems")
1761 << (int)QAbstractItemView::MultiSelection
1762 << (int)QAbstractItemView::SelectItems
1765 QTest::newRow("MultiSelection and SelectRows")
1768 << (int)QAbstractItemView::MultiSelection
1769 << (int)QAbstractItemView::SelectRows
1772 QTest::newRow("MultiSelection and SelectColumns")
1775 << (int)QAbstractItemView::MultiSelection
1776 << (int)QAbstractItemView::SelectColumns
1779 QTest::newRow("ExtendedSelection and SelectItems")
1782 << (int)QAbstractItemView::ExtendedSelection
1783 << (int)QAbstractItemView::SelectItems
1786 QTest::newRow("ExtendedSelection and SelectRows")
1789 << (int)QAbstractItemView::ExtendedSelection
1790 << (int)QAbstractItemView::SelectRows
1793 QTest::newRow("ExtendedSelection and SelectColumns")
1796 << (int)QAbstractItemView::ExtendedSelection
1797 << (int)QAbstractItemView::SelectColumns
1800 QTest::newRow("ContiguousSelection and SelectItems")
1803 << (int)QAbstractItemView::ContiguousSelection
1804 << (int)QAbstractItemView::SelectItems
1807 QTest::newRow("ContiguousSelection and SelectRows")
1810 << (int)QAbstractItemView::ContiguousSelection
1811 << (int)QAbstractItemView::SelectRows
1814 QTest::newRow("ContiguousSelection and SelectColumns")
1817 << (int)QAbstractItemView::ContiguousSelection
1818 << (int)QAbstractItemView::SelectColumns
1822 void tst_QTableView::selectColumn()
1824 QFETCH(int, rowCount);
1825 QFETCH(int, columnCount);
1826 QFETCH(int, column);
1828 QFETCH(int, behavior);
1829 QFETCH(int, selectedItems);
1831 QtTestTableModel model(rowCount, columnCount);
1834 view.setModel(&model);
1835 view.setSelectionMode((QAbstractItemView::SelectionMode)mode);
1836 view.setSelectionBehavior((QAbstractItemView::SelectionBehavior)behavior);
1838 QCOMPARE(view.selectionModel()->selectedIndexes().count(), 0);
1840 view.selectColumn(column);
1842 QCOMPARE(view.selectionModel()->selectedIndexes().count(), selectedItems);
1843 for (int i = 0; selectedItems > 0 && i < columnCount; ++i)
1844 QCOMPARE(view.selectionModel()->selectedIndexes().at(i).column(), column);
1847 Q_DECLARE_METATYPE(QRect)
1848 void tst_QTableView::visualRect_data()
1850 QTest::addColumn<int>("rowCount");
1851 QTest::addColumn<int>("columnCount");
1852 QTest::addColumn<int>("hideRow");
1853 QTest::addColumn<int>("hideColumn");
1854 QTest::addColumn<int>("row");
1855 QTest::addColumn<int>("column");
1856 QTest::addColumn<int>("rowHeight");
1857 QTest::addColumn<int>("columnWidth");
1858 QTest::addColumn<QRect>("expectedRect");
1860 QTest::newRow("(0,0)")
1865 << QRect(0, 0, 29, 19);
1867 QTest::newRow("(0,0) hidden row")
1874 QTest::newRow("(0,0) hidden column")
1881 QTest::newRow("(0,0) hidden row and column")
1888 QTest::newRow("(0,0) out of bounds")
1895 QTest::newRow("(5,5), hidden row")
1902 QTest::newRow("(9,9)")
1907 << QRect(30*9, 20*9, 29, 19);
1910 void tst_QTableView::visualRect()
1912 QFETCH(int, rowCount);
1913 QFETCH(int, columnCount);
1914 QFETCH(int, hideRow);
1915 QFETCH(int, hideColumn);
1917 QFETCH(int, column);
1918 QFETCH(int, rowHeight);
1919 QFETCH(int, columnWidth);
1920 QFETCH(QRect, expectedRect);
1922 QtTestTableModel model(rowCount, columnCount);
1925 view.setModel(&model);
1926 // Make sure that it has 1 pixel between each cell.
1927 view.setGridStyle(Qt::SolidLine);
1928 for (int i = 0; i < view.verticalHeader()->count(); ++i)
1929 view.verticalHeader()->resizeSection(i, rowHeight);
1930 for (int i = 0; i < view.horizontalHeader()->count(); ++i)
1931 view.horizontalHeader()->resizeSection(i, columnWidth);
1933 view.hideRow(hideRow);
1934 view.hideColumn(hideColumn);
1936 QRect rect = view.visualRect(model.index(row, column));
1937 QCOMPARE(rect, expectedRect);
1940 void tst_QTableView::fetchMore()
1942 QtTestTableModel model(64, 64);
1944 model.can_fetch_more = true;
1947 view.setModel(&model);
1950 QCOMPARE(model.fetch_more_count, 0);
1951 view.verticalScrollBar()->setValue(view.verticalScrollBar()->maximum());
1952 QVERIFY(model.fetch_more_count > 0);
1954 model.fetch_more_count = 0; //reset
1956 QCOMPARE(model.fetch_more_count, 0);
1958 view.scrollToBottom();
1959 QVERIFY(model.fetch_more_count > 0);
1961 model.fetch_more_count = 0; //reset
1963 view.setCurrentIndex(model.index(0, 0));
1964 QCOMPARE(model.fetch_more_count, 0);
1966 for (int i = 0; i < 64; ++i)
1967 QTest::keyClick(&view, Qt::Key_Down);
1968 QCOMPARE(view.currentIndex(), model.index(63, 0));
1969 QVERIFY(model.fetch_more_count > 0);
1972 void tst_QTableView::setHeaders()
1976 // Make sure we don't delete ourselves
1977 view.setVerticalHeader(view.verticalHeader());
1978 view.verticalHeader()->count();
1979 view.setHorizontalHeader(view.horizontalHeader());
1980 view.horizontalHeader()->count();
1982 // Try passing around a header without it being deleted
1984 view2.setVerticalHeader(view.verticalHeader());
1985 view2.setHorizontalHeader(view.horizontalHeader());
1986 view.setHorizontalHeader(new QHeaderView(Qt::Horizontal));
1987 view.setVerticalHeader(new QHeaderView(Qt::Vertical));
1988 view2.verticalHeader()->count();
1989 view2.horizontalHeader()->count();
1993 void tst_QTableView::resizeRowsToContents_data()
1995 QTest::addColumn<int>("rowCount");
1996 QTest::addColumn<int>("columnCount");
1997 QTest::addColumn<bool>("showGrid");
1998 QTest::addColumn<int>("cellWidth");
1999 QTest::addColumn<int>("cellHeight");
2000 QTest::addColumn<int>("rowHeight");
2001 QTest::addColumn<int>("columnWidth");
2003 QTest::newRow("10x10 grid shown 40x40")
2004 << 10 << 10 << false << 40 << 40 << 40 << 40;
2006 QTest::newRow("10x10 grid not shown 40x40")
2007 << 10 << 10 << true << 40 << 40 << 41 << 41;
2010 void tst_QTableView::resizeRowsToContents()
2012 QFETCH(int, rowCount);
2013 QFETCH(int, columnCount);
2014 QFETCH(bool, showGrid);
2015 QFETCH(int, cellWidth);
2016 QFETCH(int, cellHeight);
2017 QFETCH(int, rowHeight);
2018 QFETCH(int, columnWidth);
2019 Q_UNUSED(columnWidth);
2021 QtTestTableModel model(rowCount, columnCount);
2022 QtTestTableView view;
2023 QtTestItemDelegate delegate;
2025 view.setModel(&model);
2026 view.setItemDelegate(&delegate);
2027 view.setShowGrid(showGrid); // the grid will add to the row height
2029 delegate.hint = QSize(cellWidth, cellHeight);
2031 QSignalSpy resizedSpy(view.verticalHeader(), SIGNAL(sectionResized(int, int, int)));
2032 view.resizeRowsToContents();
2034 QCOMPARE(resizedSpy.count(), model.rowCount());
2035 for (int r = 0; r < model.rowCount(); ++r) {
2036 QCOMPARE(view.rowHeight(r), rowHeight);
2040 void tst_QTableView::resizeColumnsToContents_data()
2042 QTest::addColumn<int>("rowCount");
2043 QTest::addColumn<int>("columnCount");
2044 QTest::addColumn<bool>("showGrid");
2045 QTest::addColumn<int>("cellWidth");
2046 QTest::addColumn<int>("cellHeight");
2047 QTest::addColumn<int>("rowHeight");
2048 QTest::addColumn<int>("columnWidth");
2050 QTest::newRow("10x10 grid shown 40x40")
2051 << 10 << 10 << false << 40 << 40 << 40 << 40;
2053 QTest::newRow("10x10 grid not shown 40x40")
2054 << 10 << 10 << true << 40 << 40 << 41 << 41;
2057 void tst_QTableView::resizeColumnsToContents()
2059 QFETCH(int, rowCount);
2060 QFETCH(int, columnCount);
2061 QFETCH(bool, showGrid);
2062 QFETCH(int, cellWidth);
2063 QFETCH(int, cellHeight);
2064 QFETCH(int, rowHeight);
2065 QFETCH(int, columnWidth);
2066 Q_UNUSED(rowHeight);
2068 QtTestTableModel model(rowCount, columnCount);
2069 QtTestTableView view;
2070 QtTestItemDelegate delegate;
2072 view.setModel(&model);
2073 view.setItemDelegate(&delegate);
2074 view.setShowGrid(showGrid); // the grid will add to the row height
2076 delegate.hint = QSize(cellWidth, cellHeight);
2078 QSignalSpy resizedSpy(view.horizontalHeader(), SIGNAL(sectionResized(int, int, int)));
2079 view.resizeColumnsToContents();
2081 QCOMPARE(resizedSpy.count(), model.columnCount());
2082 for (int c = 0; c < model.columnCount(); ++c)
2083 QCOMPARE(view.columnWidth(c), columnWidth);
2086 void tst_QTableView::rowViewportPosition_data()
2088 QTest::addColumn<int>("rowCount");
2089 QTest::addColumn<int>("rowHeight");
2090 QTest::addColumn<int>("row");
2091 QTest::addColumn<int>("verticalScrollMode");
2092 QTest::addColumn<int>("verticalScrollValue");
2093 QTest::addColumn<int>("rowViewportPosition");
2095 QTest::newRow("row 0, scroll per item 0")
2096 << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerItem) << 0 << 0;
2098 QTest::newRow("row 1, scroll per item, 0")
2099 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 0 << 1 * 40;
2101 QTest::newRow("row 1, scroll per item, 1")
2102 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 1 << 0;
2104 QTest::newRow("row 5, scroll per item, 0")
2105 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 0 << 5 * 40;
2107 QTest::newRow("row 5, scroll per item, 5")
2108 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 5 << 0;
2110 QTest::newRow("row 9, scroll per item, 0")
2111 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 0 << 9 * 40;
2113 QTest::newRow("row 9, scroll per item, 5")
2114 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 5 << 4 * 40;
2116 QTest::newRow("row 0, scroll per pixel 0")
2117 << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerPixel) << 0 << 0;
2119 QTest::newRow("row 1, scroll per pixel, 0")
2120 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 0 << 1 * 40;
2122 QTest::newRow("row 1, scroll per pixel, 1")
2123 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 1 * 40 << 0;
2125 QTest::newRow("row 5, scroll per pixel, 0")
2126 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 0 << 5 * 40;
2128 QTest::newRow("row 5, scroll per pixel, 5")
2129 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 0;
2131 QTest::newRow("row 9, scroll per pixel, 0")
2132 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 0 << 9 * 40;
2134 QTest::newRow("row 9, scroll per pixel, 5")
2135 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 4 * 40;
2138 void tst_QTableView::rowViewportPosition()
2140 QFETCH(int, rowCount);
2141 QFETCH(int, rowHeight);
2143 QFETCH(int, verticalScrollMode);
2144 QFETCH(int, verticalScrollValue);
2145 QFETCH(int, rowViewportPosition);
2147 QtTestTableModel model(rowCount, 1);
2148 QtTestTableView view;
2149 view.resize(100, 2 * rowHeight);
2152 view.setModel(&model);
2153 for (int r = 0; r < rowCount; ++r)
2154 view.setRowHeight(r, rowHeight);
2156 view.setVerticalScrollMode((QAbstractItemView::ScrollMode)verticalScrollMode);
2157 view.verticalScrollBar()->setValue(verticalScrollValue);
2159 QCOMPARE(view.rowViewportPosition(row), rowViewportPosition);
2162 void tst_QTableView::rowAt_data()
2164 QTest::addColumn<int>("rowCount");
2165 QTest::addColumn<int>("rowHeight");
2166 QTest::addColumn<IntList>("hiddenRows");
2167 QTest::addColumn<int>("coordinate");
2168 QTest::addColumn<int>("row");
2170 QTest::newRow("row at 100") << 5 << 40 << IntList() << 100 << 2;
2171 QTest::newRow("row at 180") << 5 << 40 << IntList() << 180 << 4;
2172 QTest::newRow("row at 20") << 5 << 40 << IntList() << 20 << 0;
2174 // ### expand the dataset to include hidden rows
2177 void tst_QTableView::rowAt()
2179 QFETCH(int, rowCount);
2180 QFETCH(int, rowHeight);
2181 QFETCH(IntList, hiddenRows);
2182 QFETCH(int, coordinate);
2185 QtTestTableModel model(rowCount, 1);
2186 QtTestTableView view;
2187 view.resize(100, 2 * rowHeight);
2189 view.setModel(&model);
2191 for (int r = 0; r < rowCount; ++r)
2192 view.setRowHeight(r, rowHeight);
2194 for (int i = 0; i < hiddenRows.count(); ++i)
2195 view.hideRow(hiddenRows.at(i));
2197 QCOMPARE(view.rowAt(coordinate), row);
2200 void tst_QTableView::rowHeight_data()
2202 QTest::addColumn<int>("rowCount");
2203 QTest::addColumn<IntList>("rowHeights");
2204 QTest::addColumn<BoolList>("hiddenRows");
2206 QTest::newRow("increasing")
2208 << (IntList() << 20 << 30 << 40 << 50 << 60)
2209 << (BoolList() << false << false << false << false << false);
2211 QTest::newRow("decreasing")
2213 << (IntList() << 60 << 50 << 40 << 30 << 20)
2214 << (BoolList() << false << false << false << false << false);
2216 QTest::newRow("random")
2218 << (IntList() << 87 << 34 << 68 << 91 << 27)
2219 << (BoolList() << false << false << false << false << false);
2221 // ### expand the dataset to include hidden rows
2224 void tst_QTableView::rowHeight()
2226 QFETCH(int, rowCount);
2227 QFETCH(IntList, rowHeights);
2228 QFETCH(BoolList, hiddenRows);
2230 QtTestTableModel model(rowCount, 1);
2231 QtTestTableView view;
2233 view.setModel(&model);
2235 for (int r = 0; r < rowCount; ++r) {
2236 view.setRowHeight(r, rowHeights.at(r));
2237 view.setRowHidden(r, hiddenRows.at(r));
2240 for (int r = 0; r < rowCount; ++r) {
2241 if (hiddenRows.at(r))
2242 QCOMPARE(view.rowHeight(r), 0);
2244 QCOMPARE(view.rowHeight(r), rowHeights.at(r));
2248 void tst_QTableView::columnViewportPosition_data()
2250 QTest::addColumn<int>("columnCount");
2251 QTest::addColumn<int>("columnWidth");
2252 QTest::addColumn<int>("column");
2253 QTest::addColumn<int>("horizontalScrollMode");
2254 QTest::addColumn<int>("horizontalScrollValue");
2255 QTest::addColumn<int>("columnViewportPosition");
2257 QTest::newRow("column 0, scroll per item 0")
2258 << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerItem) << 0 << 0;
2260 QTest::newRow("column 1, scroll per item, 0")
2261 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 0 << 1 * 40;
2263 QTest::newRow("column 1, scroll per item, 1")
2264 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 1 << 0;
2266 QTest::newRow("column 5, scroll per item, 0")
2267 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 0 << 5 * 40;
2269 QTest::newRow("column 5, scroll per item, 5")
2270 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 5 << 0;
2272 QTest::newRow("column 9, scroll per item, 0")
2273 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 0 << 9 * 40;
2275 QTest::newRow("column 9, scroll per item, 5")
2276 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 5 << 4 * 40;
2278 QTest::newRow("column 0, scroll per pixel 0")
2279 << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerPixel) << 0 << 0;
2281 QTest::newRow("column 1, scroll per pixel 0")
2282 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 0 << 1 * 40;
2284 QTest::newRow("column 1, scroll per pixel 1")
2285 << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 1 * 40 << 0;
2287 QTest::newRow("column 5, scroll per pixel 0")
2288 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 0 << 5 * 40;
2290 QTest::newRow("column 5, scroll per pixel 5")
2291 << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 0;
2293 QTest::newRow("column 9, scroll per pixel 0")
2294 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 0 << 9 * 40;
2296 QTest::newRow("column 9, scroll per pixel 5")
2297 << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 4 * 40;
2300 void tst_QTableView::columnViewportPosition()
2302 QFETCH(int, columnCount);
2303 QFETCH(int, columnWidth);
2304 QFETCH(int, column);
2305 QFETCH(int, horizontalScrollMode);
2306 QFETCH(int, horizontalScrollValue);
2307 QFETCH(int, columnViewportPosition);
2309 QtTestTableModel model(1, columnCount);
2310 QtTestTableView view;
2311 view.resize(2 * columnWidth, 100);
2314 view.setModel(&model);
2315 for (int c = 0; c < columnCount; ++c)
2316 view.setColumnWidth(c, columnWidth);
2318 view.setHorizontalScrollMode((QAbstractItemView::ScrollMode)horizontalScrollMode);
2319 view.horizontalScrollBar()->setValue(horizontalScrollValue);
2321 QCOMPARE(view.columnViewportPosition(column), columnViewportPosition);
2324 void tst_QTableView::columnAt_data()
2326 QTest::addColumn<int>("columnCount");
2327 QTest::addColumn<int>("columnWidth");
2328 QTest::addColumn<IntList>("hiddenColumns");
2329 QTest::addColumn<int>("coordinate");
2330 QTest::addColumn<int>("column");
2332 QTest::newRow("column at 100") << 5 << 40 << IntList() << 100 << 2;
2333 QTest::newRow("column at 180") << 5 << 40 << IntList() << 180 << 4;
2334 QTest::newRow("column at 20") << 5 << 40 << IntList() << 20 << 0;
2336 // ### expand the dataset to include hidden coumns
2339 void tst_QTableView::columnAt()
2341 QFETCH(int, columnCount);
2342 QFETCH(int, columnWidth);
2343 QFETCH(IntList, hiddenColumns);
2344 QFETCH(int, coordinate);
2345 QFETCH(int, column);
2347 QtTestTableModel model(1, columnCount);
2348 QtTestTableView view;
2349 view.resize(2 * columnWidth, 100);
2351 view.setModel(&model);
2353 for (int c = 0; c < columnCount; ++c)
2354 view.setColumnWidth(c, columnWidth);
2356 for (int i = 0; i < hiddenColumns.count(); ++i)
2357 view.hideColumn(hiddenColumns.at(i));
2359 QCOMPARE(view.columnAt(coordinate), column);
2362 void tst_QTableView::columnWidth_data()
2364 QTest::addColumn<int>("columnCount");
2365 QTest::addColumn<IntList>("columnWidths");
2366 QTest::addColumn<BoolList>("hiddenColumns");
2368 QTest::newRow("increasing")
2370 << (IntList() << 20 << 30 << 40 << 50 << 60)
2371 << (BoolList() << false << false << false << false << false);
2373 QTest::newRow("decreasing")
2375 << (IntList() << 60 << 50 << 40 << 30 << 20)
2376 << (BoolList() << false << false << false << false << false);
2378 QTest::newRow("random")
2380 << (IntList() << 87 << 34 << 68 << 91 << 27)
2381 << (BoolList() << false << false << false << false << false);
2383 // ### expand the dataset to include hidden columns
2386 void tst_QTableView::columnWidth()
2388 QFETCH(int, columnCount);
2389 QFETCH(IntList, columnWidths);
2390 QFETCH(BoolList, hiddenColumns);
2392 QtTestTableModel model(1, columnCount);
2393 QtTestTableView view;
2395 view.setModel(&model);
2397 for (int c = 0; c < columnCount; ++c) {
2398 view.setColumnWidth(c, columnWidths.at(c));
2399 view.setColumnHidden(c, hiddenColumns.at(c));
2402 for (int c = 0; c < columnCount; ++c) {
2403 if (hiddenColumns.at(c))
2404 QCOMPARE(view.columnWidth(c), 0);
2406 QCOMPARE(view.columnWidth(c), columnWidths.at(c));
2410 void tst_QTableView::hiddenRow_data()
2412 QTest::addColumn<int>("rowCount");
2413 QTest::addColumn<BoolList>("hiddenRows");
2415 QTest::newRow("first hidden")
2416 << 5 << (BoolList() << true << false << false << false << false);
2418 QTest::newRow("last hidden")
2419 << 5 << (BoolList() << false << false << false << false << true);
2421 QTest::newRow("none hidden")
2422 << 5 << (BoolList() << false << false << false << false << false);
2424 QTest::newRow("all hidden")
2425 << 5 << (BoolList() << true << true << true << true << true);
2428 void tst_QTableView::hiddenRow()
2430 QFETCH(int, rowCount);
2431 QFETCH(BoolList, hiddenRows);
2434 QtTestTableModel model(rowCount, 1);
2435 QtTestTableView view;
2437 view.setModel(&model);
2439 for (int r = 0; r < rowCount; ++r)
2440 QVERIFY(!view.isRowHidden(r));
2442 for (int r = 0; r < rowCount; ++r)
2443 view.setRowHidden(r, hiddenRows.at(r));
2445 for (int r = 0; r < rowCount; ++r)
2446 QCOMPARE(view.isRowHidden(r), hiddenRows.at(r));
2448 for (int r = 0; r < rowCount; ++r)
2449 view.setRowHidden(r, false);
2451 for (int r = 0; r < rowCount; ++r)
2452 QVERIFY(!view.isRowHidden(r));
2455 void tst_QTableView::hiddenColumn_data()
2457 QTest::addColumn<int>("columnCount");
2458 QTest::addColumn<BoolList>("hiddenColumns");
2460 QTest::newRow("first hidden")
2461 << 5 << (BoolList() << true << false << false << false << false);
2463 QTest::newRow("last hidden")
2464 << 5 << (BoolList() << false << false << false << false << true);
2466 QTest::newRow("none hidden")
2467 << 5 << (BoolList() << false << false << false << false << false);
2469 QTest::newRow("all hidden")
2470 << 5 << (BoolList() << true << true << true << true << true);
2473 void tst_QTableView::hiddenColumn()
2475 QFETCH(int, columnCount);
2476 QFETCH(BoolList, hiddenColumns);
2478 QtTestTableModel model(1, columnCount);
2479 QtTestTableView view;
2481 view.setModel(&model);
2483 for (int c = 0; c < columnCount; ++c)
2484 QVERIFY(!view.isColumnHidden(c));
2486 for (int c = 0; c < columnCount; ++c)
2487 view.setColumnHidden(c, hiddenColumns.at(c));
2489 for (int c = 0; c < columnCount; ++c)
2490 QCOMPARE(view.isColumnHidden(c), hiddenColumns.at(c));
2492 for (int c = 0; c < columnCount; ++c)
2493 view.setColumnHidden(c, false);
2495 for (int c = 0; c < columnCount; ++c)
2496 QVERIFY(!view.isColumnHidden(c));
2499 void tst_QTableView::sortingEnabled_data()
2501 // QTest::addColumn<int>("columnCount");
2504 void tst_QTableView::sortingEnabled()
2506 // QFETCH(int, columnCount);
2509 void tst_QTableView::scrollTo_data()
2511 QTest::addColumn<int>("verticalScrollMode");
2512 QTest::addColumn<int>("horizontalScrollMode");
2513 QTest::addColumn<int>("rowCount");
2514 QTest::addColumn<int>("columnCount");
2515 QTest::addColumn<int>("rowHeight");
2516 QTest::addColumn<int>("columnWidth");
2517 QTest::addColumn<int>("hiddenRow");
2518 QTest::addColumn<int>("hiddenColumn");
2519 QTest::addColumn<int>("row");
2520 QTest::addColumn<int>("column");
2521 QTest::addColumn<int>("rowSpan");
2522 QTest::addColumn<int>("columnSpan");
2523 QTest::addColumn<int>("horizontalScroll");
2524 QTest::addColumn<int>("verticalScroll");
2525 QTest::addColumn<int>("scrollHint");
2526 QTest::addColumn<int>("expectedHorizontalScroll");
2527 QTest::addColumn<int>("expectedVerticalScroll");
2529 QTest::newRow("no hidden, no span, no scroll, per item")
2530 << (int)QAbstractItemView::ScrollPerItem
2531 << (int)QAbstractItemView::ScrollPerItem
2532 << 10 << 10 // table
2538 << (int)QAbstractItemView::PositionAtTop
2539 << 0 << 0; // expected
2541 QTest::newRow("no hidden, no span, no scroll, per pixel")
2542 << (int)QAbstractItemView::ScrollPerPixel
2543 << (int)QAbstractItemView::ScrollPerPixel
2544 << 10 << 10 // table
2550 << (int)QAbstractItemView::PositionAtTop
2551 << 0 << 0; // expected
2553 QTest::newRow("hidden, no span, no scroll, per item")
2554 << (int)QAbstractItemView::ScrollPerItem
2555 << (int)QAbstractItemView::ScrollPerItem
2556 << 10 << 10 // table
2562 << (int)QAbstractItemView::PositionAtTop
2563 << 4 << 4; // expected
2566 void tst_QTableView::scrollTo()
2568 QFETCH(int, horizontalScrollMode);
2569 QFETCH(int, verticalScrollMode);
2570 QFETCH(int, rowCount);
2571 QFETCH(int, columnCount);
2572 QFETCH(int, rowHeight);
2573 QFETCH(int, columnWidth);
2574 QFETCH(int, hiddenRow);
2575 QFETCH(int, hiddenColumn);
2577 QFETCH(int, column);
2578 QFETCH(int, rowSpan);
2579 QFETCH(int, columnSpan);
2580 QFETCH(int, horizontalScroll);
2581 QFETCH(int, verticalScroll);
2582 QFETCH(int, scrollHint);
2583 QFETCH(int, expectedHorizontalScroll);
2584 QFETCH(int, expectedVerticalScroll);
2586 QtTestTableModel model(rowCount, columnCount);
2588 QtTestTableView view(&toplevel);
2591 // resizing to this size will ensure that there can ONLY_BE_ONE_CELL inside the view.
2592 QSize forcedSize(columnWidth * 2, rowHeight * 2);
2593 view.resize(forcedSize);
2594 QVERIFY(QTest::qWaitForWindowExposed(&toplevel));
2595 QTRY_COMPARE(view.size(), forcedSize);
2597 view.setModel(&model);
2598 view.setSpan(row, column, rowSpan, columnSpan);
2599 view.hideRow(hiddenRow);
2600 view.hideColumn(hiddenColumn);
2601 view.setHorizontalScrollMode((QAbstractItemView::ScrollMode)horizontalScrollMode);
2602 view.setVerticalScrollMode((QAbstractItemView::ScrollMode)verticalScrollMode);
2604 for (int r = 0; r < rowCount; ++r)
2605 view.setRowHeight(r, rowHeight);
2606 for (int c = 0; c < columnCount; ++c)
2607 view.setColumnWidth(c, columnWidth);
2609 QTest::qWait(150); // ### needed to pass the test
2610 view.horizontalScrollBar()->setValue(horizontalScroll);
2611 view.verticalScrollBar()->setValue(verticalScroll);
2613 QModelIndex index = model.index(row, column);
2614 QVERIFY(index.isValid());
2615 view.scrollTo(index, (QAbstractItemView::ScrollHint)scrollHint);
2616 QCOMPARE(view.verticalScrollBar()->value(), expectedVerticalScroll);
2617 QCOMPARE(view.horizontalScrollBar()->value(), expectedHorizontalScroll);
2620 void tst_QTableView::indexAt_data()
2622 QTest::addColumn<int>("rowCount");
2623 QTest::addColumn<int>("columnCount");
2625 QTest::addColumn<int>("rowHeight");
2626 QTest::addColumn<int>("columnWidth");
2628 QTest::addColumn<int>("hiddenRow");
2629 QTest::addColumn<int>("hiddenColumn");
2631 QTest::addColumn<int>("row");
2632 QTest::addColumn<int>("column");
2633 QTest::addColumn<int>("rowSpan");
2634 QTest::addColumn<int>("columnSpan");
2635 QTest::addColumn<int>("horizontalScroll");
2636 QTest::addColumn<int>("verticalScroll");
2637 QTest::addColumn<int>("x");
2638 QTest::addColumn<int>("y");
2639 QTest::addColumn<int>("expectedRow");
2640 QTest::addColumn<int>("expectedColumn");
2642 QTest::newRow("no hidden, no span, no scroll, (20,20)")
2649 << 20 << 20 // point
2650 << 0 << 0; // expected
2652 QTest::newRow("row hidden, no span, no scroll, at (20,20)")
2659 << 20 << 20 // point
2660 << 1 << 0; // expected
2662 QTest::newRow("col hidden, no span, no scroll, at (20,20)")
2669 << 20 << 20 // point
2670 << 0 << 1; // expected
2672 QTest::newRow("no hidden, row span, no scroll, at (60,20)")
2679 << 20 << 60 // point
2680 << 0 << 0; // expected
2683 QTest::newRow("no hidden, col span, no scroll, at (60,20)")
2690 << 60 << 20 // point
2691 << 0 << 0; // expected
2693 QTest::newRow("no hidden, no span, scroll (5,0), at (20,20)")
2700 << 20 << 20 // point
2701 << 0 << 5; // expected
2703 QTest::newRow("no hidden, no span, scroll (0,5), at (20,20)")
2710 << 20 << 20 // point
2711 << 5 << 0; // expected
2713 QTest::newRow("no hidden, no span, scroll (5,5), at (20,20)")
2720 << 20 << 20 // point
2721 << 5 << 5; // expected
2724 void tst_QTableView::indexAt()
2726 QFETCH(int, rowCount);
2727 QFETCH(int, columnCount);
2728 QFETCH(int, rowHeight);
2729 QFETCH(int, columnWidth);
2730 QFETCH(int, hiddenRow);
2731 QFETCH(int, hiddenColumn);
2733 QFETCH(int, column);
2734 QFETCH(int, rowSpan);
2735 QFETCH(int, columnSpan);
2736 QFETCH(int, horizontalScroll);
2737 QFETCH(int, verticalScroll);
2740 QFETCH(int, expectedRow);
2741 QFETCH(int, expectedColumn);
2743 QtTestTableModel model(rowCount, columnCount);
2745 QtTestTableView view(&toplevel);
2748 QVERIFY(QTest::qWaitForWindowExposed(&toplevel));
2750 //some styles change the scroll mode in their polish
2751 view.setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
2752 view.setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
2754 view.setModel(&model);
2755 view.setSpan(row, column, rowSpan, columnSpan);
2756 view.hideRow(hiddenRow);
2757 view.hideColumn(hiddenColumn);
2759 for (int r = 0; r < rowCount; ++r)
2760 view.setRowHeight(r, rowHeight);
2761 for (int c = 0; c < columnCount; ++c)
2762 view.setColumnWidth(c, columnWidth);
2765 view.horizontalScrollBar()->setValue(horizontalScroll);
2766 view.verticalScrollBar()->setValue(verticalScroll);
2769 QModelIndex index = view.indexAt(QPoint(x, y));
2770 QCOMPARE(index.row(), expectedRow);
2771 QCOMPARE(index.column(), expectedColumn);
2774 void tst_QTableView::span_data()
2776 QTest::addColumn<int>("rowCount");
2777 QTest::addColumn<int>("columnCount");
2778 QTest::addColumn<int>("hiddenRow");
2779 QTest::addColumn<int>("hiddenColumn");
2780 QTest::addColumn<int>("row");
2781 QTest::addColumn<int>("column");
2782 QTest::addColumn<int>("rowSpan");
2783 QTest::addColumn<int>("columnSpan");
2784 QTest::addColumn<int>("expectedRowSpan");
2785 QTest::addColumn<int>("expectedColumnSpan");
2786 QTest::addColumn<bool>("clear");
2788 QTest::newRow("top left 2x2")
2796 QTest::newRow("top left 1x2")
2804 QTest::newRow("top left 2x1")
2812 /* This makes no sens.
2813 QTest::newRow("top left 2x0")
2821 QTest::newRow("top left 0x2")
2829 QTest::newRow("invalid 2x2")
2837 QTest::newRow("top left 2x2")
2845 QTest::newRow("bottom right 2x2")
2853 QTest::newRow("invalid span 2x2")
2861 QTest::newRow("invalid span 3x3")
2871 void tst_QTableView::span()
2873 QFETCH(int, rowCount);
2874 QFETCH(int, columnCount);
2875 QFETCH(int, hiddenRow);
2876 QFETCH(int, hiddenColumn);
2878 QFETCH(int, column);
2879 QFETCH(int, rowSpan);
2880 QFETCH(int, columnSpan);
2881 QFETCH(int, expectedRowSpan);
2882 QFETCH(int, expectedColumnSpan);
2883 QFETCH(bool, clear);
2885 QtTestTableModel model(rowCount, columnCount);
2886 QtTestTableView view;
2888 view.setModel(&model);
2891 view.setSpan(row, column, rowSpan, columnSpan);
2893 model.removeLastRow();
2894 model.removeLastRow();
2898 view.hideRow(hiddenRow);
2899 view.hideColumn(hiddenColumn);
2902 QCOMPARE(view.rowSpan(row, column), expectedRowSpan);
2903 QCOMPARE(view.columnSpan(row, column), expectedColumnSpan);
2905 if (hiddenRow > -1) {
2906 QModelIndex hidden = model.index(hiddenRow, columnCount - 1);
2907 QVERIFY(view.isIndexHidden(hidden));
2910 if (hiddenColumn > -1) {
2911 QModelIndex hidden = model.index(rowCount - 1, hiddenColumn);
2912 QVERIFY(view.isIndexHidden(hidden));
2916 QCOMPARE(view.rowSpan(row, column), 1);
2917 QCOMPARE(view.columnSpan(row, column), 1);
2919 VERIFY_SPANS_CONSISTENCY(&view);
2922 typedef QVector<QRect> SpanList;
2923 Q_DECLARE_METATYPE(SpanList)
2925 void tst_QTableView::spans_data()
2927 QTest::addColumn<int>("rows");
2928 QTest::addColumn<int>("columns");
2929 QTest::addColumn<SpanList>("spans");
2930 QTest::addColumn<bool>("hideRowLastRowOfFirstSpan");
2931 QTest::addColumn<QPoint>("pos");
2932 QTest::addColumn<int>("expectedRowSpan");
2933 QTest::addColumn<int>("expectedColumnSpan");
2935 QTest::newRow("1x3 span, query 3,0")
2937 << (SpanList() << QRect(3, 0, 1, 3))
2938 << false //no hidden row
2943 QTest::newRow("1x3 span, query 3,1")
2945 << (SpanList() << QRect(3, 0, 1, 3))
2946 << false //no hidden row
2951 QTest::newRow("1x3 span, query 3,2")
2953 << (SpanList() << QRect(3, 0, 1, 3))
2954 << false //no hidden row
2959 QTest::newRow("two 1x2 spans at the same column, query at 3,0")
2961 << (SpanList() << QRect(3, 0, 1, 2) << QRect(4, 0, 1, 2))
2962 << false //no hidden row
2967 QTest::newRow("two 1x2 spans at the same column, query at 4,0")
2969 << (SpanList() << QRect(3, 0, 1, 2) << QRect(4, 0, 1, 2))
2970 << false //no hidden row
2975 QTest::newRow("how to order spans (1,1)")
2977 << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
2978 << false //no hidden row
2983 QTest::newRow("how to order spans (2,1)")
2985 << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
2986 << false //no hidden row
2991 QTest::newRow("how to order spans (3,1)")
2993 << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
2994 << false //no hidden row
2999 QTest::newRow("how to order spans (1,2)")
3001 << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
3002 << false //no hidden row
3007 QTest::newRow("how to order spans (2,2)")
3009 << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
3010 << false //no hidden row
3015 QTest::newRow("spans with hidden rows")
3017 << (SpanList() << QRect(0, 0, 2, 2) << QRect(2, 0, 1, 2))
3018 << true //we hide the last row of the first span
3023 QTest::newRow("QTBUG-6004: No failing assertion, then it passes.")
3025 << (SpanList() << QRect(0, 0, 2, 2) << QRect(0, 0, 1, 1))
3031 QTest::newRow("QTBUG-6004 (follow-up): No failing assertion, then it passes.")
3033 << (SpanList() << QRect(2, 2, 1, 3) << QRect(2, 2, 1, 1))
3039 QTest::newRow("QTBUG-9631: remove one span")
3041 << (SpanList() << QRect(1, 1, 2, 1) << QRect(2, 2, 2, 2) << QRect(1, 1, 1, 1))
3048 void tst_QTableView::spans()
3051 QFETCH(int, columns);
3052 QFETCH(SpanList, spans);
3053 QFETCH(bool, hideRowLastRowOfFirstSpan);
3054 QFETCH(QPoint, pos);
3055 QFETCH(int, expectedRowSpan);
3056 QFETCH(int, expectedColumnSpan);
3058 QtTestTableModel model(rows, columns);
3059 QtTestTableView view;
3061 view.setModel(&model);
3064 for (int i = 0; i < spans.count(); ++i) {
3065 QRect sp = spans.at(i);
3066 view.setSpan(sp.x(), sp.y(), sp.width(), sp.height());
3069 if (hideRowLastRowOfFirstSpan) {
3070 view.setRowHidden(spans.at(0).bottom(), true);
3071 //we check that the span didn't break the visual rects of the model indexes
3072 QRect first = view.visualRect( model.index(spans.at(0).top(), 0));
3073 QRect next = view.visualRect( model.index(spans.at(0).bottom() + 1, 0));
3074 QVERIFY(first.intersected(next).isEmpty());
3077 QCOMPARE(view.columnSpan(pos.x(), pos.y()), expectedColumnSpan);
3078 QCOMPARE(view.rowSpan(pos.x(), pos.y()), expectedRowSpan);
3080 VERIFY_SPANS_CONSISTENCY(&view);
3083 void tst_QTableView::spansAfterRowInsertion()
3085 QtTestTableModel model(10, 10);
3086 QtTestTableView view;
3087 view.setModel(&model);
3088 view.setSpan(3, 3, 3, 3);
3092 // Insertion before the span only shifts the span.
3093 view.model()->insertRows(0, 2);
3094 QCOMPARE(view.rowSpan(3, 3), 1);
3095 QCOMPARE(view.columnSpan(3, 3), 1);
3096 QCOMPARE(view.rowSpan(5, 3), 3);
3097 QCOMPARE(view.columnSpan(5, 3), 3);
3099 // Insertion happens before the given row, so it only shifts the span also.
3100 view.model()->insertRows(5, 2);
3101 QCOMPARE(view.rowSpan(5, 3), 1);
3102 QCOMPARE(view.columnSpan(5, 3), 1);
3103 QCOMPARE(view.rowSpan(7, 3), 3);
3104 QCOMPARE(view.columnSpan(7, 3), 3);
3106 // Insertion inside the span expands it.
3107 view.model()->insertRows(8, 2);
3108 QCOMPARE(view.rowSpan(7, 3), 5);
3109 QCOMPARE(view.columnSpan(7, 3), 3);
3111 // Insertion after the span does nothing to it.
3112 view.model()->insertRows(12, 2);
3113 QCOMPARE(view.rowSpan(7, 3), 5);
3114 QCOMPARE(view.columnSpan(7, 3), 3);
3116 VERIFY_SPANS_CONSISTENCY(&view);
3119 void tst_QTableView::spansAfterColumnInsertion()
3121 QtTestTableModel model(10, 10);
3122 QtTestTableView view;
3123 view.setModel(&model);
3124 view.setSpan(3, 3, 3, 3);
3128 // Insertion before the span only shifts the span.
3129 view.model()->insertColumns(0, 2);
3130 QCOMPARE(view.rowSpan(3, 3), 1);
3131 QCOMPARE(view.columnSpan(3, 3), 1);
3132 QCOMPARE(view.rowSpan(3, 5), 3);
3133 QCOMPARE(view.columnSpan(3, 5), 3);
3135 // Insertion happens before the given column, so it only shifts the span also.
3136 view.model()->insertColumns(5, 2);
3137 QCOMPARE(view.rowSpan(3, 5), 1);
3138 QCOMPARE(view.columnSpan(3, 5), 1);
3139 QCOMPARE(view.rowSpan(3, 7), 3);
3140 QCOMPARE(view.columnSpan(3, 7), 3);
3142 // Insertion inside the span expands it.
3143 view.model()->insertColumns(8, 2);
3144 QCOMPARE(view.rowSpan(3, 7), 3);
3145 QCOMPARE(view.columnSpan(3, 7), 5);
3147 // Insertion after the span does nothing to it.
3148 view.model()->insertColumns(12, 2);
3149 QCOMPARE(view.rowSpan(3, 7), 3);
3150 QCOMPARE(view.columnSpan(3, 7), 5);
3152 VERIFY_SPANS_CONSISTENCY(&view);
3155 void tst_QTableView::spansAfterRowRemoval()
3157 QtTestTableModel model(10, 10);
3158 QtTestTableView view;
3159 view.setModel(&model);
3162 spans << QRect(0, 1, 1, 2)
3163 << QRect(1, 2, 1, 2)
3164 << QRect(2, 2, 1, 5)
3165 << QRect(2, 8, 1, 2)
3166 << QRect(3, 4, 1, 2)
3167 << QRect(4, 4, 1, 4)
3168 << QRect(5, 6, 1, 3)
3169 << QRect(6, 7, 1, 3);
3170 foreach (QRect span, spans)
3171 view.setSpan(span.top(), span.left(), span.height(), span.width());
3175 view.model()->removeRows(3, 3);
3177 QList<QRect> expectedSpans;
3178 expectedSpans << QRect(0, 1, 1, 2)
3179 << QRect(1, 2, 1, 1)
3180 << QRect(2, 2, 1, 2)
3181 << QRect(2, 5, 1, 2)
3182 << QRect(3, 4, 1, 1)
3183 << QRect(4, 3, 1, 2)
3184 << QRect(5, 3, 1, 3)
3185 << QRect(6, 4, 1, 3);
3186 foreach (QRect span, expectedSpans) {
3187 QCOMPARE(view.columnSpan(span.top(), span.left()), span.width());
3188 QCOMPARE(view.rowSpan(span.top(), span.left()), span.height());
3191 VERIFY_SPANS_CONSISTENCY(&view);
3194 void tst_QTableView::spansAfterColumnRemoval()
3196 QtTestTableModel model(10, 10);
3197 QtTestTableView view;
3198 view.setModel(&model);
3200 // Same set as above just swapping columns and rows.
3202 spans << QRect(0, 1, 1, 2)
3203 << QRect(1, 2, 1, 2)
3204 << QRect(2, 2, 1, 5)
3205 << QRect(2, 8, 1, 2)
3206 << QRect(3, 4, 1, 2)
3207 << QRect(4, 4, 1, 4)
3208 << QRect(5, 6, 1, 3)
3209 << QRect(6, 7, 1, 3);
3210 foreach (QRect span, spans)
3211 view.setSpan(span.left(), span.top(), span.width(), span.height());
3215 view.model()->removeColumns(3, 3);
3217 QList<QRect> expectedSpans;
3218 expectedSpans << QRect(0, 1, 1, 2)
3219 << QRect(1, 2, 1, 1)
3220 << QRect(2, 2, 1, 2)
3221 << QRect(2, 5, 1, 2)
3222 << QRect(3, 4, 1, 1)
3223 << QRect(4, 3, 1, 2)
3224 << QRect(5, 3, 1, 3)
3225 << QRect(6, 4, 1, 3);
3226 foreach (QRect span, expectedSpans) {
3227 QCOMPARE(view.columnSpan(span.left(), span.top()), span.height());
3228 QCOMPARE(view.rowSpan(span.left(), span.top()), span.width());
3231 VERIFY_SPANS_CONSISTENCY(&view);
3234 class Model : public QAbstractTableModel {
3239 Model(QObject * parent = 0) : QAbstractTableModel(parent) {
3242 int rowCount(const QModelIndex &) const {
3245 int columnCount(const QModelIndex &) const {
3248 QVariant data(const QModelIndex &, int) const
3252 void res() { reset(); }
3258 void tst_QTableView::checkHeaderReset()
3269 QCOMPARE(view.horizontalHeader()->count(), 4);
3272 void tst_QTableView::checkHeaderMinSize()
3274 //tests if the minimumsize is of a header is taken into account
3275 //while computing QTableView geometry. For that we test the position of the
3279 m.setStringList( QStringList() << QLatin1String("one cell is enough"));
3282 //setting the minimum height on the horizontal header
3283 //and the minimum width on the vertical header
3284 view.horizontalHeader()->setMinimumHeight(50);
3285 view.verticalHeader()->setMinimumWidth(100);
3289 QVERIFY( view.verticalHeader()->y() >= view.horizontalHeader()->minimumHeight());
3290 QVERIFY( view.horizontalHeader()->x() >= view.verticalHeader()->minimumWidth());
3293 void tst_QTableView::resizeToContents()
3295 //checks that the resize to contents is consistent
3296 QTableWidget table(2,3);
3297 QTableWidget table2(2,3);
3298 QTableWidget table3(2,3);
3301 table.setHorizontalHeaderItem(0, new QTableWidgetItem("A Lot of text here: BLA BLA BLA"));
3302 table2.setHorizontalHeaderItem(0, new QTableWidgetItem("A Lot of text here: BLA BLA BLA"));
3303 table3.setHorizontalHeaderItem(0, new QTableWidgetItem("A Lot of text here: BLA BLA BLA"));
3304 table.horizontalHeader()->setVisible(false);
3305 table2.horizontalHeader()->setVisible(false);
3306 table.verticalHeader()->setVisible(false);
3307 table2.verticalHeader()->setVisible(false);
3310 for(int i = 0;i<table.columnCount();i++) {
3311 table.resizeColumnToContents(i);
3313 for(int i = 0;i<table.rowCount();i++) {
3314 table.resizeRowToContents(i);
3316 table2.resizeColumnsToContents();
3317 table2.resizeRowsToContents();
3318 table3.resizeColumnsToContents();
3319 table3.resizeRowsToContents();
3321 //now let's check the row/col sizes
3322 for(int i = 0;i<table.columnCount();i++) {
3323 QCOMPARE( table.columnWidth(i), table2.columnWidth(i));
3324 QCOMPARE( table2.columnWidth(i), table3.columnWidth(i));
3326 for(int i = 0;i<table.rowCount();i++) {
3327 QCOMPARE( table.rowHeight(i), table2.rowHeight(i));
3328 QCOMPARE( table2.rowHeight(i), table3.rowHeight(i));
3334 extern bool Q_GUI_EXPORT qt_tab_all_widgets; // qapplication.cpp
3337 void tst_QTableView::tabFocus()
3339 if (!qt_tab_all_widgets)
3340 QSKIP("This test requires full keyboard control to be enabled.");
3342 // QTableView enables tabKeyNavigation by default, but you should be able
3343 // to change focus on an empty table view, or on a table view that doesn't
3344 // have this property set.
3347 QTableView *view = new QTableView(&window);
3348 QLineEdit *edit = new QLineEdit(&window);
3351 QApplication::setActiveWindow(&window);
3353 window.activateWindow();
3354 QVERIFY(QTest::qWaitForWindowActive(&window));
3357 QVERIFY(window.hasFocus());
3358 QVERIFY(!view->hasFocus());
3359 QVERIFY(!edit->hasFocus());
3361 for (int i = 0; i < 2; ++i) {
3363 QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
3364 QTRY_VERIFY(!window.hasFocus());
3365 QVERIFY(view->hasFocus());
3366 QVERIFY(!edit->hasFocus());
3369 QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
3370 QTRY_VERIFY(edit->hasFocus());
3371 QVERIFY(!window.hasFocus());
3372 QVERIFY(!view->hasFocus());
3376 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3377 QTRY_VERIFY(view->hasFocus());
3378 QVERIFY(!window.hasFocus());
3379 QVERIFY(!edit->hasFocus());
3382 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3383 QTRY_VERIFY(edit->hasFocus());
3384 QVERIFY(!window.hasFocus());
3385 QVERIFY(!view->hasFocus());
3387 QStandardItemModel *model = new QStandardItemModel;
3388 view->setModel(model);
3391 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3392 QTRY_VERIFY(view->hasFocus());
3393 QVERIFY(!window.hasFocus());
3394 QVERIFY(!edit->hasFocus());
3397 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3398 QTRY_VERIFY(edit->hasFocus());
3399 QVERIFY(!window.hasFocus());
3400 QVERIFY(!view->hasFocus());
3402 model->insertRow(0, new QStandardItem("Hei"));
3403 model->insertRow(0, new QStandardItem("Hei"));
3404 model->insertRow(0, new QStandardItem("Hei"));
3407 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3408 QTRY_VERIFY(view->hasFocus());
3409 QVERIFY(!window.hasFocus());
3410 QVERIFY(!edit->hasFocus());
3412 // backtab to edit doesn't work
3413 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3414 QVERIFY(!window.hasFocus());
3415 QVERIFY(view->hasFocus());
3416 QVERIFY(!edit->hasFocus());
3418 view->setTabKeyNavigation(false);
3421 QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
3422 QTRY_VERIFY(edit->hasFocus());
3423 QVERIFY(!window.hasFocus());
3424 QVERIFY(!view->hasFocus());
3426 QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
3427 QTRY_VERIFY(view->hasFocus());
3428 QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
3429 QTRY_VERIFY(edit->hasFocus());
3434 class BigModel : public QAbstractTableModel
3438 virtual QVariant data(const QModelIndex &index,
3439 int role = Qt::DisplayRole) const
3441 if (role == Qt::DisplayRole)
3442 return QString("%1 - %2").arg(index.column()).arg(index.row());
3447 int rowCount(const QModelIndex & parent = QModelIndex()) const
3453 int columnCount(const QModelIndex & parent = QModelIndex()) const
3460 void tst_QTableView::bigModel()
3465 view.setModel(&model);
3467 view.setSpan(10002,10002,6,6);
3469 view.resize(1000,1000);
3471 view.scrollTo(model.index(10010,10010));
3475 void tst_QTableView::selectionSignal()
3477 QtTestTableModel model(10, 10);
3478 QtTestTableView view;
3479 view.checkSignalOrder = true;
3480 view.setModel(&model);
3481 view.resize(200, 200);
3483 QVERIFY(QTest::qWaitForWindowExposed(&view));
3484 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, view.visualRect(model.index(2, 0)).center());
3487 void tst_QTableView::setCurrentIndex()
3489 QtTestTableModel model(4, 4);
3490 QtTestTableView view;
3491 view.setModel(&model);
3493 // submit() slot should be called in model when current row changes
3494 view.setCurrentIndex(model.index(0,0));
3495 QCOMPARE(model.submit_count, 1);
3496 view.setCurrentIndex(model.index(0,2));
3497 QCOMPARE(model.submit_count, 1);
3498 view.setCurrentIndex(model.index(1,0));
3499 QCOMPARE(model.submit_count, 2);
3500 view.setCurrentIndex(model.index(3,3));
3501 QCOMPARE(model.submit_count, 3);
3502 view.setCurrentIndex(model.index(0,1));
3503 QCOMPARE(model.submit_count, 4);
3504 view.setCurrentIndex(model.index(0,0));
3505 QCOMPARE(model.submit_count, 4);
3508 class task173773_EventFilter : public QObject
3510 int paintEventCount_;
3512 task173773_EventFilter() : paintEventCount_(0) {}
3513 int paintEventCount() const { return paintEventCount_; }
3515 bool eventFilter(QObject *obj, QEvent *e)
3518 if (e->type() == QEvent::Paint)
3524 void tst_QTableView::task173773_updateVerticalHeader()
3526 QStandardItemModel model(2, 1);
3527 model.setData(model.index(0, 0), 0);
3528 model.setData(model.index(1, 0), 1);
3530 QSortFilterProxyModel proxyModel;
3531 proxyModel.setSourceModel(&model);
3534 view.setModel(&proxyModel);
3535 view.setSortingEnabled(true);
3537 QVERIFY(QTest::qWaitForWindowExposed(&view));
3539 view.sortByColumn(0, Qt::AscendingOrder);
3542 task173773_EventFilter eventFilter;
3543 view.verticalHeader()->viewport()->installEventFilter(&eventFilter);
3545 view.sortByColumn(0, Qt::DescendingOrder);
3548 // ### note: this test may occasionally pass even if the bug is present!
3549 QVERIFY(eventFilter.paintEventCount() > 0);
3552 void tst_QTableView::task227953_setRootIndex()
3554 QTableView tableView;
3556 //model = tree with two items with tables as children
3557 QStandardItemModel model;
3558 QStandardItem item1, item2;
3559 model.appendColumn(QList<QStandardItem*>() << &item1 << &item2);
3562 //setup the first table as a child of the first item
3563 for ( int row = 0; row < 40; ++row ) {
3564 item1.appendRow(QList<QStandardItem*>() << new QStandardItem(QString("row %0").arg(row)));
3567 //setup the second table as a child of the second item
3568 for ( int row = 0; row < 10; ++row ) {
3569 item2.appendRow(QList<QStandardItem*>() << new QStandardItem(QString("row %0").arg(row)));
3572 tableView.setModel(&model);
3574 //show the first 10 rows of the first table
3575 QModelIndex root = model.indexFromItem(&item1);
3576 tableView.setRootIndex(root);
3577 for (int i = 10; i != 40; ++i) {
3578 tableView.setRowHidden(i, true);
3581 QCOMPARE(tableView.verticalHeader()->count(), 40);
3582 QCOMPARE(tableView.verticalHeader()->hiddenSectionCount(), 30);
3584 //show the first 10 rows of the second table
3585 tableView.setRootIndex(model.indexFromItem(&item2));
3587 QCOMPARE(tableView.verticalHeader()->count(), 10);
3588 QCOMPARE(tableView.verticalHeader()->hiddenSectionCount(), 0);
3589 QVERIFY(!tableView.verticalHeader()->isHidden());
3592 void tst_QTableView::task240266_veryBigColumn()
3595 table.setFixedSize(500, 300); //just to make sure we have the 2 first columns visible
3596 QStandardItemModel model(1, 3);
3597 table.setModel(&model);
3598 table.setColumnWidth(0, 100); //normal column
3599 table.setColumnWidth(1, 100); //normal column
3600 table.setColumnWidth(2, 9000); //very big column
3602 QVERIFY(QTest::qWaitForWindowExposed(&table));
3604 //some styles change the scroll mode in their polish
3605 table.setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
3606 table.setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
3608 QScrollBar *scroll = table.horizontalScrollBar();
3609 QCOMPARE(scroll->minimum(), 0);
3610 QCOMPARE(scroll->maximum(), model.columnCount() - 1);
3611 QCOMPARE(scroll->singleStep(), 1);
3613 //1 is not always a very correct value for pageStep. Ideally this should be dynamic.
3614 //Maybe something for Qt 5 ;-)
3615 QCOMPARE(scroll->pageStep(), 1);
3619 void tst_QTableView::task248688_autoScrollNavigation()
3621 //we make sure that when navigating with the keyboard the view is correctly scrolled
3622 //to the current item
3623 QStandardItemModel model(16, 16);
3625 view.setModel(&model);
3630 for (int r = 0; r < model.rowCount(); ++r) {
3631 if (view.isRowHidden(r))
3633 for (int c = 0; c < model.columnCount(); ++c) {
3634 if (view.isColumnHidden(c))
3636 QModelIndex index = model.index(r, c);
3637 view.setCurrentIndex(index);
3638 QVERIFY(view.viewport()->rect().contains(view.visualRect(index)));
3644 void tst_QTableView::mouseWheel_data()
3646 QTest::addColumn<int>("scrollMode");
3647 QTest::addColumn<int>("delta");
3648 QTest::addColumn<int>("horizontalPositon");
3649 QTest::addColumn<int>("verticalPosition");
3651 QTest::newRow("scroll up per item")
3652 << int(QAbstractItemView::ScrollPerItem) << 120
3653 << 10 - qApp->wheelScrollLines() << 10 - qApp->wheelScrollLines();
3654 QTest::newRow("scroll down per item")
3655 << int(QAbstractItemView::ScrollPerItem) << -120
3656 << 10 + qApp->wheelScrollLines() << 10 + qApp->wheelScrollLines();
3657 QTest::newRow("scroll down per pixel")
3658 << int(QAbstractItemView::ScrollPerPixel) << -120
3659 << 10 + qApp->wheelScrollLines() * 89 << 10 + qApp->wheelScrollLines() * 28;
3662 void tst_QTableView::mouseWheel()
3665 QSKIP("Since different Windows CE versions sport different taskbars, we skip this test");
3668 QFETCH(int, scrollMode);
3670 QFETCH(int, horizontalPositon);
3671 QFETCH(int, verticalPosition);
3673 QtTestTableModel model(100, 100);
3675 QtTestTableView view(&topLevel);
3676 view.resize(500, 500);
3677 for (int r = 0; r < 100; ++r)
3678 view.setRowHeight(r, 50);
3679 for (int c = 0; c < 100; ++c)
3680 view.setColumnWidth(c, 100);
3683 QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
3685 view.setModel(&model);
3687 view.setHorizontalScrollMode((QAbstractItemView::ScrollMode)scrollMode);
3688 view.setVerticalScrollMode((QAbstractItemView::ScrollMode)scrollMode);
3689 view.horizontalScrollBar()->setValue(10);
3690 view.verticalScrollBar()->setValue(10);
3692 QPoint pos = view.viewport()->geometry().center();
3693 QWheelEvent verticalEvent(pos, delta, 0, 0, Qt::Vertical);
3694 QWheelEvent horizontalEvent(pos, delta, 0, 0, Qt::Horizontal);
3695 QApplication::sendEvent(view.viewport(), &horizontalEvent);
3696 QVERIFY(qAbs(view.horizontalScrollBar()->value() - horizontalPositon) < 10);
3697 QApplication::sendEvent(view.viewport(), &verticalEvent);
3698 QVERIFY(qAbs(view.verticalScrollBar()->value() - verticalPosition) < 10);
3701 void tst_QTableView::addColumnWhileEditing()
3704 QStandardItemModel model(1, 10);
3705 view.setModel(&model);
3706 QModelIndex last = model.index(0,9);
3709 view.openPersistentEditor(last);
3710 view.scrollTo(last);
3712 //let's see if the editor is moved to the right location
3713 //after adding a column
3714 model.setColumnCount(model.columnCount() + 1);
3715 QPointer<QLineEdit> editor = qFindChild<QLineEdit*>(&view);
3717 QCOMPARE(editor->geometry(), view.visualRect(last));
3719 //let's see if the editor is moved to the right location
3720 //after removing a column
3721 view.scrollTo(model.index(0, model.columnCount()-1));
3722 model.setColumnCount(model.columnCount() - 1);
3724 QCOMPARE(editor->geometry(), view.visualRect(last));
3727 void tst_QTableView::task259308_scrollVerticalHeaderSwappedSections()
3729 QStandardItemModel model;
3730 model.setRowCount(50);
3731 model.setColumnCount(2);
3732 for (int row = 0; row < model.rowCount(); ++row)
3733 for (int col = 0; col < model.columnCount(); ++col) {
3734 const QModelIndex &idx = model.index(row, col);
3735 model.setData(idx, QVariant(row), Qt::EditRole);
3739 tv.setModel(&model);
3741 tv.verticalHeader()->swapSections(0, model.rowCount() - 1);
3742 tv.setCurrentIndex(model.index(model.rowCount() - 1, 0));
3744 QVERIFY(QTest::qWaitForWindowExposed(&tv));
3745 QTest::keyClick(&tv, Qt::Key_PageUp); // PageUp won't scroll when at top
3746 QTRY_COMPARE(tv.rowAt(0), tv.verticalHeader()->logicalIndex(0));
3748 int newRow = tv.rowAt(tv.viewport()->height());
3749 if (newRow == tv.rowAt(tv.viewport()->height() - 1)) // Overlapping row
3751 QTest::keyClick(&tv, Qt::Key_PageDown); // Scroll down and check current
3752 QTRY_COMPARE(tv.currentIndex().row(), newRow);
3754 tv.setCurrentIndex(model.index(0, 0));
3756 QTest::keyClick(&tv, Qt::Key_PageDown); // PageDown won't scroll when at the bottom
3757 QTRY_COMPARE(tv.rowAt(tv.viewport()->height() - 1), tv.verticalHeader()->logicalIndex(model.rowCount() - 1));
3760 template <typename T>
3763 ValueSaver(T &v) : var(v), value(v) { }
3764 ~ValueSaver() { var = value; }
3767 void tst_QTableView::task191545_dragSelectRows()
3769 QStandardItemModel model(10, 10);
3771 table.setModel(&model);
3772 table.setSelectionBehavior(QAbstractItemView::SelectItems);
3773 table.setSelectionMode(QAbstractItemView::ExtendedSelection);
3774 table.setMinimumSize(1000, 400);
3778 ValueSaver<Qt::KeyboardModifiers> saver(QApplicationPrivate::modifier_buttons);
3779 QApplicationPrivate::modifier_buttons = Qt::ControlModifier;
3782 QRect cellRect = table.visualRect(model.index(3, 0));
3783 QHeaderView *vHeader = table.verticalHeader();
3784 QWidget *vHeaderVp = vHeader->viewport();
3785 QPoint rowPos(cellRect.center());
3786 QMouseEvent rowPressEvent(QEvent::MouseButtonPress, rowPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3787 qApp->sendEvent(vHeaderVp, &rowPressEvent);
3789 for (int i = 0; i < 4; ++i) {
3790 rowPos.setY(rowPos.y() + cellRect.height());
3791 QMouseEvent moveEvent(QEvent::MouseMove, rowPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
3792 qApp->sendEvent(vHeaderVp, &moveEvent);
3794 QMouseEvent rowReleaseEvent(QEvent::MouseButtonRelease, rowPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3795 qApp->sendEvent(vHeaderVp, &rowReleaseEvent);
3797 for (int i = 0; i < 4; ++i) {
3798 QModelIndex index = model.index(3 + i, 0, table.rootIndex());
3799 QVERIFY(vHeader->selectionModel()->selectedRows().contains(index));
3804 QRect cellRect = table.visualRect(model.index(0, 3));
3805 QHeaderView *hHeader = table.horizontalHeader();
3806 QWidget *hHeaderVp = hHeader->viewport();
3807 QPoint colPos((cellRect.left() + cellRect.right()) / 2, 5);
3808 QMouseEvent colPressEvent(QEvent::MouseButtonPress, colPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3809 qApp->sendEvent(hHeaderVp, &colPressEvent);
3811 for (int i = 0; i < 4; ++i) {
3812 colPos.setX(colPos.x() + cellRect.width());
3813 QMouseEvent moveEvent(QEvent::MouseMove, colPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
3814 qApp->sendEvent(hHeaderVp, &moveEvent);
3816 QMouseEvent colReleaseEvent(QEvent::MouseButtonRelease, colPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3817 qApp->sendEvent(hHeaderVp, &colReleaseEvent);
3819 for (int i = 0; i < 4; ++i) {
3820 QModelIndex index = model.index(0, 3 + i, table.rootIndex());
3821 QVERIFY(hHeader->selectionModel()->selectedColumns().contains(index));
3826 QRect cellRect = table.visualRect(model.index(2, 2));
3827 QWidget *tableVp = table.viewport();
3828 QPoint cellPos = cellRect.center();
3829 QMouseEvent cellPressEvent(QEvent::MouseButtonPress, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3830 qApp->sendEvent(tableVp, &cellPressEvent);
3832 for (int i = 0; i < 6; ++i) {
3833 cellPos.setX(cellPos.x() + cellRect.width());
3834 cellPos.setY(cellPos.y() + cellRect.height());
3835 QMouseEvent moveEvent(QEvent::MouseMove, cellPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
3836 qApp->sendEvent(tableVp, &moveEvent);
3838 QMouseEvent cellReleaseEvent(QEvent::MouseButtonRelease, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3839 qApp->sendEvent(tableVp, &cellReleaseEvent);
3841 for (int i = 0; i < 6; ++i)
3842 for (int j = 0; j < 6; ++j) {
3843 QModelIndex index = model.index(2 + i, 2 + j, table.rootIndex());
3844 QVERIFY(table.selectionModel()->isSelected(index));
3849 QRect cellRect = table.visualRect(model.index(3, 3));
3850 QWidget *tableVp = table.viewport();
3851 QPoint cellPos = cellRect.center();
3852 QMouseEvent cellPressEvent(QEvent::MouseButtonPress, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3853 qApp->sendEvent(tableVp, &cellPressEvent);
3855 for (int i = 0; i < 6; ++i) {
3856 cellPos.setX(cellPos.x() + cellRect.width());
3857 cellPos.setY(cellPos.y() + cellRect.height());
3858 QMouseEvent moveEvent(QEvent::MouseMove, cellPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
3859 qApp->sendEvent(tableVp, &moveEvent);
3861 QMouseEvent cellReleaseEvent(QEvent::MouseButtonRelease, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
3862 qApp->sendEvent(tableVp, &cellReleaseEvent);
3865 for (int i = 0; i < 6; ++i)
3866 for (int j = 0; j < 6; ++j) {
3867 QModelIndex index = model.index(3 + i, 3 + j, table.rootIndex());
3868 QVERIFY(!table.selectionModel()->isSelected(index));
3873 void tst_QTableView::task234926_setHeaderSorting()
3875 QStringListModel model;
3877 data << "orange" << "apple" << "banana" << "lemon" << "pumpkin";
3878 QStringList sortedDataA = data;
3879 QStringList sortedDataD = data;
3881 qSort(sortedDataD.begin(), sortedDataD.end(), qGreater<QString>());
3882 model.setStringList(data);
3884 view.setModel(&model);
3887 QCOMPARE(model.stringList(), data);
3888 view.setSortingEnabled(true);
3889 view.sortByColumn(0, Qt::AscendingOrder);
3890 QApplication::processEvents();
3891 QCOMPARE(model.stringList() , sortedDataA);
3893 view.horizontalHeader()->setSortIndicator(0, Qt::DescendingOrder);
3894 QApplication::processEvents();
3895 QCOMPARE(model.stringList() , sortedDataD);
3897 QHeaderView *h = new QHeaderView(Qt::Horizontal);
3898 h->setModel(&model);
3899 view.setHorizontalHeader(h);
3900 h->setSortIndicator(0, Qt::AscendingOrder);
3901 QApplication::processEvents();
3902 QCOMPARE(model.stringList() , sortedDataA);
3904 h->setSortIndicator(0, Qt::DescendingOrder);
3905 QApplication::processEvents();
3906 QCOMPARE(model.stringList() , sortedDataD);
3909 void tst_QTableView::taskQTBUG_5062_spansInconsistency()
3911 const int nRows = 5;
3912 const int nColumns = 5;
3914 QtTestTableModel model(nRows, nColumns);
3915 QtTestTableView view;
3916 view.setModel(&model);
3918 for (int i = 0; i < nRows; ++i)
3919 view.setSpan(i, 0, 1, nColumns);
3920 view.setSpan(2, 0, 1, 1);
3921 view.setSpan(3, 0, 1, 1);
3923 VERIFY_SPANS_CONSISTENCY(&view);
3926 void tst_QTableView::taskQTBUG_4516_clickOnRichTextLabel()
3929 QStandardItemModel model(5,5);
3930 view.setModel(&model);
3931 QLabel label("rich text");
3932 label.setTextFormat(Qt::RichText);
3933 view.setIndexWidget(model.index(1,1), &label);
3934 view.setCurrentIndex(model.index(0,0));
3935 QCOMPARE(view.currentIndex(), model.index(0,0));
3937 QTest::mouseClick(&label, Qt::LeftButton);
3938 QCOMPARE(view.currentIndex(), model.index(1,1));
3944 void tst_QTableView::changeHeaderData()
3947 QStandardItemModel model(5,5);
3948 view.setModel(&model);
3950 QVERIFY(QTest::qWaitForWindowExposed(&view));
3952 QString text = "long long long text";
3953 const int textWidth = view.verticalHeader()->fontMetrics().width(text);
3954 QVERIFY(view.verticalHeader()->width() < textWidth);
3956 model.setHeaderData(2, Qt::Vertical, text);
3957 QTest::qWait(100); //leave time for layout
3959 QVERIFY(view.verticalHeader()->width() > textWidth);
3962 void tst_QTableView::taskQTBUG_5237_wheelEventOnHeader()
3965 QStandardItemModel model(500,5);
3966 view.setModel(&model);
3968 QVERIFY(QTest::qWaitForWindowExposed(&view));
3970 int sbValueBefore = view.verticalScrollBar()->value();
3971 QHeaderView *header = view.verticalHeader();
3972 QTest::mouseMove(header);
3973 QWheelEvent wheelEvent(header->geometry().center(), -720, 0, 0);
3974 QApplication::sendEvent(header->viewport(), &wheelEvent);
3975 int sbValueAfter = view.verticalScrollBar()->value();
3976 QVERIFY(sbValueBefore != sbValueAfter);
3979 class TestTableView : public QTableView {
3982 TestTableView(QWidget *parent = 0) : QTableView(parent)
3984 connect(this, SIGNAL(entered(const QModelIndex&)), this, SLOT(openEditor(const QModelIndex&)));
3988 void onDataChanged()
3990 for (int i = 0; i < model()->rowCount(); i++) {
3991 setRowHidden(i, model()->data(model()->index(i, 0)).toBool());
3995 void openEditor(const QModelIndex& index)
3996 { openPersistentEditor(index); }
4000 void tst_QTableView::taskQTBUG_8585_crashForNoGoodReason()
4002 QStandardItemModel model;
4003 model.insertColumn(0, QModelIndex());
4004 for(int i = 0; i < 20; i++)
4010 w.setMouseTracking(true);
4012 connect(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), &w, SLOT(onDataChanged()));
4014 QVERIFY(QTest::qWaitForWindowExposed(&w));
4015 for (int i = 0; i < 10; i++)
4017 QTest::mouseMove(w.viewport(), QPoint(50, 20));
4018 w.model()->setData(w.indexAt(QPoint(50, 20)), true);
4019 QTest::mouseMove(w.viewport(), QPoint(50, 25));
4023 class TableView7774 : public QTableView
4026 QRegion visualRegionForSelection(const QItemSelection &selection) const
4028 return QTableView::visualRegionForSelection(selection);
4032 void tst_QTableView::taskQTBUG_7774_RtoLVisualRegionForSelection()
4035 QStandardItemModel model(5,5);
4036 view.setModel(&model);
4037 view.setLayoutDirection(Qt::RightToLeft);
4039 QVERIFY(QTest::qWaitForWindowExposed(&view));
4041 QItemSelectionRange range(model.index(2, 0), model.index(2, model.columnCount() - 1));
4042 QItemSelection selection;
4044 QRegion region = view.visualRegionForSelection(selection);
4045 QCOMPARE(region.rects().at(0), view.visualRect(range.topLeft()) | view.visualRect(range.bottomRight()));
4048 void tst_QTableView::taskQTBUG_8777_scrollToSpans()
4050 QTableWidget table(75,5);
4051 for (int i=0; i<50; i++)
4052 table.setSpan(2+i, 0, 1, 5);
4053 table.setCurrentCell(0,2);
4056 for (int i = 0; i < 45; ++i)
4057 QTest::keyClick(&table, Qt::Key_Down);
4059 QVERIFY(table.verticalScrollBar()->value() > 10);
4062 void tst_QTableView::taskQTBUG_10169_sizeHintForRow()
4064 QtTestTableView tableView;
4065 QStandardItemModel model(1, 3);
4066 model.setData(model.index(0, 0), "Word wrapping text goes here.");
4067 tableView.setModel(&model);
4068 tableView.verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
4069 const int orderedHeight = tableView.sizeHintForRow(0);
4070 tableView.horizontalHeader()->moveSection(2, 0);
4071 const int reorderedHeight = tableView.sizeHintForRow(0);
4073 //the order of the columns shouldn't matter.
4074 QCOMPARE(orderedHeight, reorderedHeight);
4077 QTEST_MAIN(tst_QTableView)
4078 #include "tst_qtableview.moc"