瀏覽代碼

Fix the crash on right click, one bug, and added a new behavior.

To fix the crash, we change the context menu policy when the widget
has the behavior "editable on focus", or "editable on double click".
The new behavior is : focus on double click.
Fix the bug about the selection of a subMenu.
Benjamin Long 13 年之前
父節點
當前提交
41fcd1f3cb
共有 3 個文件被更改,包括 35 次插入11 次删除
  1. 1 0
      Libs/Widgets/Testing/Cpp/ctkMenuComboBoxTest1.cpp
  2. 27 6
      Libs/Widgets/ctkMenuComboBox.cpp
  3. 7 5
      Libs/Widgets/ctkMenuComboBox.h

+ 1 - 0
Libs/Widgets/Testing/Cpp/ctkMenuComboBoxTest1.cpp

@@ -65,6 +65,7 @@ int ctkMenuComboBoxTest1(int argc, char * argv [] )
   Menu2->setAutoFillBackground(true);
   Menu2->setMinimumContentsLength(25);
   Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnFocus);
+  Menu2->setEditableBehavior(ctkMenuComboBox::EditableOnDoubleClick);
   //Menu2->show();
 
 /*

+ 27 - 6
Libs/Widgets/ctkMenuComboBox.cpp

@@ -69,7 +69,7 @@ ctkMenuComboBoxPrivate::ctkMenuComboBoxPrivate(ctkMenuComboBox& object)
 {
   this->MenuComboBox = 0;
   this->SearchCompleter = 0;
-  this->EditBehavior = ctkMenuComboBox::EditableOnFocus;
+  this->EditBehavior = ctkMenuComboBox::EditableOnDoubleClick;
   this->IsDefaultTextCurrent = true;
   this->IsDefaultIconCurrent = true;
 }
@@ -97,6 +97,7 @@ void ctkMenuComboBoxPrivate::init()
   // Behave like a QComboBox
   q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed,
                                QSizePolicy::ComboBox));
+
   q->setDefaultText(ctkMenuComboBox::tr("Search..."));
 }
 
@@ -105,11 +106,11 @@ QAction* ctkMenuComboBoxPrivate::actionByTitle(const QString& text, const QMenu*
 {
   if (parentMenu->title() == text)
     {
-    return parentMenu->menuAction();
+    return 0;
     }
   foreach(QAction* action, parentMenu->actions())
     {
-    if (action->text().toLower() == text.toLower())
+    if (!action->menu() && action->text().toLower() == text.toLower())
       {
       return action;
       }
@@ -353,13 +354,26 @@ void ctkMenuComboBox::setEditableBehavior(ctkMenuComboBox::EditableBehavior edit
   switch (edit)
   {
     case ctkMenuComboBox::Editable:
+      d->MenuComboBox->setContextMenuPolicy(Qt::DefaultContextMenu);
       d->setComboBoxEditable(true);
       break;
     case ctkMenuComboBox::NotEditable:
+      d->MenuComboBox->setContextMenuPolicy(Qt::DefaultContextMenu);
       d->setComboBoxEditable(false);
       break;
     case ctkMenuComboBox::EditableOnFocus:
       d->setComboBoxEditable(this->hasFocus());
+      // Here we set the context menu policy to fix a crash on the right click.
+      // When the line edit lost the focus, the comboBox become not editable,
+      // and the line edit is deleted. that cause a crash when we have the right click,
+      // which call a popup, because the focus is losted.
+      d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
+      break;
+    case ctkMenuComboBox::EditableOnDoubleClick:
+      d->setComboBoxEditable(false);
+      // Same reason.
+      d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
+      break;
   }
 }
 
@@ -403,7 +417,6 @@ void ctkMenuComboBox::onActionSelected(QAction* action)
 
   d->MenuComboBox->clearFocus();
 
-  qDebug() << action->text();
   emit ctkMenuComboBox::actionChanged(action);
 }
 
@@ -429,15 +442,23 @@ void ctkMenuComboBox::onReturnPressed()
 bool ctkMenuComboBox::eventFilter(QObject* target, QEvent* event)
 {
   Q_D(ctkMenuComboBox);
+
   if (target == d->MenuComboBox)
     {
+    if (event->type() == QEvent::MouseButtonDblClick &&
+        d->EditBehavior == ctkMenuComboBox::EditableOnDoubleClick)
+      {
+      d->setComboBoxEditable(true);
+      }
     if (event->type() == QEvent::Resize)
       {
       this->layout()->invalidate();
       }
-    if(d->EditBehavior == ctkMenuComboBox::EditableOnFocus)
+    if(d->EditBehavior == ctkMenuComboBox::EditableOnFocus ||
+       d->EditBehavior == ctkMenuComboBox::EditableOnDoubleClick)
       {
-      if (event->type() == QEvent::FocusIn)
+      if (event->type() == QEvent::FocusIn &&
+          d->EditBehavior == ctkMenuComboBox::EditableOnFocus)
         {
         d->setComboBoxEditable(true);
         }

+ 7 - 5
Libs/Widgets/ctkMenuComboBox.h

@@ -32,18 +32,19 @@
 class ctkMenuComboBoxPrivate;
 
 /// QComboBox linked with a QMenu. See ctkMenuComboBox::setMenu()
-/// ctkMenuComboBox can be editable, editable on focus or disable.
+/// ctkMenuComboBox can be editable, disable,
+/// editable on focus or editable on double click.
 ///   if it is editable :
 /// the comboBox is always editable, you can filter the Menu or show it.
-///   if it is editable on focus:
+///   if it is editable on focus - on double click:
 /// the combobox become editable when it has the focus in.
 /// So ctkMenuComboBox's purpose is to filter a menu, if you edit the current text
 /// or show the menu, if you click on the arrow.
 ///   if it is disabled :
 /// the ctkMenuComboBox has the same behavior as a QPushButton. You can't filter the menu.
 
-/// By default ctkMenuComboBox is editable on focus.
-/// See ctkmenuComboBox::editableType() to change the default behavior.
+/// By default ctkMenuComboBox is editable on double click.
+/// See ctkmenuComboBox::setEditableType() to change the default behavior.
 
 class CTK_WIDGETS_EXPORT ctkMenuComboBox : public QWidget
 {
@@ -57,7 +58,8 @@ public:
   enum EditableBehavior{
     NotEditable = 0,
     Editable,
-    EditableOnFocus
+    EditableOnFocus,
+    EditableOnDoubleClick
   };
 
   /// Superclass typedef