Results 1 to 4 of 4

Thread: Default delegate displays empty QLineEdit?

  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 18:17. Reason: reformatted to look better

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Default delegate displays empty QLineEdit?

    does index.data(Qt:isplayRole).toString() return something?
    what you will see in console if you execute this code?
    Qt Code:
    1. ...
    2. qDebug() << index.data(Qt::DisplayRole).toString();
    3. qDebug() << index.data(Qt::EditRole).toString();
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    andy.fillebrown (16th April 2009)

  4. #3
    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: Default delegate displays empty QLineEdit?

    The delegate's edit functionality works on EditRole thus you have to return data from this role for your custom models and you have to accept data for this role in setData() if you want the default delegate to be able to edit your model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    andy.fillebrown (16th April 2009)

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

    Default Re: Default delegate displays empty QLineEdit?

    Ok, problem solved... from your replies it seemed likely the problem was in my code so I looked things over more carefully and found a bug in my QAbstractListModel::data reimplementation...

    was:
    Qt Code:
    1. [...]
    2.  
    3. if (role != Qt::DisplayRole || role != Qt::EditRole)
    4. return QVariant();
    5.  
    6. // return data...
    7.  
    8. [...]
    To copy to clipboard, switch view to plain text mode 

    fixed:
    Qt Code:
    1. [...]
    2.  
    3. if (role != Qt::DisplayRole && role != Qt::EditRole)
    4. return QVariant();
    5.  
    6. // return data...
    7.  
    8. [...]
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    -andy.f
    Last edited by andy.fillebrown; 16th April 2009 at 14:14. 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, 22:34
  2. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 16: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.