Results 1 to 11 of 11

Thread: QItemDelegate

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QItemDelegate

    Hi everybody,

    I'm currently busy playing with QAbstractItemModels and QItemDelegates.
    This is de situation, I have a column warrenty date ("Geleverd op") and that's a date field in side my model, so I installed a QItemDelegate on a column QItemDelegate::setDelegateForColumn();

    EDIT: I changed it to setItemDelegate for testing purpose!
    But it doesn't work, it constructs the delegate but he doesn't use the delegate....

    this is the code were I set the delegate:
    Qt Code:
    1. void OrderAddWindow::setTable()
    2. {
    3. qDebug("OrderAddWindow::setTable(): Loading table and model");
    4.  
    5. QStringList header;
    6. QList<QStringList> articleList;
    7.  
    8. header << "Artikel nr" << "Beschrijving" << "Bewerking" << "Prijs" << "Besteld" << "Geleverd" << "Geleverd op";
    9.  
    10.  
    11. model = new ArticlesAddTableModel( articleList , header);
    12. this->tableArticles->setModel(model);
    13. this->tableArticles->setItemDelegate(new DateDelegate(this->tableArticles));
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    This is de delegate code:
    Qt Code:
    1. #include "dateDelegate.h"
    2. #include <QtGui>
    3.  
    4. DateDelegate::DateDelegate(QObject *parent)
    5. : QItemDelegate(parent)
    6. {
    7. qDebug("DateDelegate::DateDelegate():Constructing delegate");
    8. }
    9.  
    10. QWidget* DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index)
    11. {
    12. qDebug("DateDelegate::createEditor(): Creating delegate widget");
    13. //create the view for the item
    14. const QAbstractItemModel *model = index.model();
    15.  
    16. if(!model)
    17. QItemDelegate::createEditor(parent, option, index);
    18.  
    19. QDateEdit *edit = new QDateEdit(parent);
    20. edit->setDisplayFormat("dd-MM-yyyy");
    21. return edit;
    22. }
    23.  
    24. void DateDelegate::setEditorData(QWidget *editor, const QModelIndex& index)
    25. {
    26. qDebug("DateDelegate::setEditorDate(): Set data");
    27. QDateEdit *edit = qobject_cast<QDateEdit*>(editor);
    28.  
    29. if(!edit)
    30. QItemDelegate::setEditorData(editor, index);
    31. edit->setDate(QDate::currentDate());
    32.  
    33. }
    34.  
    35. void DateDelegate::setModelData(QWidget *parent, QAbstractItemModel *model, const QModelIndex& index)
    36. {
    37. qDebug("DateDelegate::setModelData(): Set data in model");
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    and this is de model code and the subclass of the model
    Qt Code:
    1. #include "articlesTableModel.h"
    2. #include <QtGui>
    3.  
    4.  
    5. ArticlesTableModel::ArticlesTableModel(const QList<QStringList> &data, const QStringList &header, QObject *parent)
    6. {
    7. qDebug("ArticlesTableModel::ArticlesTableModel(): Constructing the table model");
    8. this->articles = data;
    9. this->header = header;
    10. }
    11.  
    12.  
    13. int ArticlesTableModel::rowCount( const QModelIndex& parent) const
    14. {
    15. Q_UNUSED(parent);
    16. return articles.count();
    17. }
    18.  
    19. int ArticlesTableModel::columnCount( const QModelIndex& parent) const
    20. {
    21. Q_UNUSED(parent);
    22. return header.count();
    23. }
    24.  
    25. QVariant ArticlesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    26. {
    27. if( Qt::Horizontal == orientation)
    28. {
    29. if(Qt::DisplayRole == role)
    30. {
    31. return header.at(section);
    32. }
    33. }
    34.  
    35. return QAbstractTableModel::headerData(section, orientation, role);
    36. }
    37.  
    38. QVariant ArticlesTableModel::data(const QModelIndex &index, int role) const
    39. {
    40.  
    41. if(!index.isValid())
    42. return QVariant();
    43.  
    44. QStringList record = this->articles.at(index.row());
    45.  
    46. if(role == Qt::DisplayRole || role == Qt::EditRole){
    47.  
    48. return record.at(index.column());
    49.  
    50. }
    51.  
    52. return QVariant();
    53.  
    54. }
    55.  
    56. Qt::ItemFlags ArticlesTableModel::flags(const QModelIndex &index) const
    57. {
    58. if(!index.isValid())
    59. return 0;
    60.  
    61. return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
    62. }
    63.  
    64.  
    65. bool ArticlesTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
    66. {
    67. if(index.isValid() && (role == Qt::DisplayRole || role == Qt::EditRole)){
    68. this->articles[index.row()][index.column()] = value.toString();
    69. emit dataChanged(index, index);
    70. return true;
    71. }
    72.  
    73. return false;
    74. }
    75.  
    76. bool ArticlesTableModel::insertRows(int row, int count, const QModelIndex &parent)
    77. {
    78. Q_UNUSED(parent);
    79. QStringList emptyRecord;
    80. for(int i = 0; i < columnCount(QModelIndex()); i++)
    81. emptyRecord.append(QString());
    82. beginInsertRows(QModelIndex(), row, row+count-1);
    83. for(int i = 0; i < count; i++){
    84. this->articles.insert(row, emptyRecord);
    85. }
    86. endInsertRows();
    87.  
    88. return true;
    89. }
    90.  
    91. bool ArticlesTableModel::removeRows(int row, int count, const QModelIndex &parent)
    92. {
    93. Q_UNUSED(parent);
    94. if(row-count-1 > this->articles.count() -1) return false;
    95. beginRemoveRows(QModelIndex(), row, row+count-1);
    96. for(int i=0; i<count; i++)
    97. this->articles.removeAt(row);
    98. endRemoveRows();
    99. return true;
    100.  
    101. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "ArticlesAddTableModel.h"
    2. #include <QtGui>
    3.  
    4.  
    5. ArticlesAddTableModel::ArticlesAddTableModel(const QList<QStringList> &data, const QStringList &header, QObject *parent)
    6. : ArticlesTableModel(data, header, parent)
    7. {
    8. qDebug("ArticlesAddTableModel::ArticlesAddTableModel(): Constructing the add table model");
    9. }
    10.  
    11. QList<QStringList> ArticlesAddTableModel::retreiveData()
    12. {
    13. return this->articles;
    14. }
    15.  
    16.  
    17. void ArticlesAddTableModel::removeRow()
    18. {
    19. qDebug("ArticlesAddTableModel::removeRow(): Remove row from model");
    20. this->removeRows(rowCount(QModelIndex()) - 1, 1, QModelIndex());
    21. }
    22.  
    23. void ArticlesAddTableModel::addRow()
    24. {
    25.  
    26. qDebug("ArticlesAddTableModel::addRow(): Add an empty row to model");
    27.  
    28. insertRows(rowCount(QModelIndex()), 1, QModelIndex());
    29. }
    To copy to clipboard, switch view to plain text mode 

    Does somebody know how to trigger createEditor() in the QItemDelegate?

    Greetings,

    Cyberboy

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate

    I guess with double click

    Thats how I saw in delegates examples in Qt demo

  3. #3
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QItemDelegate

    Hmm... double click doesn't work, it just opens the normal editing mode :S Could it be something with te flags? Should I pas only the display flag? And give the widget the responsibility for the editing?

  4. #4
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QItemDelegate

    I also want to have multi-lined text in a table cell? Do I have to write a delegate that contains a QLabel widget or can I just enable it in the tableview?

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QItemDelegate

    Watch the const-qualifications...
    createEditor is a const method in the base class, so it has to be const in your class, too.
    You currently do not reimplement the method (as you want to), but shadow it. Your implementation is not called.
    In short: add a "const".


    The default delegate does support multiple lines (if broken with \n and not html tags like <br>).

    HTH

  6. #6
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QItemDelegate

    Thanks for the reply, I added the const qualification to the createEditor function.
    Qt Code:
    1. const QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index);
    To copy to clipboard, switch view to plain text mode 

    And I changed it also in the .cpp file. But it doesn't work :S

    And about the multiline support, is it possible to type multilines, because when you press return the cell is changes from Qt::EditRole to Qt:isplayRole? Or do I have to make some kind of popup widget which support multiple lines and insert it later on?

    Greetings,

    Cyberboy

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QItemDelegate

    no, not that way:
    Qt Code:
    1. QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    To copy to clipboard, switch view to plain text mode 

    "const" methods are methods that must not change any members of a class
    (unless they are mutable). Consult your C++ book for details.

  8. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QItemDelegate

    multilines: with a QLineEdit (the default editor) it does not work.
    You have to modify the delegate to return a suitable editor, e.g. a QTextEdit.
    You also have to take care because the (default) eventFilter will catch the "return" key and close the editor which you do not want here.

  9. #9
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QItemDelegate

    I'll go for the popup widget, that seems to be a logical step and it will save me allot of work too.
    But nobody knows why my delegate isn't working as it suppose to be?

  10. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QItemDelegate

    didn't the posting sent at 13:39 help?
    (put the const behind the signature, not in front the QWidget*)

  11. #11
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QItemDelegate

    It works thank you so much, and I'm sorry that I put the const before the method, I'm not that well know with the c++ syntax. I'm still busy with finish the book about c++ syntax. But thank you anyway!!

Similar Threads

  1. QItemDelegate Editor Crash
    By mclark in forum Qt Programming
    Replies: 13
    Last Post: 22nd March 2018, 04:06
  2. QItemDelegate, QTreeView and scrollbars
    By SiLiZiUMM in forum Qt Programming
    Replies: 11
    Last Post: 6th May 2008, 17:23
  3. Completer on QItemDelegate
    By gemidjy in forum Qt Programming
    Replies: 6
    Last Post: 31st March 2008, 10:29
  4. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  5. premature call to setmodeldata with QItemDelegate
    By placebo in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2007, 17:39

Tags for this Thread

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.