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 <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2013-10-15 13:45:24 -03:00 committed by Dirk Hohndel
parent 6ccb541f1d
commit 34f2a5ecc7
2 changed files with 18 additions and 1 deletions

View file

@ -22,6 +22,7 @@
// Gets the index of the model in the currentRow and column. // Gets the index of the model in the currentRow and column.
// currCombo is defined below. // currCombo is defined below.
#define IDX( XX ) mymodel->index(currCombo.currRow, XX) #define IDX( XX ) mymodel->index(currCombo.currRow, XX)
static bool keyboardFinished = false;
StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent): StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent):
QStyledItemDelegate(parent), QStyledItemDelegate(parent),
@ -91,11 +92,12 @@ QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewI
comboDelegate->lineEdit()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this))); comboDelegate->lineEdit()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this)));
comboDelegate->view()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this))); comboDelegate->view()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this)));
connect(comboDelegate, SIGNAL(highlighted(QString)), this, SLOT(testActivation(QString))); 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(comboDelegate, SIGNAL(activated(QString)), this, SLOT(fakeActivation()));
connect(this, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(fixTabBehavior()));
currCombo.comboEditor = comboDelegate; currCombo.comboEditor = comboDelegate;
currCombo.currRow = index.row(); currCombo.currRow = index.row();
currCombo.model = const_cast<QAbstractItemModel*>(index.model()); currCombo.model = const_cast<QAbstractItemModel*>(index.model());
keyboardFinished = false;
// Current display of things on Gnome3 looks like shit, so // Current display of things on Gnome3 looks like shit, so
// let`s fix that. // let`s fix that.
@ -137,6 +139,16 @@ void ComboBoxDelegate::fakeActivation(){
QStyledItemDelegate::eventFilter(currCombo.comboEditor, &ev); 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) bool ComboBoxDelegate::eventFilter(QObject* object, QEvent* event)
{ {
// Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices. // 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.ignoreSelection = true;
currCombo.comboEditor->showPopup(); 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. else{ // the 'Drop Down Menu' part.
QKeyEvent *ev = static_cast<QKeyEvent*>(event); QKeyEvent *ev = static_cast<QKeyEvent*>(event);

View file

@ -27,6 +27,7 @@ public slots:
void testActivation(const QString& currString = QString()); void testActivation(const QString& currString = QString());
//HACK: try to get rid of this in the future. //HACK: try to get rid of this in the future.
void fakeActivation(); void fakeActivation();
void fixTabBehavior();
virtual void revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint) = 0; virtual void revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint) = 0;
protected: protected:
QAbstractItemModel *model; QAbstractItemModel *model;