[Kde-games-devel] A QListView programming problem

Ian Wadham iandw.au at gmail.com
Sun Oct 30 21:29:29 UTC 2011


On 30/10/2011, at 10:02 PM, Albert Astals Cid wrote:
> A Diumenge, 30 d'octubre de 2011, Ian Wadham vàreu escriure:
>> Hi guys,
>> 
>> In KSudoku the WelcomeScreen contains a list of puzzle types and
>> sizes from which the user can select.  I would like to be able to save
>> and restore this selection between sessions, using KConfigGroup.
>> 
>> I thought I could just save and restore an integer row-index, but no
>> such luck.  The list is displayed in a QListView, which is declared in
>> code generated from a *.ui file created by Designer.  In QListView the
>> list is accessed by using QListView::selectionModel()->currentIndex()
>> to obtain a QModelIndex object and then that index-object is used to
>> retrieve a row from the underlying data collection.  IOW there seems
>> to be no easy way to find out where the cursor and highlighting are.
> 
> Not sure i understand what your problem is, can't you just save the row() of 
> the QModelIndex returned by QListView::selectionModel()->currentIndex() ?

True, I believe you can do int modelIndex.row(), int modelIndex.column() and
QModelIndex modelIndex..parent() to get the row, column and parent and in
a QListView the column should be zero and the parent an empty object, so
neither of those would need to be saved in the config file.

I got lost on the return trip and pored over doco for half a day trying to work
out how to put the model index, cursor, selection and highlighting back as
they were.  There is a QModelIndex QAbstractItemModel::index (int row,
int column, QModelIndex & parent = QModelIndex()) function, but it is pure
virtual and I could not find out if the chain of classes leading to QListView
re-defines it.  Then again, the doco utters dark words about not using index()
and it being advisable to use createIndex() from somewhere else and I
could not find out if QListView inherits a createIndex().

Finally I am not sure how to put the QModelIndex back in and change the view,
i.e. what appears on the screen.

I think you use QItemSelectionModel::setCurrentIndex() but how?  What
lower-level class or object inherits/implements that?  And which of the many
"command" options should I use?  Maybe QItemSelectionModel::SelectCurrent.

Another thing I could not find in the Qt 4.7.3 doco was an inheritance list or
inheritance diagram.  I tried backtracking from the list of All Functions, but
just got back to where I came from in the doco.

Cheers, Ian W..
> 
>> 
>> I do not think you can save an index-object in KConfigGroup.  I googled
>> about on this, but it seems that there is no easy answer for finding
>> where the cursor and selection highlighting is in a QListView and then
>> resetting that position on a later occasion.
>> 
>> The best way seems to be to remember one or two attributes of the data
>> and, when restoring, search the entire QListView data, find the item you
>> want (e.g. by its attribute values) and set the selection there.
>> 
>> Does anybody know a better way?
>> 
>> All the best, Ian W.
>> 
>> P.S. KSudoku's list is always displayed in the same order.



More information about the kde-games-devel mailing list