From 013c6a6b6a5a645d3f918cd6bdfdc3e6b4c93b8f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 13 Dec 2018 15:17:18 +0100 Subject: [PATCH] Speed up inserting many items in sorted wxChoice in wxQt Only sort the combobox once instead of doing it for every item. See https://github.com/wxWidgets/wxWidgets/pull/1054 --- src/qt/choice.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/qt/choice.cpp b/src/qt/choice.cpp index 02101b7562..2716a9c004 100644 --- a/src/qt/choice.cpp +++ b/src/qt/choice.cpp @@ -141,7 +141,28 @@ int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, wxClientDataType type) { InvalidateBestSize(); + + // Hack: to avoid resorting the model many times in DoInsertOneItem(), + // which will be called for each item from DoInsertItemsInLoop(), reset the + // wxCB_SORT style if we have it temporarily and only sort once at the end. + bool wasSorted = false; + if ( IsSorted() ) + { + wasSorted = true; + ToggleWindowStyle(wxCB_SORT); + } + int n = DoInsertItemsInLoop(items, pos, clientData, type); + + if ( wasSorted ) + { + // Restore the flag turned off above. + ToggleWindowStyle(wxCB_SORT); + + // And actually sort items now. + m_qtComboBox->model()->sort(0); + } + return n; }