3783 QCOMPARE(view.currentIndex(), model.index(12, 0)); |
3784 QCOMPARE(view.currentIndex(), model.index(12, 0)); |
3784 QTest::keyClick(view.viewport(), Qt::Key_Up); |
3785 QTest::keyClick(view.viewport(), Qt::Key_Up); |
3785 QCOMPARE(view.currentIndex(), model.index(6, 0)); |
3786 QCOMPARE(view.currentIndex(), model.index(6, 0)); |
3786 } |
3787 } |
3787 |
3788 |
|
3789 class Model_11466 : public QAbstractItemModel |
|
3790 { |
|
3791 Q_OBJECT |
|
3792 public: |
|
3793 Model_11466(QObject *parent) : |
|
3794 m_block(false) |
|
3795 { |
|
3796 // set up the model to have two top level items and a few others |
|
3797 m_selectionModel = new QItemSelectionModel(this, this); // owned by this |
|
3798 |
|
3799 connect(m_selectionModel, SIGNAL(currentChanged(const QModelIndex &,const QModelIndex &)), |
|
3800 this, SLOT(slotCurrentChanged(const QModelIndex &,const QModelIndex &))); |
|
3801 }; |
|
3802 |
|
3803 int rowCount(const QModelIndex &parent) const |
|
3804 { |
|
3805 if (parent.isValid()) |
|
3806 return (parent.internalId() == 0) ? 4 : 0; |
|
3807 return 2; // two top level items |
|
3808 } |
|
3809 |
|
3810 int columnCount(const QModelIndex &parent) const |
|
3811 { |
|
3812 return 2; |
|
3813 } |
|
3814 |
|
3815 QVariant data(const QModelIndex &index, int role) const |
|
3816 { |
|
3817 if (role == Qt::DisplayRole && index.isValid()) { |
|
3818 qint64 parentRowPlusOne = index.internalId(); |
|
3819 QString str; |
|
3820 QTextStream stream(&str); |
|
3821 if (parentRowPlusOne > 0) |
|
3822 stream << parentRowPlusOne << " -> " << index.row() << " : " << index.column(); |
|
3823 else |
|
3824 stream << index.row() << " : " << index.column(); |
|
3825 return QVariant(str); |
|
3826 } |
|
3827 return QVariant(); |
|
3828 } |
|
3829 |
|
3830 QModelIndex parent(const QModelIndex &index) const |
|
3831 { |
|
3832 if (index.isValid()) { |
|
3833 qint64 parentRowPlusOne = index.internalId(); |
|
3834 if (parentRowPlusOne > 0) { |
|
3835 int row = static_cast<int>(parentRowPlusOne - 1); |
|
3836 return createIndex(row, 0, (quint32)0); |
|
3837 } |
|
3838 } |
|
3839 return QModelIndex(); |
|
3840 } |
|
3841 |
|
3842 void bindView(QTreeView *view) |
|
3843 { |
|
3844 // sets the view to this model with a shared selection model |
|
3845 QItemSelectionModel *oldModel = view->selectionModel(); |
|
3846 if (oldModel != m_selectionModel) |
|
3847 delete oldModel; |
|
3848 view->setModel(this); // this creates a new selection model for the view, but we dont want it either ... |
|
3849 oldModel = view->selectionModel(); |
|
3850 view->setSelectionModel(m_selectionModel); |
|
3851 delete oldModel; |
|
3852 } |
|
3853 |
|
3854 QModelIndex index(int row, int column, const QModelIndex &parent) const |
|
3855 { |
|
3856 return createIndex(row, column, parent.isValid() ? (quint32)(parent.row() + 1) : (quint32)0); |
|
3857 } |
|
3858 |
|
3859 public slots: |
|
3860 void slotCurrentChanged(const QModelIndex ¤t,const QModelIndex &) |
|
3861 { |
|
3862 if (m_block) |
|
3863 return; |
|
3864 |
|
3865 if (current.isValid()) { |
|
3866 int selectedRow = current.row(); |
|
3867 quint32 parentRowPlusOne = static_cast<quint32>(current.internalId()); |
|
3868 |
|
3869 for (int i = 0; i < 2; ++i) { |
|
3870 // announce the removal of all non top level items |
|
3871 beginRemoveRows(createIndex(i, 0, 0), 0, 3); |
|
3872 // nothing to actually do for the removal |
|
3873 endRemoveRows(); |
|
3874 |
|
3875 // put them back in again |
|
3876 beginInsertRows(createIndex(i, 0, 0), 0, 3); |
|
3877 // nothing to actually do for the insertion |
|
3878 endInsertRows(); |
|
3879 } |
|
3880 // reselect the current item ... |
|
3881 QModelIndex selectedIndex = createIndex(selectedRow, 0, parentRowPlusOne); |
|
3882 |
|
3883 m_block = true; // recursion block |
|
3884 m_selectionModel->select(selectedIndex, QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Current|QItemSelectionModel::Rows); |
|
3885 m_selectionModel->setCurrentIndex(selectedIndex, QItemSelectionModel::NoUpdate); |
|
3886 m_block = false; |
|
3887 } else { |
|
3888 m_selectionModel->clear(); |
|
3889 } |
|
3890 } |
|
3891 |
|
3892 private: |
|
3893 bool m_block; |
|
3894 QItemSelectionModel *m_selectionModel; |
|
3895 }; |
|
3896 |
|
3897 void tst_QTreeView::taskQTBUG_11466_keyboardNavigationRegression() |
|
3898 { |
|
3899 QTreeView treeView; |
|
3900 treeView.setSelectionBehavior(QAbstractItemView::SelectRows); |
|
3901 treeView.setSelectionMode(QAbstractItemView::SingleSelection); |
|
3902 Model_11466 model(&treeView); |
|
3903 model.bindView(&treeView); |
|
3904 treeView.expandAll(); |
|
3905 treeView.show(); |
|
3906 QTest::qWaitForWindowShown(&treeView); |
|
3907 |
|
3908 QTest::keyPress(treeView.viewport(), Qt::Key_Down); |
|
3909 QTest::qWait(10); |
|
3910 QTRY_COMPARE(treeView.currentIndex(), treeView.selectionModel()->selection().indexes().first()); |
|
3911 } |
|
3912 |
3788 QTEST_MAIN(tst_QTreeView) |
3913 QTEST_MAIN(tst_QTreeView) |
3789 #include "tst_qtreeview.moc" |
3914 #include "tst_qtreeview.moc" |