如何让选择模型与代理模型一起使用?

发布于 2024-07-05 19:27:01 字数 303 浏览 3 评论 0原文

我有一个模型和两个视图,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-07-12 19:27:01

可能发生的情况是视图确实有两种不同的模型。 一个是您的原始模型,另一个是排序过滤器模型。

我不确定这是否有效,这取决于 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.

晒暮凉 2024-07-12 19:27:01

您可能需要将 void QItemSelectionModel::select 与 < a href="http://doc.qt.nokia.com/latest/qabstractproxymodel.html#mapSelectionFromSource" rel="nofollow">QAbstractProxyModel::mapSelectionFromSource 和 QAbstractProxyModel::mapSelectionToSource。 在 QListView 的 SelectionChange 信号处理程序中,您应该具有

tableView->selection()->select(
    proxyModel->mapSelectionToSource(selected),
    QItemSelectionModel::ClearAndSelect);

与 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

tableView->selection()->select(
    proxyModel->mapSelectionToSource(selected),
    QItemSelectionModel::ClearAndSelect);

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...

病毒体 2024-07-12 19:27:01

不太确定您的模型子类是如何实现的 - 但选择取决于持久模型索引是否正确。 你能提供一些源代码吗? 您在两者上使用相同的选择模型吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文