From c3e1abad4e141e6e9d876e5cff194c473a2654eb Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 21 Mar 2012 20:41:57 +0100 Subject: [PATCH] Add USER properties to QDateEdit and QTimeEdit. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Both classes had such components before, but there were issues with the NOTIFY signal not being in the same class as the Q_PROPERTY. This patch solves that problem by using a signal of a different name. Task-number: QTBUG-15731 Change-Id: Ibc7ce4dba8a6b88c05d62a90e14d0101c5cd3082 Reviewed-by: Olivier Goffart Reviewed-by: Thorbjørn Lund Martsum --- dist/changes-5.0.0 | 5 +++++ src/widgets/itemviews/qitemdelegate.cpp | 12 ------------ src/widgets/itemviews/qstyleditemdelegate.cpp | 12 ------------ src/widgets/widgets/qdatetimeedit.cpp | 20 ++++++++++++++++++++ src/widgets/widgets/qdatetimeedit.h | 8 ++++++++ .../itemviews/qitemdelegate/tst_qitemdelegate.cpp | 10 ++++++++++ 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index ab9b80c..bb01da3 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -395,6 +395,11 @@ QtWidgets * ResizeMode resizeMode(int logicalindex) const - use sectionResizeMode(int logicalindex) instead. +* QDateEdit and QTimeEdit have re-gained a USER property. These were originally removed + before Qt 4.7.0, and are re-added for 5.0. This means that the userProperty for + those classes are now QDate and QTime respectively, not QDateTime as they have been + for the 4.7 and 4.8 releases. + QtNetwork --------- * QHostAddress::isLoopback() API added. Returns true if the address is diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp index 419c62f..6c62378 100644 --- a/src/widgets/itemviews/qitemdelegate.cpp +++ b/src/widgets/itemviews/qitemdelegate.cpp @@ -555,18 +555,6 @@ void QItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) con QVariant v = index.data(Qt::EditRole); QByteArray n = editor->metaObject()->userProperty().name(); - // ### Qt 5: remove - // A work-around for missing "USER true" in qdatetimeedit.h for - // QTimeEdit's time property and QDateEdit's date property. - // It only triggers if the default user property "dateTime" is - // reported for QTimeEdit and QDateEdit. - if (n == "dateTime") { - if (editor->inherits("QTimeEdit")) - n = "time"; - else if (editor->inherits("QDateEdit")) - n = "date"; - } - // ### Qt 5: give QComboBox a USER property if (n.isEmpty() && editor->inherits("QComboBox")) n = d->editorFactory()->valuePropertyName(v.userType()); diff --git a/src/widgets/itemviews/qstyleditemdelegate.cpp b/src/widgets/itemviews/qstyleditemdelegate.cpp index 93893af..b27dcb0 100644 --- a/src/widgets/itemviews/qstyleditemdelegate.cpp +++ b/src/widgets/itemviews/qstyleditemdelegate.cpp @@ -492,18 +492,6 @@ void QStyledItemDelegate::setEditorData(QWidget *editor, const QModelIndex &inde QVariant v = index.data(Qt::EditRole); QByteArray n = editor->metaObject()->userProperty().name(); - // ### Qt 5: remove - // A work-around for missing "USER true" in qdatetimeedit.h for - // QTimeEdit's time property and QDateEdit's date property. - // It only triggers if the default user property "dateTime" is - // reported for QTimeEdit and QDateEdit. - if (n == "dateTime") { - if (editor->inherits("QTimeEdit")) - n = "time"; - else if (editor->inherits("QDateEdit")) - n = "date"; - } - // ### Qt 5: give QComboBox a USER property if (n.isEmpty() && editor->inherits("QComboBox")) n = d->editorFactory()->valuePropertyName(v.userType()); diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 3d0996a..5e808c1 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -1549,6 +1549,7 @@ void QDateTimeEdit::mousePressEvent(QMouseEvent *event) QTimeEdit::QTimeEdit(QWidget *parent) : QDateTimeEdit(QDATETIMEEDIT_TIME_MIN, QVariant::Time, parent) { + connect(this, SIGNAL(timeChanged(QTime)), SIGNAL(userTimeChanged(QTime))); } /*! @@ -1561,6 +1562,15 @@ QTimeEdit::QTimeEdit(const QTime &time, QWidget *parent) { } +/*! + \fn void QTimeEdit::userTimeChanged(const QTime &time) + + This signal only exists to fully implement the time Q_PROPERTY on the class. + Normally timeChanged should be used instead. + + \internal +*/ + /*! \class QDateEdit @@ -1603,6 +1613,7 @@ QTimeEdit::QTimeEdit(const QTime &time, QWidget *parent) QDateEdit::QDateEdit(QWidget *parent) : QDateTimeEdit(QDATETIMEEDIT_DATE_INITIAL, QVariant::Date, parent) { + connect(this, SIGNAL(dateChanged(QDate)), SIGNAL(userDateChanged(QDate))); } /*! @@ -1615,6 +1626,15 @@ QDateEdit::QDateEdit(const QDate &date, QWidget *parent) { } +/*! + \fn void QDateEdit::userDateChanged(const QDate &date) + + This signal only exists to fully implement the date Q_PROPERTY on the class. + Normally dateChanged should be used instead. + + \internal +*/ + // --- QDateTimeEditPrivate --- diff --git a/src/widgets/widgets/qdatetimeedit.h b/src/widgets/widgets/qdatetimeedit.h index ffb8503..07fc2b0 100644 --- a/src/widgets/widgets/qdatetimeedit.h +++ b/src/widgets/widgets/qdatetimeedit.h @@ -205,17 +205,25 @@ private: class Q_WIDGETS_EXPORT QTimeEdit : public QDateTimeEdit { Q_OBJECT + Q_PROPERTY(QTime time READ time WRITE setTime NOTIFY userTimeChanged USER true) public: QTimeEdit(QWidget *parent = 0); QTimeEdit(const QTime &time, QWidget *parent = 0); + +Q_SIGNALS: + void userTimeChanged(const QTime &time); }; class Q_WIDGETS_EXPORT QDateEdit : public QDateTimeEdit { Q_OBJECT + Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY userDateChanged USER true) public: QDateEdit(QWidget *parent = 0); QDateEdit(const QDate &date, QWidget *parent = 0); + +Q_SIGNALS: + void userDateChanged(const QDate &date); }; Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeEdit::Sections) diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp index 2b52f5f..497cdfd 100644 --- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp @@ -780,6 +780,9 @@ void tst_QItemDelegate::dateTimeEditor() QTimeEdit *timeEditor = qFindChild(widget.viewport()); QVERIFY(timeEditor); QCOMPARE(timeEditor->time(), time); + // The data must actually be different in order for the model + // to be updated. + timeEditor->setTime(time.addSecs(60)); widget.clearFocus(); qApp->setActiveWindow(&widget); @@ -791,6 +794,7 @@ void tst_QItemDelegate::dateTimeEditor() QDateEdit *dateEditor = qFindChild(widget.viewport()); QVERIFY(dateEditor); QCOMPARE(dateEditor->date(), date); + dateEditor->setDate(date.addDays(60)); widget.clearFocus(); widget.setFocus(); @@ -806,6 +810,12 @@ void tst_QItemDelegate::dateTimeEditor() QVERIFY(dateTimeEditor); QCOMPARE(dateTimeEditor->date(), date); QCOMPARE(dateTimeEditor->time(), time); + dateTimeEditor->setTime(time.addSecs(600)); + widget.clearFocus(); + + QVERIFY(item1->data(Qt::EditRole).userType() == QMetaType::QTime); + QVERIFY(item2->data(Qt::EditRole).userType() == QMetaType::QDate); + QVERIFY(item3->data(Qt::EditRole).userType() == QMetaType::QDateTime); } void tst_QItemDelegate::decoration_data() -- 2.7.4