Items disappear from QComboBox when a QSortFilterProxyModel is set as the model
-
I have a QComboBox, and I want to sort the items with a custom sort class. I read the docs for QSortFilterProxyModel, and implemented a subclass as suggested (code is below). However, when I set my QSortFilterProxyModel as the model for my QComboBox, all of the items I've added disappear!
My QSortFilterProxyModel subclass looks like this:
class FactorSortBySourceNodeFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT public: FactorSortBySourceNodeFilterProxyModel(QObject* parent = nullptr); void AddFactorSourceIdPair(int factor_id,int node_id); protected: bool lessThan(const QModelIndex& left, const QModelIndex& right) const override; private: std::unordered_map<int,int> factor_source_node_id_; };The above class has a custom
lessThanmethod that looks up items byfactor_idand sorts by the correspondingnode_id.I'm using it like this (here,
thisis aQObjectsubclass, andfactor_browser_is aQComboBox):factor_browser_ = new QComboBox(widget_); FactorSortBySourceNodeFilterProxyModel* proxy_model = new FactorSortBySourceNodeFilterProxyModel(this); proxy_model->setSourceModel(factor_browser_->model()); factor_browser_->setModel(proxy_model); for (const auto& factor_ptr : all_factors) { const QString factor_id_string = "custom-string-here" QVariant factor_id_data; factor_id_data.setValue(factor_ptr->GetFactorId()); factor_browser_->addItem(factor_id_string, factor_id_data); proxy_model->AddFactorSourceIdPair(factor_ptr->GetFactorId(), factor_ptr->->GetNodeId()); } factor_browser_->model()->sort(0);I added some print statements to the
AddFactorSourceIdPairmethod, and confirmed that it's called as expected. But whensortis called, the model contains no items, even though I'm adding several items with theaddItemcall in the loop! I printedfactor_browser_->model()->rowCount(), and it's zero. I'd welcome any hints as to what I'm doing wrong here. Thanks! -
Hi and welcome to devnet,
What if you return always true for testing lessThan ?
-
Thank you for the reply! I actually just solved this, with the help of this StackOverflow post. The problem was that the
setModelcall was deleting theQComboBox's internal model. The key is to create a newQStandardItemModel, set it as the source model for the proxy, add data to that model, and sort it. Then, in the last step, set the proxy as the container's model withsetModel. Rough outline:QStandardItemModel* model = new QStandardItemModel(this); FactorSortBySourceNodeFilterProxyModel* proxy_model = new FactorSortBySourceNodeFilterProxyModel(this); proxy_model->setSourceModel(model);In the code above,
FactorSortBySourceNodeFilterProxyModelis a subclass ofQSortFilterProxyModel.Then, in a loop, I call
model->appendRow(new QStandardItem(my-data-here));repeatedly. Lastly, I do this:proxy_model->sort(0); factor_browser_->setModel(proxy_model);Here,
factor_browser_is myQComboBox. The items are now sorted with the customlessThanthat myFactorSortBySourceNodeFilterProxyModelimplements.