Respect the size hint signal for the column and row delegates

The sizeHintChanged() signal from the column and row delegates need to be
connected to the doItemsLayout() slot so that the view is updated when the
size hint changes.

Additionally doDelayedItemsLayout() is called to ensure that this is
up-to-date as the size hints may have changed when the new delegate was
set on the row or column.

Change-Id: I458293f05ce9ef40a03bdbcab1a6e7a10f648c89
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
Reviewed-by: Kevin Puetz <PuetzKevinA@JohnDeere.com>
This commit is contained in:
Andy Shaw 2014-11-11 12:34:13 +01:00 committed by Giuseppe D'Angelo
parent e7e6d6f95a
commit 2ca57e39e3
2 changed files with 48 additions and 2 deletions
src/widgets/itemviews
tests/auto/widgets/itemviews/qabstractitemview

View File

@ -891,6 +891,7 @@ void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *de
disconnect(rowDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
disconnect(rowDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
disconnect(rowDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
}
d->rowDelegates.remove(row);
}
@ -899,10 +900,12 @@ void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate *de
connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
}
d->rowDelegates.insert(row, delegate);
}
viewport()->update();
d->doDelayedItemsLayout();
}
/*!
@ -948,6 +951,7 @@ void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelega
disconnect(columnDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
disconnect(columnDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
disconnect(columnDelegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()));
}
d->columnDelegates.remove(column);
}
@ -956,10 +960,12 @@ void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelega
connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
this, SLOT(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(delegate, SIGNAL(commitData(QWidget*)), this, SLOT(commitData(QWidget*)));
connect(delegate, SIGNAL(sizeHintChanged(QModelIndex)), this, SLOT(doItemsLayout()), Qt::QueuedConnection);
}
d->columnDelegates.insert(column, delegate);
}
viewport()->update();
d->doDelayedItemsLayout();
}
/*!

View File

@ -180,6 +180,16 @@ public:
{ doAutoScroll(); }
};
class GeometriesTestView : public QTableView
{
Q_OBJECT
public:
GeometriesTestView() : QTableView(), updateGeometriesCalled(false) {}
bool updateGeometriesCalled;
protected slots:
void updateGeometries() Q_DECL_OVERRIDE { updateGeometriesCalled = true; QTableView::updateGeometries(); }
};
class tst_QAbstractItemView : public QObject
{
Q_OBJECT
@ -241,6 +251,7 @@ private slots:
void testFocusPolicy();
void QTBUG31411_noSelection();
void QTBUG39324_settingSameInstanceOfIndexWidget();
void sizeHintChangeTriggersLayout();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -248,7 +259,7 @@ class MyAbstractItemDelegate : public QAbstractItemDelegate
public:
MyAbstractItemDelegate() : QAbstractItemDelegate() { calledVirtualDtor = false; }
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const {}
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return QSize(); }
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return size; }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
openedEditor = new QWidget(parent);
@ -259,9 +270,10 @@ public:
calledVirtualDtor = true;
editor->deleteLater();
}
void changeSize() { size = QSize(50, 50); emit sizeHintChanged(QModelIndex()); }
mutable bool calledVirtualDtor;
mutable QWidget *openedEditor;
QSize size;
};
// Testing get/set functions
@ -725,6 +737,34 @@ void tst_QAbstractItemView::columnDelegate()
QCOMPARE(w->metaObject()->className(), "QWidget");
}
void tst_QAbstractItemView::sizeHintChangeTriggersLayout()
{
QStandardItemModel model(4, 4);
MyAbstractItemDelegate delegate;
MyAbstractItemDelegate rowDelegate;
MyAbstractItemDelegate columnDelegate;
GeometriesTestView view;
view.setModel(&model);
view.setItemDelegate(&delegate);
view.setItemDelegateForRow(1, &rowDelegate);
view.setItemDelegateForColumn(2, &columnDelegate);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
view.updateGeometriesCalled = false;
delegate.changeSize();
QCoreApplication::sendPostedEvents();
QVERIFY(view.updateGeometriesCalled);
view.updateGeometriesCalled = false;
rowDelegate.changeSize();
QCoreApplication::sendPostedEvents();
QVERIFY(view.updateGeometriesCalled);
view.updateGeometriesCalled = false;
columnDelegate.changeSize();
QCoreApplication::sendPostedEvents();
QVERIFY(view.updateGeometriesCalled);
}
void tst_QAbstractItemView::selectAll()
{
QStandardItemModel model(4,4);