QT setCurrentIndex不会发出用于无效的模型Inderex的CurrentChange

Qt setCurrentIndex does not emit currentChanged for invalid ModelIndex

本文关键字:无效 模型 Inderex CurrentChange 用于 setCurrentIndex QT      更新时间:2023-10-16

我有一个向导,只有在用户从qlistview选择条目时,只允许用户进步。为了检查这一点,我已将包含验证逻辑的方法连接到currentChanged信号,但是如果所选索引无效,则不会发出信号:

// It's connected like this
connect(connectionsListView->selectionModel(), 
        SIGNAL(currentChanged(QModelIndex,QModelIndex)),
        this, SLOT(connectionSelected(QModelIndex)));
// Does not emit for empty ListModel
connectionsListView->selectionModel()->setCurrentIndex
    (connectionListModel->index(0, 0), QItemSelectionModel::SelectCurrent);
// Does not emit whatsoever
connectionsListView->selectionModel()->clear();
// Valid selections are delegated to my validation logic and handled correctly

有什么想法为什么会发生这种情况以及我能做什么来解决这个问题?目前,我有第二个信号,即我在索引无效的情况下在代码中发射的,但是如果索引无效,我希望QT简单地发射信号。

第一行无法散发,因为索引没有改变:这是无效的,并且由于模型为空,但仍然无效。有一个不变的:在空模型上,选择索引无效。因此,只要模型为空,就永远不要期望currentChanged信号发射。

第二行不会出于相同的原因发射:索引仍然无效。

您可能需要创建一个复制问题的测试用例:您需要执行更改currentIndex返回的值但不会发射信号的值。请注意,重置选择模型不会发射信号。您可以将其视为错误。