[Qt-interest] QDateEdit unable to popup calendar widget in QTableView
Ibic Dev
ibicdev at gmail.com
Tue Jul 28 19:26:24 CEST 2009
Hi,
I followed the Spin Box Delegate Example when I was trying to put
QDateEdit in QTableView; my hope was to use QDateEdit's calendar
widget to edit the date column in QTableView. However, the calendar
just couldn't be popped up despite that I called QDateEdit's
setCalendarPopup(true) method. The calendar seemed to appear for
short moment then completely gone. QDateEdit worked otherwise (e.g.,
the fields could be edited using spin boxes) if I setCalendarPopup to
false.
Anyone knows what was wrong? I searched Qt Interest Archives and
found no answer.
Thanks in advance.
Ibic
The following is my code snippet:
======
// Custome Item Delegate using QDateEdit as editor
class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(QObject *parent=0)
: QItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignCenter | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, text);
drawFocus(painter, myOption, myOption.rect);
}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QDateEdit *dateEdit = new QDateEdit(parent);
dateEdit->setDisplayFormat("yyyy-MM-dd");
dateEdit->setCalendarPopup(true); // enable calendar
return dateEdit;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const
{
QDate date = index.model()->data(index, Qt::DisplayRole).toDate();
if (date == QDate()) date = QDate::currentDate();
QDateEdit *dateEdit = qobject_cast<QDateEdit *>(editor);
dateEdit->setDate(date);
}
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateEdit *dateEdit = qobject_cast<QDateEdit *>(editor);
QDate date = dateEdit->date();
model->setData(index, date);
}
};
QSqlTableModel *model = new QSqlTableModel;
// irrelevant code skipped
QTableView *view = new QTableView;
view->setModel(model);
view->setItemDelegateForColumn(1, new DateDelegate(creditView));
// irrelevant code skipped
More information about the Qt-interest-old
mailing list