Results 1 to 2 of 2

Thread: QTableWidget + QDateEdit + Delegates

  1. #1
    Join Date
    Jun 2008
    Location
    Rybnik, Poland
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableWidget + QDateEdit + Delegates

    Hi
    i try to set QDateEdit in cell QTableWidget but doesn't work
    Qt Code:
    1. //mainwindow
    2. ui.tabela->setItemDelegate(new DateDelegate(5));
    3. QTableWidgetItem *nazwa5 = new QTableWidgetItem(obiekt[idx].item[5]);
    4. ui.tabela->setItem(idx, 5, nazwa5);
    To copy to clipboard, switch view to plain text mode 
    "obiekt[idx].item[5]" it's QString

    class DateDelegate
    Qt Code:
    1. // *.h
    2. #include <QItemDelegate>
    3. #include <QModelIndex>
    4. #include <QObject>
    5. #include <QSize>
    6. #include <QDateEdit>
    7.  
    8. class DateDelegate : public QItemDelegate
    9. {
    10. Q_OBJECT
    11. public:
    12. DateDelegate(int durationColumn, QObject *parent = 0);
    13. // void paint(QPainter *painter, const QStyleOptionViewItem &option,
    14. // const QModelIndex &index) const;
    15. QWidget *createEditor(QWidget *parent,
    16. const QStyleOptionViewItem &option,
    17. const QModelIndex &index) const;
    18. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    19. void setModelData(QWidget *editor, QAbstractItemModel *model,
    20. const QModelIndex &index) const;
    21. private slots:
    22. void commitAndCloseEditor();
    23. private:
    24. int durationColumn;
    25. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. // *.cpp
    2. #include "datedelegate.h"
    3. #include <QtGui>
    4.  
    5.  
    6.  
    7. DateDelegate::DateDelegate(int durationColumn, QObject *parent)
    8. : QItemDelegate(parent)
    9. {
    10. this->durationColumn = durationColumn;
    11. }
    12.  
    13. QWidget *DateDelegate::createEditor(QWidget *parent,
    14. const QStyleOptionViewItem &option,
    15. const QModelIndex &index) const
    16. {
    17. if(index.column() == 5 ){
    18. QDateTimeEdit *editor = new QDateTimeEdit(parent);
    19. editor->setDisplayFormat("dd.MM.yyyy");
    20. editor->setCalendarPopup(true);
    21. connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
    22. return editor;
    23.  
    24. }
    25. if(index.column() == 6 ){
    26. QDateTimeEdit *editor = new QDateTimeEdit(parent);
    27. editor->setDisplayFormat("dd.MM.yyyy");
    28. editor->setCalendarPopup(true);
    29. connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
    30. return editor;
    31.  
    32. }
    33. if(index.column() == 7 ){
    34. QDateTimeEdit *editor = new QDateTimeEdit(parent);
    35. editor->setDisplayFormat("dd.MM.yyyy");
    36. editor->setCalendarPopup(true);
    37. connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
    38. return editor;
    39.  
    40. }
    41.  
    42. }
    43.  
    44. void DateDelegate::commitAndCloseEditor()
    45. {
    46. QDateEdit *editor = qobject_cast<QDateEdit *>(sender());
    47. emit commitData(editor);
    48. emit closeEditor(editor);
    49. }
    50.  
    51. void DateDelegate::setEditorData(QWidget *editor,
    52. const QModelIndex &index) const
    53. {
    54. QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
    55. QString value = index.model()->data(index, Qt::EditRole).toString();
    56.  
    57. dateEditor->setDate(QDate::fromString(value , "dd.MM.yyyy"));
    58.  
    59. }
    60.  
    61. void DateDelegate::setModelData(QWidget *editor,
    62. const QModelIndex &index) const
    63. {
    64. QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
    65. QString value = dateEditor->date().toString("dd.MM.yyyy");
    66. model->setData(index, value, Qt::EditRole);
    67. }
    To copy to clipboard, switch view to plain text mode 
    sorry for my english
    thx for help

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget + QDateEdit + Delegates

    What does "doesn't work" mean exactly in your case?

Similar Threads

  1. Problem with receiving events from QDateEdit
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 11:21

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.