Results 1 to 4 of 4

Thread: Default delegate displays empty QLineEdit?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Posts
    33
    Thanks
    6
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Default delegate displays empty QLineEdit?

    It is strange to me that the default delegate for QListView displays an empty QLineEdit widget during editing instead of one filled with the text from the model's DisplayRole data. It makes me think I am doing something wrong, or inefficiently. Here is my current solution...

    Qt Code:
    1. /*
    2. [1] Reimplement "setEditorData" in a subclass of QStyledItemDelegate so that it puts
    3. the text in the editor (selected by default, as expected) instead of leaving it blank.
    4. */
    5.  
    6. void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    7. {
    8. QStyledItemDelegate::setEditorData(editor, index);
    9.  
    10. if (editor->inherits("QLineEdit"))
    11. {
    12. QLineEdit *e = static_cast<QLineEdit*>(editor);
    13. e->setText(index.data(Qt::DisplayRole).toString());
    14. }
    15. }
    16.  
    17. /*
    18. [2] Set MyDelegate as the item delegate for the view before "show()"...
    19. */
    20.  
    21. void MyMainWindow::showListView()
    22. {
    23. [...]
    24.  
    25. MyListModel *model = new MyListModel(this);
    26. model->setScene(this->currentScene());
    27.  
    28. MyDelegate *delegate = new MyDelegate(this);
    29.  
    30. QListView *view = new QListView;
    31. view->setModel(model);
    32. view->setItemDelegate(delegate); // <---[2]
    33. view->show();
    34.  
    35. [...]
    36. }
    To copy to clipboard, switch view to plain text mode 

    ...is it really necessary to subclass QStyledItemDelegate to get the desired result, or am I missing something?

    Cheers,
    -andy.f
    Last edited by andy.fillebrown; 14th April 2009 at 17:17. Reason: reformatted to look better

Similar Threads

  1. QLineEdit actions in Delegate
    By mclark in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2009, 21:34
  2. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13

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.