信号何时QListView选择由于键盘活动而改变? [英] Signal when a QListView selection changes due to keyboard activity?

查看:372
本文介绍了信号何时QListView选择由于键盘活动而改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用QT Designer创建的QDialog,看起来像这样:

I have a QDialog, created with QT Designer, that looks like so:

左侧的服务器列表是带有QStringListModel的QListView.鼠标单击列表视图中的项目,方法是将视图的激活的(QModelIndex)信号连接到对话框中的插槽功能,从而用所选项目的信息更新表单.

The list of servers on the left is a QListView with a QStringListModel. Mouse clicking an item in the list view updates the form with the information for the selected item by connecting the view’s activated(QModelIndex) signal to a slot function in the dialog.

但是,向上或向下按下键盘也可以更改所选项目,但是不会发出信号,因此该表格不会更新为与所选项目匹配.该如何解决?

However, pressing up or down on the keyboard also changes the selected item, but no signal is emitted, so the form isn't updated to match the selected item. How can this be fixed?

推荐答案

activated(QModelIndex)信号实际上所指的不仅仅是选择行为.这个概念比较模糊,但是更像是明确选择的行为.如果您只是在寻找当前选择已更改的通知,则可以获取选择模型并连接其更新.

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());
   }
}

在上面的代码中,displayModelIndexInMyView(QModelIndex)应该替换为当前的activated(QModelIndex)处理槽,而clearMyView()应该替换为在没有任何选择的情况下想要执行的操作.

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.

这篇关于信号何时QListView选择由于键盘活动而改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆