如何让选择模型与代理模型一起使用?
我有一个模型和两个视图,如下所示:
Model ---> OSortFilterProxyModel ---> OListView
Model ------------------------------> OTableView
当用户在其中一个视图中选择某些内容时,我希望另一个视图镜像该选择。 所以我想我应该使用 QSelectionModel 将它们链接在一起。 但这是行不通的。 我有一种感觉,这是因为观点认为他们有两个不同的模型,而实际上他们有相同的模型。 有办法让它发挥作用吗?
I have a model and two views set up like this:
Model ---> OSortFilterProxyModel ---> OListView
Model ------------------------------> OTableView
When the user selects something in one of the views, I want the other view to mirror that selection. So I thought I'd use a QSelectionModel to link them together. But this does not work. I have a feeling it is because the views think they have two different models, when in fact they have the same model. Is there a way to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能发生的情况是视图确实有两种不同的模型。 一个是您的原始模型,另一个是排序过滤器模型。
我不确定这是否有效,这取决于 Qt 认为“激活”的内容,但您可以将一个函数连接到每个视图的激活插槽。 这些将向您传递一个模型索引。 您必须通过代理模型以适当的方向(mapFromSource 和 mapToSource)发送模型索引。 然后,在另一个视图上调用 setCurrentIndex。
激活信号的文档指出,“激活”的含义因平台而异。 您可能还可以锁定其他信号,例如选择模型的选择更改信号。 您可能需要执行不同的调用才能更改用户所看到的选择。 最后,只要您记住与源模型的映射,就可以在派生的 QSelectionModel 中完成甚至更容易。
What is probably happening is that the views do have two different models. One is your original model, the other is the sort filter model.
I'm not sure if this would work, and it depends on what Qt considers "activated", but you could connect a function to each of the view's activated slots. These will pass you a model index. You'll have to send the model index through the proxy model in the appropriate direction (mapFromSource and mapToSource). Then, call the setCurrentIndex on the other view.
The documentation for the activated signal states that what is considered "activated" varies by platform. There might be other signals you could latch onto, such as the selection model's selection changed signal. You might have to do a different call to change the selection as seen by the user. And finally, it might be possible or even easier to do in a derived QSelectionModel, as long as you remember about mapping to/from the source model.
您可能需要将 void QItemSelectionModel::select 与 < a href="http://doc.qt.nokia.com/latest/qabstractproxymodel.html#mapSelectionFromSource" rel="nofollow">QAbstractProxyModel::mapSelectionFromSource 和 QAbstractProxyModel::mapSelectionToSource。 在 QListView 的 SelectionChange 信号处理程序中,您应该具有
与 QTableView 的 signalChange 信号处理程序中的 mapSelectionFromSource 类似的功能。
请注意,我不确定当表更改列表的选择时 Qt 是否会阻止无限递归,而列表的选择又会更改表的选择等等......
You propably need to use void QItemSelectionModel::select combined with QAbstractProxyModel::mapSelectionFromSource and QAbstractProxyModel::mapSelectionToSource. In QListView's selectionChange signal handler you should have
and analogically with mapSelectionFromSource in QTableView's signalChange signal handler.
Note that i am not sure if Qt will prevent infinite recursion when table will change selection of list which in turn will change selection of table and so on...
不太确定您的模型子类是如何实现的 - 但选择取决于持久模型索引是否正确。 你能提供一些源代码吗? 您在两者上使用相同的选择模型吗?
Not quite sure how your model subclass is implemented - but the selection depends on persistent model indexes being correct. Can you provide some source code? Are you using the same selection model on both?