我想編寫一個程序,像使用Qt框架的小導航系統一樣工作,但我仍然對它很陌生。按字母順序更新和排序Qt組合框
我用兩個Comboboxes創建了一個對話框。每個組合框包含所有「城市名稱」。 在初始化時,兩個框的內容按字母順序排序。
如果選中第一個框中的名稱,則不應該在第二個框中顯示名稱,反之亦然。
我成功取出該項目,並再次添加它,如果選擇了其他項目,但知道我不能再對它們進行排序
這是我試過到目前爲止用於更新:
for(std::vector<City>::iterator iter = citylist.begin(); iter != citylist.end(); iter++){
if(ui->combo2->currentText() != (*iter).getName()
and ui->combo1->findText((*iter).getName()) == -1){
ui->combo1->addItem((*iter).getName(),QComboBox::InsertAlphabetically);
}
}
但不插入的項目按字母順序...
,所以我想以後對它進行排序:
QSortFilterProxyModel* proxy = new QSortFilterProxyModel(ui->combo1);
proxy->setSourceModel(ui->combo1->model());
// combo's current model must be reparented,
// otherwise QComboBox::setModel() will delete it
ui->combo1->model()->setParent(proxy);
ui->combo1->setModel(proxy);
// sort
ui->combo1->model()->sort(0);
但是,如果我嘗試調用此函數發生錯誤,並且應用程序終止。
那麼有誰在那裏,誰能夠幫助我?