Fix result handling in QDialog::done

The setData method of an item view would get an incorrect value of a
QDialog's result. This patch changes the order of functions called to
fix that.

[ChangeLog][QtWidgets][QDialog] Fixed a bug where accessing the result
of QDialog's result could yield an incorrect value in some situation
like using it as a delegate for item views.

Task-number: QTBUG-6018
Task-number: QTBUG-12156
Task-number: QTBUG-14430
Change-Id: I6ee4b6e8cacf6a806631c05c6c5dbcff925df65e
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
bb10
Samuel Gaist 2016-08-29 09:59:46 +02:00 committed by Shawn Rutledge
parent d0b8356e7e
commit 8b1d9d308b
2 changed files with 58 additions and 1 deletions

View File

@ -557,8 +557,8 @@ int QDialog::exec()
void QDialog::done(int r)
{
Q_D(QDialog);
hide();
setResult(r);
hide();
d->close_helper(QWidgetPrivate::CloseNoEvent);
d->resetModalitySetByOpen();

View File

@ -52,6 +52,7 @@
#include <qstringlistmodel.h>
#include <qsortfilterproxymodel.h>
#include <qproxystyle.h>
#include <qdialog.h>
static inline void setFrameless(QWidget *w)
{
@ -149,6 +150,7 @@ private slots:
void QTBUG50535_update_on_new_selection_model();
void testSelectionModelInSyncWithView();
void testClickToSelect();
void testDialogAsEditor();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -173,6 +175,29 @@ public:
QSize size;
};
class DialogItemDelegate : public QStyledItemDelegate
{
public:
DialogItemDelegate() : QStyledItemDelegate() { }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
openedEditor = new QDialog(parent);
return openedEditor;
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
Q_UNUSED(model)
Q_UNUSED(index)
QDialog *dialog = qobject_cast<QDialog *>(editor);
result = static_cast<QDialog::DialogCode>(dialog->result());
}
mutable QDialog::DialogCode result;
mutable QDialog *openedEditor;
};
// Testing get/set functions
void tst_QAbstractItemView::getSetCheck()
{
@ -2156,5 +2181,37 @@ void tst_QAbstractItemView::testClickToSelect()
QCOMPARE(spy.back().front().value<QRect>(), QRect(nearCenterA, QSize(1, 1)));
}
void tst_QAbstractItemView::testDialogAsEditor()
{
DialogItemDelegate delegate;
QStandardItemModel model;
model.appendRow(new QStandardItem(QStringLiteral("editme")));
QListView view;
view.setItemDelegate(&delegate);
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
view.edit(model.index(0,0));
QVERIFY(QTest::qWaitForWindowExposed(delegate.openedEditor));
delegate.openedEditor->reject();
QApplication::processEvents();
QCOMPARE(delegate.result, QDialog::Rejected);
view.edit(model.index(0,0));
QVERIFY(QTest::qWaitForWindowExposed(delegate.openedEditor));
delegate.openedEditor->accept();
QApplication::processEvents();
QCOMPARE(delegate.result, QDialog::Accepted);
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"