PDA

View Full Version : QComboBox+QItemDelegate+QDateTimeEdit Problem



bangqianchen
15th September 2008, 03:30
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


#ifndef Inc_ComboBoxDelegate_H
#define Inc_ComboBoxDelegate_H

#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QComboBox>

class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT

public:
ComboBoxDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;

void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif //Inc_ComboBoxDelegate_H


Source file:ComboBoxDelegate.cpp


#include <QtGui>
#include "ComboBoxDelegate.h"

ComboBoxDelegate::ComboBoxDelegate(QObject *parent):QItemDelegate(parent)
{
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("dd/M/yyyy");
editor->setCalendarPopup(true);
return editor;
}

void ComboBoxDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor)
{
dateEditor->setDate(QDate::fromString(index.model()->data(index, Qt::EditRole).toString(), "d/M/yyyy"));
}
}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor)
{
model->setData(index, dateEditor->date().toString("dd/M/yyyy"));
}
}

void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}


when create a new combobox, I set the itemdelegate with ComboBoxDelegate as below:


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

when click the combobox, no QDateTimeEdit was create, it is failed. however, when I use QTableWIdget instead of QComboBox, It is work well.


QTableWidget * plst = new QTableWidget(1,1,this);
plst->setItemDelegate(new ComboBoxDelegate(this));

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!

aamer4yu
15th September 2008, 06:23
Are you using the same delegate instance twice ??
Thats what the warning tells. And also the warning is mentioned in Qt Assistant too.

bangqianchen
15th September 2008, 11:14
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.