Results 1 to 7 of 7

Thread: Completer on QItemDelegate

  1. #1
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Completer on QItemDelegate

    Hi, I need to create a QItemDelegate in a QTableWidget cell, that would be QLineEdit in that particular cell. On that cell I need to install a QCompleter object. Thus, I subclass the QItemDelegate with my class and reimplement the virtual createEditor() method this way:

    Qt Code:
    1. QWidget *ProductsDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */,
    2. const QModelIndex &/* index */) const
    3. {
    4. QLineEdit *productsEditor = new QLineEdit(parent);
    5. QCompleter *productsCompleter=new QCompleter();
    6.  
    7. QSqlQueryModel *sqlProductsQuery=new SqlQueryModel(productsCompleter);
    8. sqlProductsQuery->setQuery("SELECT product_name FROM jeam_products");
    9. productsCompleter->setModel(sqlProductsQuery);
    10.  
    11. productsEditor->setCompleter(productsCompleter);
    12.  
    13. return productsEditor;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Then I install the delegate in the column in the QTableWidget object with QTableWidget::setItemDelegateForColumn(...) method.

    This creates the delegate fine. When I double click the cell it opens the line edit and when I press "A" it reads from the database and shows popup with completion recomendations. I have enabled all EditTriggers for the table, because basically I need all of them. That means that the AnyKey edit trigger is also enabled. So i set the focus on the cell on which the delegate is installed and type the letter (for i.e.) 'A'. If the SqlQuery finds entry in the database that starts with A, the cell immediatelly opens and closes, it loses focus and the delegate closes/destroys itself. The delegate opens for editing only if I have set the QCompleter::InlineCompletion CompletionMode or if there is no such entry in the database (ie. if I type 'W' and there is no entry that starts with 'W' in the db table).

    This is very strange and I hoped I did everything right ...
    Any ideas and answers would be very much appreciated.

    thanks

  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: Completer on QItemDelegate

    Does it happen only for the AnyKey trigger? Your code seems fine, the problem is most likely related to the fact that the completer gains and the line edit loses focus while completing. Let's focus on pinpointing the problem first and only then think about ways of solving it.

  3. #3
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Re: Completer on QItemDelegate

    To eliminate that reason I tried with setting the focus with setFocus(..reason..) but didn't help either.. what I think is that the problem is some where around the completer, when the completer is initialized, the editor is created, then it gets destructed if I activate the cell from the AnyKeyPressed edit trigger (btw, happens ONLY with this trigger and ONLY if I have selected PopupCompletion mode for the completer).

  4. #4
    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: Completer on QItemDelegate

    The problem is exactly what Wysota suggested. I recommend taking a quick look at src/gui/itemviews/qitemdelegate.cpp: QItemDelegate::eventFilter()'s QEvent::FocusOut branch.

    Workaround suggestion (don't expect it to work 100%, it was written on the fly):
    Qt Code:
    1. bool ProductsDelegate::eventFilter(QObject* object, QEvent* event)
    2. {
    3. if (event->type() == QEvent::FocusOut)
    4. {
    5. QWidget* focusWidget = QApplication::focusWidget();
    6. QLineEdit* lineEdit = qobject_cast<QLineEdit*>(object);
    7. if (lineEdit && focusWidget)
    8. {
    9. QCompleter* completer = lineEdit->completer();
    10. if (completer && focusWidget == completer->popup())
    11. return false; // let completer popup receive focus, don't close editor
    12. }
    13. }
    14. return QItemDelegate::eventFilter(object, event); // <-- it's important to call base class implementation!
    15. }
    To copy to clipboard, switch view to plain text mode 
    No need to explicitly install event filter (QAbstractItemView already does that), but just override it more or less like shown above.
    J-P Nurmi

  5. #5
    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: Completer on QItemDelegate

    Actually, now that I give it a try I'm not even able to reproduce the problem. This works just fine for me (with both Qt 4.3.3 and a couple of weeks old snapshot of Qt 4.4):
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. class ItemDelegate : public QItemDelegate
    5. {
    6. public:
    7. ItemDelegate(QObject* parent = 0) : QItemDelegate(parent) { }
    8.  
    9. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    10. {
    11. QWidget* editor = QItemDelegate::createEditor(parent, option, index);
    12. QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editor);
    13. if (lineEdit)
    14. {
    15. QStringList stringList = QStringList() << "foo" << "bar" << "baz";
    16. QCompleter* completer = new QCompleter(stringList, lineEdit);
    17. lineEdit->setCompleter(completer);
    18. }
    19. return editor;
    20. }
    21. };
    22.  
    23. int main(int argc, char* argv[])
    24. {
    25. QApplication app(argc, argv);
    26. QTableWidget table(4, 4);
    27. table.setEditTriggers(QAbstractItemView::AllEditTriggers);
    28. table.setItemDelegate(new ItemDelegate(&table));
    29. table.show();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    Doesn't it work for you?
    J-P Nurmi

  6. #6
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Re: Completer on QItemDelegate

    Quote Originally Posted by jpn View Post
    Actually, now that I give it a try I'm not even able to reproduce the problem. This works just fine for me (with both Qt 4.3.3 and a couple of weeks old snapshot of Qt 4.4):
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. class ItemDelegate : public QItemDelegate
    5. {
    6. public:
    7. ItemDelegate(QObject* parent = 0) : QItemDelegate(parent) { }
    8.  
    9. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    10. {
    11. QWidget* editor = QItemDelegate::createEditor(parent, option, index);
    12. QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editor);
    13. if (lineEdit)
    14. {
    15. QStringList stringList = QStringList() << "foo" << "bar" << "baz";
    16. QCompleter* completer = new QCompleter(stringList, lineEdit);
    17. lineEdit->setCompleter(completer);
    18. }
    19. return editor;
    20. }
    21. };
    22.  
    23. int main(int argc, char* argv[])
    24. {
    25. QApplication app(argc, argv);
    26. QTableWidget table(4, 4);
    27. table.setEditTriggers(QAbstractItemView::AllEditTriggers);
    28. table.setItemDelegate(new ItemDelegate(&table));
    29. table.show();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    Doesn't it work for you?

    When I compile your code as standalone application it works as expected, otherwise when I apply it on my code and on my view, it doesn't work :/ happens exactly the same thing. It is because I don't put AllEditTriggers, but I have only couple of them...try setting the AnyKeyPressed trigger .. doesn't work.

  7. #7
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Re: Completer on QItemDelegate

    Could this be bug in Qt ?

Similar Threads

  1. QItemDelegate Editor Crash
    By mclark in forum Qt Programming
    Replies: 13
    Last Post: 22nd March 2018, 04:06
  2. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  3. QItemDelegate problem
    By WinchellChung in forum Newbie
    Replies: 2
    Last Post: 5th December 2007, 16:16
  4. premature call to setmodeldata with QItemDelegate
    By placebo in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2007, 17:39
  5. Replies: 3
    Last Post: 12th May 2006, 19:31

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.