Results 1 to 4 of 4

Thread: keypress while editing an item in QListWidget

  1. #1
    Join Date
    Mar 2006
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default keypress while editing an item in QListWidget

    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
    Qt Code:
    1. #include <QtGui/QtGui>
    2.  
    3. class Test: public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. bool eventFilter(QObject *o, QEvent *e);
    8. };
    To copy to clipboard, switch view to plain text mode 
    test2.cpp
    Qt Code:
    1. #include "test2.h"
    2.  
    3. void addit(QObject * o, Test * t)
    4. {
    5. o->installEventFilter(t);
    6. foreach (QObject * cobj, o->children())
    7. addit(cobj, t);
    8. }
    9.  
    10. bool Test::eventFilter(QObject *o, QEvent *e)
    11. {
    12. if (e->type() == QEvent::MouseButtonPress) {
    13. qDebug() << "QEvent::MouseButtonPress:";
    14. o->dumpObjectInfo();
    15. } else if (e->type() == QEvent::MouseButtonRelease) {
    16. qDebug() << "QEvent::MouseButtonRelease:";
    17. o->dumpObjectInfo();
    18. } if (e->type() == QEvent::KeyPress) {
    19. qDebug() << "QEvent::KeyPress:";
    20. o->dumpObjectInfo();
    21. } else if (e->type() == QEvent::KeyRelease) {
    22. qDebug() << "QEvent::KeyRelease:";
    23. o->dumpObjectInfo();
    24. }
    25. return false;
    26. }
    27.  
    28. int main(int argc, char * argv[])
    29. {
    30. QApplication app(argc, argv);
    31. QListWidget * list = new QListWidget();
    32. Test * test = new Test();
    33. addit(list, test);
    34. QListWidgetItem * newItem = new QListWidgetItem("foo 1", list);
    35. newItem->setFlags(Qt::ItemIsEditable);
    36. list->show();
    37. return app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keypress while editing an item in QListWidget

    I don't think QListWidget receives keypresses at all. It's the editor created by the delegate that receives them.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: keypress while editing an item in QListWidget

    Subclass QItemDelegate and override createEditor() so that it always installs your event filter on the created editor widget:
    Qt Code:
    1. QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QWidget* editor = QItemDelegate::createEditor(parent, option, index);
    4. editor->installEventFilter(eventFilter);
    5. return editor;
    6. }
    To copy to clipboard, switch view to plain text mode 
    and use this item delegate for your list widget..
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    Beluvius (4th April 2006)

  5. #4
    Join Date
    Mar 2006
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: keypress while editing an item in QListWidget

    Thanks, I got it now. I would have never thought of that one yet. Lots to learn...

Similar Threads

  1. Help on QListWidget InternalMove item signal
    By tho97 in forum Qt Programming
    Replies: 3
    Last Post: 14th February 2009, 02:13
  2. QListWidget and selecting an item
    By invictus in forum Newbie
    Replies: 4
    Last Post: 19th June 2007, 12:59
  3. Replies: 1
    Last Post: 19th April 2007, 23:23
  4. QTreeWidget item editing: want column specificity
    By McKee in forum Qt Programming
    Replies: 12
    Last Post: 10th December 2006, 23:12
  5. extract item from QListWidget
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 20:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.