diff --git a/src/widgets/widgets/qmenubar.cpp b/src/widgets/widgets/qmenubar.cpp index 871af35159..619cb64636 100644 --- a/src/widgets/widgets/qmenubar.cpp +++ b/src/widgets/widgets/qmenubar.cpp @@ -1631,9 +1631,8 @@ QSize QMenuBar::sizeHint() const int margin = 2*vmargin + 2*fw + spaceBelowMenuBar; if(d->leftWidget) { QSize sz = d->leftWidget->sizeHint(); - ret.setWidth(ret.width() + sz.width()); - if(sz.height() + margin > ret.height()) - ret.setHeight(sz.height() + margin); + sz.rheight() += margin; + ret.expandedTo(sz); } if(d->rightWidget) { QSize sz = d->rightWidget->sizeHint(); diff --git a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp index f4e2cc174e..7588950b76 100644 --- a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp +++ b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp @@ -42,6 +42,8 @@ #include #include #include +#include +#include #include #include @@ -83,6 +85,7 @@ public: private slots: void getSetCheck(); + void cleanup(); void clear(); void removeItemAt(); @@ -126,6 +129,8 @@ private slots: #endif void taskQTBUG11823_crashwithInvisibleActions(); void closeOnSecondClick(); + void cornerWidgets_data(); + void cornerWidgets(); protected slots: void onSimpleActivated( QAction*); @@ -195,6 +200,11 @@ void tst_QMenuBar::onSimpleActivated( QAction* action ) m_simpleActivatedCount++; } +void tst_QMenuBar::cleanup() +{ + QVERIFY(QApplication::topLevelWidgets().isEmpty()); +} + // Create a simple menu bar and connect its actions to onSimpleActivated(). TestMenu tst_QMenuBar::initSimpleMenuBar(QMenuBar *mb) @@ -1325,5 +1335,75 @@ void tst_QMenuBar::closeOnSecondClick() // QTBUG-32807, menu should close on 2nd QTRY_VERIFY(!fileMenu->isVisible()); } +Q_DECLARE_METATYPE(Qt::Corner) + +void tst_QMenuBar::cornerWidgets_data() +{ + QTest::addColumn("corner"); + QTest::newRow("left") << Qt::TopLeftCorner; + QTest::newRow("right") << Qt::TopRightCorner; +} + +static QByteArray msgComparison(int v1, const char *op, int v2) +{ + QString result; + QDebug(&result) << v1 << op << v2 << "failed"; + return result.toLocal8Bit(); +} + +void tst_QMenuBar::cornerWidgets() +{ + enum { cornerWidgetWidth = 100 }; + + QFETCH(Qt::Corner, corner); + +#if defined(Q_OS_OSX) || defined(Q_OS_WINCE) + QSKIP("Test interferes with native menu bars on this platform"); +#endif + + QWidget widget; + const QString dataTag = QLatin1String(QTest::currentDataTag()); + widget.setWindowTitle(QLatin1String(QTest::currentTestFunction()) + dataTag); + QVBoxLayout *layout = new QVBoxLayout(&widget); + QMenuBar *menuBar = new QMenuBar(&widget); + layout->addWidget(menuBar); + QMenu *fileMenu = menuBar->addMenu("File"); + fileMenu->addAction("Quit"); + QMenu *editMenu =menuBar->addMenu("Edit"); + editMenu->addAction("Copy"); + centerOnScreen(&widget); + + QLabel *cornerLabel = new QLabel(dataTag); + cornerLabel->setFixedWidth(cornerWidgetWidth); + menuBar->setCornerWidget(cornerLabel, corner); + QCOMPARE(menuBar->cornerWidget(corner), cornerLabel); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + + const QRect fileMenuGeometry = menuBar->actionGeometry(fileMenu->menuAction()); + const QRect editMenuGeometry = menuBar->actionGeometry(editMenu->menuAction()); + const int menuBarWidth = menuBar->width(); + switch (corner) { // QTBUG-36010 , verify corner widget geometry is correct + case Qt::TopLeftCorner: + QVERIFY2(fileMenuGeometry.left() >= cornerWidgetWidth, + msgComparison(fileMenuGeometry.left(), ">=", cornerWidgetWidth)); + QVERIFY2(menuBarWidth - editMenuGeometry.right() < cornerWidgetWidth, + msgComparison(menuBarWidth - editMenuGeometry.right(), "<", cornerWidgetWidth)); + break; + case Qt::TopRightCorner: + QVERIFY2(fileMenuGeometry.left() < cornerWidgetWidth, + msgComparison(fileMenuGeometry.left(), "<", cornerWidgetWidth)); + QVERIFY2(menuBarWidth - editMenuGeometry.right() >= cornerWidgetWidth, + msgComparison(menuBarWidth - editMenuGeometry.right(), ">=", cornerWidgetWidth)); + break; + default: + break; + } + + menuBar->setCornerWidget(0, corner); // Don't crash. + QVERIFY(!menuBar->cornerWidget(corner)); + delete cornerLabel; +} + QTEST_MAIN(tst_QMenuBar) #include "tst_qmenubar.moc"