Bladeren bron

ENH: Fix ctkMenuComboBox behavior in Qt5

In Qt5, when QCompleter sends its activated() signal, QComboBox sets
its current index to the activated item, if found. Work around that behavior
by re-selecting the original item.

Additionally, ensure that an item is not added to the combobox when the
user enters a new string.

Together, these changes make ctkMenuComboBox behave like in Qt4.
Max Smolens 7 jaren geleden
bovenliggende
commit
72ae78e45b
2 gewijzigde bestanden met toevoegingen van 22 en 1 verwijderingen
  1. 21 1
      Libs/Widgets/ctkMenuComboBox.cpp
  2. 1 0
      Libs/Widgets/ctkMenuComboBox_p.h

+ 21 - 1
Libs/Widgets/ctkMenuComboBox.cpp

@@ -103,6 +103,7 @@ void ctkMenuComboBoxPrivate::init()
   this->MenuComboBox->setMinimumContentsLength(12);
   layout->addWidget(this->MenuComboBox);
   this->MenuComboBox->installEventFilter(q);
+  this->MenuComboBox->setInsertPolicy(QComboBox::NoInsert);
   this->MenuComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
   this->MenuComboBox->addItem(this->DefaultIcon, this->DefaultText);
   q->connect(this->MenuComboBox, SIGNAL(popupShown()),
@@ -113,7 +114,7 @@ void ctkMenuComboBoxPrivate::init()
   this->SearchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
   this->SearchCompleter->setModelFiltering(ctkCompleter::FilterWordStartsWith);
   q->connect(this->SearchCompleter, SIGNAL(activated(QString)),
-             q, SLOT(onEditingFinished()));
+             this, SLOT(onCompletion(QString)));
 
   // Automatically set the minimumSizeHint of the layout to the widget
   layout->setSizeConstraint(QLayout::SetMinimumSize);
@@ -206,6 +207,25 @@ void ctkMenuComboBoxPrivate::setComboBoxEditable(bool edit)
 }
 
 // -------------------------------------------------------------------------
+void ctkMenuComboBoxPrivate::onCompletion(const QString& text)
+{
+  Q_Q(ctkMenuComboBox);
+
+  // In Qt5, when QCompleter sends its activated() signal, QComboBox sets
+  // its current index to the activated item, if found. Work around that behavior
+  // by re-selecting the original item.
+  this->MenuComboBox->setCurrentIndex(0);
+
+  // Set text to the completed string
+  if (this->MenuComboBox->lineEdit())
+    {
+    this->MenuComboBox->lineEdit()->setText(text);
+    }
+
+  q->onEditingFinished();
+}
+
+// -------------------------------------------------------------------------
 void ctkMenuComboBoxPrivate::addAction(QAction *action)
 {
   if (action->menu())

+ 1 - 0
Libs/Widgets/ctkMenuComboBox_p.h

@@ -75,6 +75,7 @@ public:
 
 public Q_SLOTS:
   void setComboBoxEditable(bool editable = true);
+  void onCompletion(const QString& text);
 
 protected:
   QIcon         DefaultIcon;