PDA

View Full Version : keypress while editing an item in QListWidget



Beluvius
3rd April 2006, 11:00
The next widget that is causing me a head-ache, the QListWidget. I never get to catch the key press event in a QListWidget. Reproduce:
moc test2.h -o test2_moc.cpp
g++ -g test2* -I /usr/local/TrollTech/Qt-4.1.0/include -L /usr/local/TrollTech/Qt-4.1.0/lib -lQtGui_debug -lQtCore_debug
test2.h


#include <QtGui/QtGui>

class Test: public QObject
{
Q_OBJECT
public:
bool eventFilter(QObject *o, QEvent *e);
};

test2.cpp


#include "test2.h"

void addit(QObject * o, Test * t)
{
o->installEventFilter(t);
foreach (QObject * cobj, o->children())
addit(cobj, t);
}

bool Test::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::MouseButtonPress) {
qDebug() << "QEvent::MouseButtonPress:";
o->dumpObjectInfo();
} else if (e->type() == QEvent::MouseButtonRelease) {
qDebug() << "QEvent::MouseButtonRelease:";
o->dumpObjectInfo();
} if (e->type() == QEvent::KeyPress) {
qDebug() << "QEvent::KeyPress:";
o->dumpObjectInfo();
} else if (e->type() == QEvent::KeyRelease) {
qDebug() << "QEvent::KeyRelease:";
o->dumpObjectInfo();
}
return false;
}

int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QListWidget * list = new QListWidget();
Test * test = new Test();
addit(list, test);
QListWidgetItem * newItem = new QListWidgetItem("foo 1", list);
newItem->setFlags(Qt::ItemIsEditable);
list->show();
return app.exec();
}


Start and double click the 'foo 1' item to begin editing it. Type any key.
The strange thing is that the key releases are caught, but never the key presses.
I tried various things, like looking at the list->itemWidget(item), but that always returns 0 at the places I tried (like after the doubleclick). Where is the keypress handled?

Greetings,
Beluvius

wysota
3rd April 2006, 13:51
I don't think QListWidget receives keypresses at all. It's the editor created by the delegate that receives them.

jpn
3rd April 2006, 15:04
Subclass QItemDelegate and override createEditor() so that it always installs your event filter on the created editor widget:


QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QWidget* editor = QItemDelegate::createEditor(parent, option, index);
editor->installEventFilter(eventFilter);
return editor;
}

and use this item delegate for your list widget..

Beluvius
4th April 2006, 09:56
Thanks, I got it now. I would have never thought of that one yet. Lots to learn...