From 75285b64ada65748f1fee3942a95b46a3f0b9d7e Mon Sep 17 00:00:00 2001 From: Wang Chuan Date: Sun, 26 Jan 2020 14:21:42 +0800 Subject: [PATCH] QComboBox: send font change event to popup menu when font changed The font change in QComboBox might cause incorrect appearance of popup menu since it doesn't notify popup menu to relayout itself Fixes the issue by send font change event to the item view of popup menu when received a font change event in QComboBox Fixes: QTBUG-75846 Change-Id: I4821015cca95a7e233a22262596a6fbf27f10aef Reviewed-by: Richard Moe Gustavsen --- src/widgets/widgets/qcombobox.cpp | 4 +- .../widgets/qcombobox/tst_qcombobox.cpp | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index 9a0e969e1c..fdf1a87bba 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -3045,12 +3045,14 @@ void QComboBox::changeEvent(QEvent *e) d->updateViewContainerPaletteAndOpacity(); break; } - case QEvent::FontChange: + case QEvent::FontChange: { d->sizeHint = QSize(); // invalidate size hint d->viewContainer()->setFont(font()); + d->viewContainer()->itemView()->doItemsLayout(); if (d->lineEdit) d->updateLineEditGeometry(); break; + } default: break; } diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index b7869a0653..5ab59dce93 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -164,6 +165,7 @@ private slots: void task_QTBUG_56693_itemFontFromModel(); void inputMethodUpdate(); void task_QTBUG_52027_mapCompleterIndex(); + void checkMenuItemPosWhenStyleSheetIsSet(); private: PlatformInputContext m_platformInputContext; @@ -3466,5 +3468,46 @@ void tst_QComboBox::task_QTBUG_52027_mapCompleterIndex() QCOMPARE(arguments.at(0).toInt(), 1); } +void tst_QComboBox::checkMenuItemPosWhenStyleSheetIsSet() +{ + QString newCss = "QComboBox {font-size: 18pt;}"; + QString oldCss = qApp->styleSheet(); + qApp->setStyleSheet(newCss); + + QWidget topLevel; + QVBoxLayout *layout = new QVBoxLayout(&topLevel); + QStackedWidget *stack = new QStackedWidget(&topLevel); + layout->addWidget(stack); + QWidget *container = new QWidget(&topLevel); + QHBoxLayout *cLayout = new QHBoxLayout(container); + QComboBox *cBox = new QComboBox; + + QStandardItemModel *model = new QStandardItemModel(cBox); + QStandardItem *item = new QStandardItem(QStringLiteral("Item1")); + model->appendRow(item); + item = new QStandardItem(QStringLiteral("Item2")); + model->appendRow(item); + item = new QStandardItem(QStringLiteral("Item3")); + model->appendRow(item); + item = new QStandardItem(QStringLiteral("Item4")); + model->appendRow(item); + cBox->setModel(model); + + cLayout->addWidget(cBox); + stack->addWidget(container); + topLevel.show(); + cBox->showPopup(); + + QTRY_VERIFY(cBox->view()); + QTRY_VERIFY(cBox->view()->isVisible()); + + int menuHeight = cBox->view()->geometry().height(); + QRect menuItemRect = cBox->view()->visualRect(model->indexFromItem(item)); + + QCOMPARE(menuHeight, menuItemRect.y() + menuItemRect.height()); + + qApp->setStyleSheet(oldCss); +} + QTEST_MAIN(tst_QComboBox) #include "tst_qcombobox.moc"