gpt4 book ai didi

c++ - QListWidget 中的 setSelectionModel 停止发射 itemSelectionChanged 信号

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:37 26 4
gpt4 key购买 nike

在我的 QListWidget 子类中,当我使用 setSelectionModel 替换它的 QItemSelectionModel 时,列表小部件停止发出它的 itemSelectionChanged 信号。在实际代码中,我这样做是为了安装 QItemSelectionModel 的子类,但即使我只使用新的 QItemSelectionModel 也会出现问题。

MyListWidget::MyListWidget(QWidget* parent)
: QListWidget(parent)
{
QItemSelectionModel* oldSelectionModel = selectionModel();
QItemSelectionModel* newSelectionModel = new QItemSelectionModel(model(), oldSelectionModel->parent());
setSelectionModel(newSelectionModel);
oldSelectionModel->deleteLater();
}

在调试器中,我可以看到 QItemSelectionModel 发出其 selectionChanged 信号,但 QListWidget 从未发出其对应的 itemSelectionChanged 信号。

如果我注释掉这个构造函数的主体,一切正常。和以前一样,我可以看到 QItemSelectionModel 发出它的 selectionChanged 信号,但现在触发 QListWidget 发出 itemSelectionChanged。

看起来我对 setSelectionModel 的调用导致 QItemSelectionModel::selectionChanged 和 QListWidget::itemSelectionChanged 之间的连接中断。

顺便说一句,我检查了我是否将错误的参数传递给了我的新 QItemSelectionModel。我创建后,newSelectionModel 的model() 和parent() 与oldSelectionModel 完全相同。我还尝试注释掉对 deleteLater 的调用,但这没有帮助。

非常感谢任何帮助。

最佳答案

根据 QAbstractItemViewQt 文档,setSelectionModel 替换了当前的选择模型,如果 setModel( ) 在此之后调用。

Note that, if you call setModel() after this function, the given selectionModel will be replaced by one created by the view.

此外,从 QListWidget 文档中,

QListWidget is a convenience class that provides a list view similar to the one supplied by QListView, but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list.

看起来每当您添加项目时,模型都会更新/刷新,从而使您的自定义选择模型无效(只是我的想法)。

此外,您不需要根据 Qt 文档删除旧的选择模型

Note: It is up to the application to delete the old selection model if it is no longer needed; i.e., if it is not being used by other views. This will happen automatically when its parent object is deleted. However, if it does not have a parent, or if the parent is a long-lived object, it may be preferable to call its deleteLater() function to explicitly delete it.

如果您想要那种级别的自定义,QListView 可能是更好的选择。

QListWidget

QAbstractItemView


只是想知道,您是否尝试过在将所有项目添加到列表小部件后设置自定义选择模型?

关于c++ - QListWidget 中的 setSelectionModel 停止发射 itemSelectionChanged 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com