static QStandardItemModel* buildModel(QWidget* parent) { QStandardItemModel* model = new QStandardItemModel(parent); model->setColumnCount(5); for (int i = 0; i < 8; ++i) { QList row; for (int c = 0; c < 5; ++c) { row.append(new QStandardItem(QString("%1 %2").arg(i).arg(c))); } model->insertRow(model->rowCount(), row); } BOOST_REQUIRE_EQUAL(5, model->columnCount()); BOOST_REQUIRE_EQUAL(8, model->rowCount()); return model; } BOOST_AUTO_TEST_CASE(testTableView) { // test the persistence QString headerState; { QTableView view; view.setSortingEnabled(true); QStandardItemModel* model = buildModel(&view); QSortFilterProxyModel* proxy = new QSortFilterProxyModel(&view); proxy->setSourceModel(model); view.setModel(proxy); BOOST_REQUIRE_EQUAL(proxy, view.model()); BOOST_REQUIRE_EQUAL(5, proxy->columnCount()); QHeaderView* header = view.horizontalHeader(); BOOST_REQUIRE(header); header->moveSection(2, 1); header->setSortIndicator(1, Qt::DescendingOrder); header->resizeSection(3, 20); header->setSectionHidden(3, true); BOOST_REQUIRE_EQUAL(1, header->visualIndex(2)); BOOST_REQUIRE_EQUAL(1, header->sortIndicatorSection()); BOOST_REQUIRE_EQUAL(Qt::DescendingOrder, header->sortIndicatorOrder()); BOOST_REQUIRE(header->isSectionHidden(3)); headerState = header->saveState().toHex(); } { QTableView res_view; res_view.setSortingEnabled(true); QStandardItemModel* res_model = buildModel(&res_view); QSortFilterProxyModel* res_proxy = new QSortFilterProxyModel(&res_view); res_proxy->setSourceModel(res_model); res_view.setModel(res_proxy); BOOST_REQUIRE_EQUAL(res_proxy, res_view.model()); BOOST_REQUIRE_EQUAL(5, res_proxy->columnCount()); QHeaderView* res_header = res_view.horizontalHeader(); BOOST_REQUIRE(res_header); res_header->restoreState(QByteArray::fromHex(headerState.toLatin1())); BOOST_REQUIRE_EQUAL(1, res_header->visualIndex(2)); BOOST_REQUIRE_EQUAL(1, res_header->sortIndicatorSection()); BOOST_REQUIRE_EQUAL(Qt::DescendingOrder, res_header->sortIndicatorOrder()); BOOST_REQUIRE(res_header->isSectionHidden(3)); QString res_state = res_header->saveState().toHex(); BOOST_CHECK(headerState == res_state); // BROKEN QT 5.9.4: the size of the hidden section was reset to // 0 when the sortIndicator was changed // QHeaderViewPrivate::_q_layoutChanged() rebuilds hiddenSectionSize // and resets all stored sizes to 0 } { // test hidden section size QTableView view; view.setSortingEnabled(true); QStandardItemModel* model = buildModel(&view); QSortFilterProxyModel* proxy = new QSortFilterProxyModel(&view); proxy->setSourceModel(model); view.setModel(proxy); BOOST_REQUIRE_EQUAL(proxy, view.model()); BOOST_REQUIRE_EQUAL(5, proxy->columnCount()); QHeaderView* header = view.horizontalHeader(); BOOST_REQUIRE(header); header->moveSection(2, 1); header->setSortIndicator(1, Qt::DescendingOrder); header->resizeSection(3, 20); BOOST_REQUIRE_EQUAL(20, header->sectionSize(3)); header->setSectionHidden(3, true); BOOST_REQUIRE_EQUAL(0, header->sectionSize(3)); header->setSectionHidden(3, false); BOOST_REQUIRE_EQUAL(20, header->sectionSize(3)); header->setSectionHidden(3, true); BOOST_REQUIRE_EQUAL(0, header->sectionSize(3)); header->setSortIndicator(2, Qt::DescendingOrder); header->setSectionHidden(3, false); BOOST_REQUIRE_EQUAL(20, header->sectionSize(3)); // BROKEN QT 5.9.4: the size of the hidden section was reset to // 0 when the sortIndicator was changed // QHeaderViewPrivate::_q_layoutChanged() rebuilds hiddenSectionSize // and resets all stored sizes to 0 } }