Signal when a QListView selection changes due to keyboard activity?

允我心安 提交于 2019-11-30 08:46:50

The activated(QModelIndex) signal actually refers to something more than just the act of selecting. The concept is rather vague, but it's more like an act of explicit choosing. If you're just looking for notification that the current selection has changed, you can grab the selection model and connect to its updates.

MyView::MyView() {
   QListView* view = new QListView(this);
   connect(view->selectionModel(), 
      SIGNAL(selectionChanged(QItemSelection,QItemSelection)), 
      this, SLOT(handleSelectionChanged(QItemSelection)));
}

...

MyView::handleSelectionChanged(const QItemSelection& selection){
   if(selection.indexes().isEmpty()) {
      clearMyView();
   } else {
      displayModelIndexInMyView(selection.indexes().first());
   }
}

In the code above, displayModelIndexInMyView(QModelIndex) should be replaced with your current handler slot for activated(QModelIndex), and clearMyView() replaced with whatever it is that you want to do when there's nothing selected.

There's a lot of ways to do this, and honestly I'm not sure what is the canonical one, but I think this will work for you.

The other way is to implement QListView::currentChanged(...) virtual function.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!