{"id":100,"date":"2007-03-14T19:45:00","date_gmt":"2007-03-14T17:45:00","guid":{"rendered":"http:\/\/www.thelins.se\/johan\/blog\/2007\/03\/selecting\/"},"modified":"2007-03-14T19:45:00","modified_gmt":"2007-03-14T17:45:00","slug":"selecting","status":"publish","type":"post","link":"https:\/\/www.thelins.se\/johan\/blog\/2007\/03\/selecting\/","title":{"rendered":">Selecting"},"content":{"rendered":"<p>>I ran into something peculiar today when using the Model\/View classes in Qt 4.2 &#8211; probably something related to <a href=\"http:\/\/www.trolltech.com\/developer\/task-tracker\/index_html?method=entry&#038;id=143383\">TaskTracker issue 143383<\/a>. To demonstrate, let&#8217;s start with a minimal model that can be reset from a public method:<\/p>\n<pre>class MyStringListModel : public QStringListModel<br \/>{<br \/>public:<br \/>MyStringListModel( const QStringList &amp;items ) : QStringListModel( items, 0 ) {}<br \/><br \/>void resetModel() { reset(); }<br \/>};<\/pre>\n<p>I start by initializing the model and viewing it through a QListView:<\/p>\n<pre>  QListView listView;<br \/>  QStringList items;<br \/>  items << \"Kalle\" << \"Olle\" << \"Albert\" << \"Sven\" << \"Anders\" << \"Markus\";<br \/>  MyStringListModel model( items );<br \/>  listView.setModel( &model );<\/pre>\n<p>Then I set the current index and select it &#8211; this is equvalent to picking it with the mouse (that was how I ran into the issue):<\/p>\n<pre>  listView.selectionModel()->setCurrentIndex(<br \/>    model.index( 3, 0, QModelIndex() ),<br \/>    QItemSelectionModel::SelectCurrent );<\/pre>\n<p>This gives us the following state:<\/p>\n<pre>  listView.selectionModel()->hasSelection() == true<br \/>  listView.selectionModel()->selectedIndexes().count() == 1<br \/>  listView.selectionModel()->currentIndex().isValue == true<\/pre>\n<p>Now, I reset the model:<\/p>\n<pre>  model.resetModel();<\/pre>\n<p>The situation changes to:<\/p>\n<pre>  listView.selectionModel()->hasSelection() == <span style=\"font-weight: bold;\">true<\/span><br \/>  listView.selectionModel()->selectedIndexes().count() == 0<br \/>  listView.selectionModel()->currentIndex().isValue == false<\/pre>\n<p>The same thing happens when using select instead of setCurrentIndex. When using the following line&#8230;<\/p>\n<pre>  listView.selectionModel()->select(<br \/>    model.index( 3, 0, QModelIndex() ),<br \/>    QItemSelectionModel::SelectCurrent );<\/pre>\n<p>&#8230; you get the following:<\/p>\n<pre>  listView.selectionModel()->hasSelection() == true<br \/>  listView.selectionModel()->selectedIndexes().count() == 1<br \/>  listView.selectionModel()->currentIndex().isValue == false<\/pre>\n<p>And after resetting the model it looks like this:<\/p>\n<pre>  listView.selectionModel()->hasSelection() == <span style=\"font-weight: bold;\">true<\/span><br \/>  listView.selectionModel()->selectedIndexes().count() == 0<br \/>  listView.selectionModel()->currentIndex().isValue == false<\/pre>\n<p>The TaskTracker entry says that the bug is fixed, so this is likely to have been fixed in the Qt 4.3 snapshots. If you&#8217;re using an earlier release it looks as if the <tt>selectedIndexes().count()<\/tt> is a better way to determine if something is selected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>>I ran into something peculiar today when using the Model\/View classes in Qt 4.2 &#8211; probably something related to TaskTracker issue 143383. To demonstrate, let&#8217;s start with a minimal model that can be reset from a public method: class MyStringListModel : public QStringListModel{public:MyStringListModel( const QStringList &amp;items ) : QStringListModel( items, 0 ) {}void resetModel() { &hellip; <a href=\"https:\/\/www.thelins.se\/johan\/blog\/2007\/03\/selecting\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">>Selecting<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":0,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}