From 34f2a5ecc7c7bef15e3f29f1dc5def8a1d2e9965 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Tue, 15 Oct 2013 13:45:24 -0300 Subject: [PATCH] Fixed the Tab behavior on the QCombobox Delegate This Patch fixes the tab behavior on the QComboBox delegate. For a QComboBox, tab was being treated as 'cancel' action on edit, but since it will send a editingFinished() signal, and the Qt::Key_Return will also send a editingFinished() signal, I couldn't use that method and had to do a little hack around it. The code is mostly clean and works. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/modeldelegates.cpp | 18 +++++++++++++++++- qt-ui/modeldelegates.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index a4f457f79..d1e283911 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -22,6 +22,7 @@ // Gets the index of the model in the currentRow and column. // currCombo is defined below. #define IDX( XX ) mymodel->index(currCombo.currRow, XX) +static bool keyboardFinished = false; StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent): QStyledItemDelegate(parent), @@ -91,11 +92,12 @@ QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewI comboDelegate->lineEdit()->installEventFilter( const_cast(qobject_cast(this))); comboDelegate->view()->installEventFilter( const_cast(qobject_cast(this))); connect(comboDelegate, SIGNAL(highlighted(QString)), this, SLOT(testActivation(QString))); - connect(comboDelegate->lineEdit(), SIGNAL(editingFinished()), this, SLOT(testActivation())); connect(comboDelegate, SIGNAL(activated(QString)), this, SLOT(fakeActivation())); + connect(this, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(fixTabBehavior())); currCombo.comboEditor = comboDelegate; currCombo.currRow = index.row(); currCombo.model = const_cast(index.model()); + keyboardFinished = false; // Current display of things on Gnome3 looks like shit, so // let`s fix that. @@ -137,6 +139,16 @@ void ComboBoxDelegate::fakeActivation(){ QStyledItemDelegate::eventFilter(currCombo.comboEditor, &ev); } +// This 'reverts' the model data to what we actually choosed, +// becaus e a TAB is being understood by Qt as 'cancel' while +// we are on a QComboBox ( but not on a QLineEdit. +void ComboBoxDelegate::fixTabBehavior() +{ + if(keyboardFinished){ + setModelData(0,0,QModelIndex()); + } +} + bool ComboBoxDelegate::eventFilter(QObject* object, QEvent* event) { // Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices. @@ -147,6 +159,10 @@ bool ComboBoxDelegate::eventFilter(QObject* object, QEvent* event) currCombo.ignoreSelection = true; currCombo.comboEditor->showPopup(); } + if(ev->key() == Qt::Key_Tab || ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return){ + currCombo.activeText = currCombo.comboEditor->currentText(); + keyboardFinished = true; + } } else{ // the 'Drop Down Menu' part. QKeyEvent *ev = static_cast(event); diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index eb9a0aa04..1aedf1bbf 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -27,6 +27,7 @@ public slots: void testActivation(const QString& currString = QString()); //HACK: try to get rid of this in the future. void fakeActivation(); + void fixTabBehavior(); virtual void revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint) = 0; protected: QAbstractItemModel *model;