[英]QT - QListView - currentSelection/currentChanged is not emitted
[英]Qt setCurrentIndex does not emit currentChanged for invalid ModelIndex
我有一個向導,其中僅當用戶從QListView中選擇一個條目時才允許該用戶前進。 為了進行檢查,我已經將包含驗證邏輯的方法連接到currentChanged
信號,但是如果所選Index無效,則不會發出該信號:
// 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
返回的值,但不會觸發信號。 請注意,重置選擇模型不會觸發信號。 您可能將其視為錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.