Results 1 to 3 of 3

Thread: QComboBox+QItemDelegate+QDateTimeEdit Problem

  1. #1
    Join Date
    Aug 2008
    Posts
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QComboBox+QItemDelegate+QDateTimeEdit Problem

    I want to implement a widget that click the combobox, a QDateTimeEdit widget is showing under the qcombobox's list. the fellowing is my codes:

    head file:ComboBoxDelegate.h
    Qt Code:
    1. #ifndef Inc_ComboBoxDelegate_H
    2. #define Inc_ComboBoxDelegate_H
    3.  
    4. #include <QItemDelegate>
    5. #include <QModelIndex>
    6. #include <QObject>
    7. #include <QSize>
    8. #include <QComboBox>
    9.  
    10. class ComboBoxDelegate : public QItemDelegate
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. ComboBoxDelegate(QObject *parent = 0);
    16.  
    17. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    18. const QModelIndex &index) const;
    19.  
    20. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    21. void setModelData(QWidget *editor, QAbstractItemModel *model,
    22. const QModelIndex &index) const;
    23.  
    24. void updateEditorGeometry(QWidget *editor,
    25. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    26. };
    27.  
    28. #endif //Inc_ComboBoxDelegate_H
    To copy to clipboard, switch view to plain text mode 

    Source file:ComboBoxDelegate.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "ComboBoxDelegate.h"
    3.  
    4. ComboBoxDelegate::ComboBoxDelegate(QObject *parent):QItemDelegate(parent)
    5. {
    6. }
    7.  
    8. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    9. const QStyleOptionViewItem &/* option */,
    10. const QModelIndex &/* index */) const
    11. {
    12. QDateTimeEdit *editor = new QDateTimeEdit(parent);
    13. editor->setDisplayFormat("dd/M/yyyy");
    14. editor->setCalendarPopup(true);
    15. return editor;
    16. }
    17.  
    18. void ComboBoxDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
    19. {
    20. QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
    21. if (dateEditor)
    22. {
    23. dateEditor->setDate(QDate::fromString(index.model()->data(index, Qt::EditRole).toString(), "d/M/yyyy"));
    24. }
    25. }
    26.  
    27. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    28. const QModelIndex &index) const
    29. {
    30. QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
    31. if (dateEditor)
    32. {
    33. model->setData(index, dateEditor->date().toString("dd/M/yyyy"));
    34. }
    35. }
    36.  
    37. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
    38. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    39. {
    40. editor->setGeometry(option.rect);
    41. }
    To copy to clipboard, switch view to plain text mode 

    when create a new combobox, I set the itemdelegate with ComboBoxDelegate as below:
    Qt Code:
    1. QComboBox* pCmb = new QComboBox(this);
    2. pCmb ->setItemDelegate(new ComboBoxDelegate(this));
    To copy to clipboard, switch view to plain text mode 
    when click the combobox, no QDateTimeEdit was create, it is failed. however, when I use QTableWIdget instead of QComboBox, It is work well.
    Qt Code:
    1. QTableWidget * plst = new QTableWidget(1,1,this);
    2. plst->setItemDelegate(new ComboBoxDelegate(this));
    To copy to clipboard, switch view to plain text mode 
    what's wrong? The QComboBox's setItemDelegate member function has warning infomation :
    Warning: You should not share the same instance of a delegate between comboboxes, widget mappers or views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() signal, and attempt to access, modify or close an editor that has already been closed.
    the qt assistant does not supply more infomation about this, and I don't know how to solve this problem, can you give me a favor? thank you!
    Last edited by jpn; 15th September 2008 at 06:03. Reason: missing [code] tags

  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: QComboBox+QItemDelegate+QDateTimeEdit Problem

    Are you using the same delegate instance twice ??
    Thats what the warning tells. And also the warning is mentioned in Qt Assistant too.

  3. #3
    Join Date
    Aug 2008
    Posts
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: QComboBox+QItemDelegate+QDateTimeEdit Problem

    I don't using the same instance twice, the setItemDelegate member function exectue only onice.

    1. QComboBox* pCmb = new QComboBox(this);
    2. pCmb ->setItemDelegate(new ComboBoxDelegate(this));

    you can coding a sample example to test ComboBoxDelegate class.

Similar Threads

  1. Editable QComboBox with QItemDelegate
    By Jmgr in forum Qt Programming
    Replies: 11
    Last Post: 10th December 2008, 09:21
  2. QComboBox problem
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 31st March 2008, 11:43
  3. Problem while using QComboBox
    By merry in forum Qt Programming
    Replies: 6
    Last Post: 20th December 2007, 10:22
  4. Problem with QComboBox
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th December 2007, 13:21
  5. QItemDelegate problem
    By WinchellChung in forum Newbie
    Replies: 2
    Last Post: 5th December 2007, 16:16

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.