Results 1 to 2 of 2

Thread: QCompleter behvaiour in table view

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default QCompleter behvaiour in table view

    I have a similar problem to the one back in this thread although the code is C++ (below) not Python. A QLineEdit with completer in a table view only displays expected completion behaviour under some circumstances.

    If you double-click or enter edit mode explicitly (F2 on Linux) the completer works correctly on the cell. If you trigger edit mode by just typing with a current cell selected this is the debug output:
    Qt Code:
    1. // Starting point: shot0.png
    2. // I type 'b' into the current cell
    3. CompleterDelegate::createEditor() called for QWidget(0x83582e0, name = "qt_scrollarea_viewport") QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(, )
    4. CompleterDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) )
    5. CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(, )
    6. CompleterDelegate::setModelData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(, )
    7. CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(QString, "b")
    8. // result: shot1.png
    9. // the cell does not display as an active editor, no completer visible: middle screen shot
    10. // type 'a'
    11. CompleterDelegate::createEditor() called for QWidget(0x83582e0, name = "qt_scrollarea_viewport") QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(QString, "b")
    12. CompleterDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) )
    13. CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(QString, "b")
    14. // result: shot3.png
    15. // setEditorText() called with 'b' but active editor displaying 'a', no completer popup
    To copy to clipboard, switch view to plain text mode 
    Shot3.png is the behaviour expected and seen if you explicitly enter edit mode.
    tile.png

    The cycle through setEditor, setModel, the setEditor at 5-7 of the debug out is telltale. Wysota mentioned something about focus and event filters in the old thread. I have briefly (minutes not hours) looked at the find completer stuff in Qt Creator but did not find event filter actions related to focus on arbitrary key presses.

    Can anyone shed light on how to get correct behaviour here?

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class CompleterDelegate : public QStyledItemDelegate
    5. {
    6. public:
    7. CompleterDelegate(QObject* parent = 0) : QStyledItemDelegate(parent) { }
    8.  
    9. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    10. {
    11. qDebug() << "CompleterDelegate::createEditor() called for" << parent << index << index.data(Qt::EditRole);
    12. QLineEdit* lineEdit = new QLineEdit(parent);
    13. QStringList stringList = QStringList() << "foo" << "bar" << "baz";
    14. QCompleter* completer = new QCompleter(stringList, lineEdit);
    15. completer->setCompletionMode(QCompleter::PopupCompletion);
    16. completer->setCaseSensitivity(Qt::CaseInsensitive);
    17.  
    18. lineEdit->setCompleter(completer);
    19. return lineEdit;
    20. }
    21. void setEditorData(QWidget *editor, const QModelIndex &index) const
    22. {
    23. qDebug() << "CompleterDelegate::setEditorData() called for" << index << index.data(Qt::EditRole);
    24. QStyledItemDelegate::setEditorData(editor, index);
    25. }
    26. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    27. {
    28. qDebug() << "CompleterDelegate::setModelData() called for" << index << index.data(Qt::EditRole);
    29. QStyledItemDelegate::setModelData(editor, model, index);
    30. }
    31. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    32. {
    33. qDebug() << "CompleterDelegate::updateEditorGeometry() called for" << index;
    34. QStyledItemDelegate::updateEditorGeometry(editor, option, index);
    35. }
    36. };
    37.  
    38.  
    39. int main(int argc, char* argv[])
    40. {
    41. QApplication app(argc, argv);
    42.  
    43. QStandardItemModel model(4, 2);
    44. tv.setWindowTitle("View");
    45. // default edit triggers are:
    46. tv.setEditTriggers(QAbstractItemView::DoubleClicked
    47. | QAbstractItemView::EditKeyPressed
    48. | QAbstractItemView::AnyKeyPressed );
    49. tv.setModel(&model);
    50. tv.setItemDelegate(new CompleterDelegate(&tv));
    51. tv.show();
    52.  
    53. return app.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 7th July 2011 at 04:30. Reason: spelling corrections

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QCompleter behvaiour in table view

    Further tinkering shows that the editor is destroyed after line 7 of the debug output before user control is returned.

    A normal editor creation without completer looks like:
    Qt Code:
    1. DummyDelegate::createEditor() called for QWidget(0x9663a98, name = "qt_scrollarea_viewport") QModelIndex(0,0,0x9665138,QStandardItemModel(0xbfd09674) )
    2. DummyDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x9665138,QStandardItemModel(0xbfd09674) )
    3. DummyDelegate::setEditorData() called for QModelIndex(0,0,0x9665138,QStandardItemModel(0xbfd09674) )
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 8th June 2011, 14:13
  2. Replies: 0
    Last Post: 17th September 2010, 12:19
  3. QCompleter causes table to lose focus
    By knack in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2010, 22:22
  4. Replies: 5
    Last Post: 3rd April 2010, 04:07
  5. Table Widget Vs. Table View
    By winston2020 in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2008, 09:56

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
  •  
Qt is a trademark of The Qt Company.