summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/styles
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2019-10-14 17:46:16 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2019-10-14 17:46:34 +0200
commit440286655e0ca271506cf7cc02ad0dbf4baef9ca (patch)
tree896fa81adb8b14a69355a3a6cf64d06ec8173c9a /tests/auto/widgets/styles
parent1e27ad1697187549151657ba187928e439300db7 (diff)
parente164d61ca8263fc4b46fdd916e1ea77c7dd2b735 (diff)
Merge remote-tracking branch 'origin/dev' into wip/cmake
Diffstat (limited to 'tests/auto/widgets/styles')
-rw-r--r--tests/auto/widgets/styles/qstyle/tst_qstyle.cpp262
-rw-r--r--tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp407
2 files changed, 356 insertions, 313 deletions
diff --git a/tests/auto/widgets/styles/qstyle/tst_qstyle.cpp b/tests/auto/widgets/styles/qstyle/tst_qstyle.cpp
index ae084310b1..4cec54856f 100644
--- a/tests/auto/widgets/styles/qstyle/tst_qstyle.cpp
+++ b/tests/auto/widgets/styles/qstyle/tst_qstyle.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@@ -67,15 +67,9 @@ using namespace QTestPrivate;
class tst_QStyle : public QObject
{
Q_OBJECT
-public:
- tst_QStyle();
-private:
- bool testAllFunctions(QStyle *);
- bool testScrollBarSubControls();
private slots:
void drawItemPixmap();
- void init();
void cleanup();
#ifndef QT_NO_STYLE_FUSION
void testFusionStyle();
@@ -100,38 +94,29 @@ private slots:
void testProxyCalled();
void testStyleOptionInit();
private:
+ bool testAllFunctions(QStyle *);
+ bool testScrollBarSubControls(const QStyle *style);
+ void testPainting(QStyle *style, const QString &platform);
void lineUpLayoutTest(QStyle *);
- QWidget *testWidget;
};
-
-tst_QStyle::tst_QStyle()
-{
- testWidget = 0;
-}
-
class MyWidget : public QWidget
{
public:
- MyWidget( QWidget* QWidget=0, const char* name=0 );
+ using QWidget::QWidget;
+
protected:
- void paintEvent( QPaintEvent* );
+ void paintEvent(QPaintEvent *) override;
};
-void tst_QStyle::init()
-{
- testWidget = new MyWidget( 0, "testObject");
-}
-
void tst_QStyle::cleanup()
{
- delete testWidget;
- testWidget = 0;
+ QVERIFY(QApplication::topLevelWidgets().isEmpty());
}
void tst_QStyle::testStyleFactory()
{
- QStringList keys = QStyleFactory::keys();
+ const QStringList keys = QStyleFactory::keys();
#ifndef QT_NO_STYLE_FUSION
QVERIFY(keys.contains("Fusion"));
#endif
@@ -139,17 +124,17 @@ void tst_QStyle::testStyleFactory()
QVERIFY(keys.contains("Windows"));
#endif
- foreach (QString styleName , keys) {
- QStyle *style = QStyleFactory::create(styleName);
- QVERIFY2(style != 0, qPrintable(QString::fromLatin1("Fail to load style '%1'").arg(styleName)));
- delete style;
+ for (const QString &styleName : keys) {
+ QScopedPointer<QStyle> style(QStyleFactory::create(styleName));
+ QVERIFY2(!style.isNull(),
+ qPrintable(QString::fromLatin1("Fail to load style '%1'").arg(styleName)));
}
}
class CustomProxy : public QProxyStyle
{
- virtual int pixelMetric(PixelMetric metric, const QStyleOption *option = 0,
- const QWidget *widget = 0) const
+ int pixelMetric(PixelMetric metric, const QStyleOption *option = nullptr,
+ const QWidget *widget = nullptr) const override
{
if (metric == QStyle::PM_ButtonIconSize)
return 13;
@@ -170,9 +155,9 @@ void tst_QStyle::testProxyStyle()
QCOMPARE(proxyStyle->baseStyle(), style);
QVERIFY(testAllFunctions(proxyStyle));
- proxyStyle->setBaseStyle(0);
+ proxyStyle->setBaseStyle(nullptr);
QVERIFY(proxyStyle->baseStyle());
- qApp->setStyle(proxyStyle);
+ QApplication::setStyle(proxyStyle);
QProxyStyle* baseStyle = new QProxyStyle("Windows");
QCOMPARE(baseStyle->baseStyle()->objectName(), style->objectName());
@@ -189,10 +174,12 @@ void tst_QStyle::testProxyStyle()
void tst_QStyle::drawItemPixmap()
{
- testWidget->resize(300, 300);
- testWidget->showNormal();
+ MyWidget testWidget;
+ testWidget.setObjectName("testObject");
+ testWidget.resize(300, 300);
+ testWidget.showNormal();
- QImage image = testWidget->grab().toImage();
+ QImage image = testWidget.grab().toImage();
const QRgb green = QColor(Qt::green).rgb();
QVERIFY(image.reinterpretAsFormat(QImage::Format_RGB32));
const QRgb *bits = reinterpret_cast<const QRgb *>(image.constBits());
@@ -201,33 +188,33 @@ void tst_QStyle::drawItemPixmap()
QEXPECT_FAIL("", "QWidget::resize does not work on WinRT", Continue);
#endif
QVERIFY(std::all_of(bits, end, [green] (QRgb r) { return r == green; }));
- testWidget->hide();
}
bool tst_QStyle::testAllFunctions(QStyle *style)
{
QStyleOption opt;
- opt.init(testWidget);
+ QWidget testWidget;
+ opt.init(&testWidget);
- testWidget->setStyle(style);
+ testWidget.setStyle(style);
//Tests styleHint with default arguments for potential crashes
for ( int hint = 0 ; hint < int(QStyle::SH_Menu_Mask); ++hint) {
style->styleHint(QStyle::StyleHint(hint));
- style->styleHint(QStyle::StyleHint(hint), &opt, testWidget);
+ style->styleHint(QStyle::StyleHint(hint), &opt, &testWidget);
}
//Tests pixelMetric with default arguments for potential crashes
for ( int pm = 0 ; pm < int(QStyle::PM_LayoutVerticalSpacing); ++pm) {
style->pixelMetric(QStyle::PixelMetric(pm));
- style->pixelMetric(QStyle::PixelMetric(pm), &opt, testWidget);
+ style->pixelMetric(QStyle::PixelMetric(pm), &opt, &testWidget);
}
//Tests drawControl with default arguments for potential crashes
for ( int control = 0 ; control < int(QStyle::CE_ColumnViewGrip); ++control) {
QPixmap surface(QSize(200, 200));
QPainter painter(&surface);
- style->drawControl(QStyle::ControlElement(control), &opt, &painter, 0);
+ style->drawControl(QStyle::ControlElement(control), &opt, &painter, nullptr);
}
//Tests drawComplexControl with default arguments for potential crashes
@@ -235,35 +222,35 @@ bool tst_QStyle::testAllFunctions(QStyle *style)
QPixmap surface(QSize(200, 200));
QPainter painter(&surface);
QStyleOptionComboBox copt1;
- copt1.init(testWidget);
+ copt1.init(&testWidget);
QStyleOptionGroupBox copt2;
- copt2.init(testWidget);
+ copt2.init(&testWidget);
QStyleOptionSizeGrip copt3;
- copt3.init(testWidget);
+ copt3.init(&testWidget);
QStyleOptionSlider copt4;
- copt4.init(testWidget);
+ copt4.init(&testWidget);
copt4.minimum = 0;
copt4.maximum = 100;
copt4.tickInterval = 25;
copt4.sliderValue = 50;
QStyleOptionSpinBox copt5;
- copt5.init(testWidget);
+ copt5.init(&testWidget);
QStyleOptionTitleBar copt6;
- copt6.init(testWidget);
+ copt6.init(&testWidget);
QStyleOptionToolButton copt7;
- copt7.init(testWidget);
+ copt7.init(&testWidget);
QStyleOptionComplex copt9;
- copt9.initFrom(testWidget);
-
- style->drawComplexControl(QStyle::CC_SpinBox, &copt5, &painter, 0);
- style->drawComplexControl(QStyle::CC_ComboBox, &copt1, &painter, 0);
- style->drawComplexControl(QStyle::CC_ScrollBar, &copt4, &painter, 0);
- style->drawComplexControl(QStyle::CC_Slider, &copt4, &painter, 0);
- style->drawComplexControl(QStyle::CC_ToolButton, &copt7, &painter, 0);
- style->drawComplexControl(QStyle::CC_TitleBar, &copt6, &painter, 0);
- style->drawComplexControl(QStyle::CC_GroupBox, &copt2, &painter, 0);
- style->drawComplexControl(QStyle::CC_Dial, &copt4, &painter, 0);
+ copt9.initFrom(&testWidget);
+
+ style->drawComplexControl(QStyle::CC_SpinBox, &copt5, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_ComboBox, &copt1, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_ScrollBar, &copt4, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_Slider, &copt4, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_ToolButton, &copt7, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_TitleBar, &copt6, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_GroupBox, &copt2, &painter, nullptr);
+ style->drawComplexControl(QStyle::CC_Dial, &copt4, &painter, nullptr);
}
//Check standard pixmaps/icons
@@ -279,20 +266,21 @@ bool tst_QStyle::testAllFunctions(QStyle *style)
}
style->itemPixmapRect(QRect(0, 0, 100, 100), Qt::AlignHCenter, QPixmap(200, 200));
- style->itemTextRect(QFontMetrics(qApp->font()), QRect(0, 0, 100, 100), Qt::AlignHCenter, true, QString("Test"));
+ style->itemTextRect(QFontMetrics(QApplication::font()), QRect(0, 0, 100, 100),
+ Qt::AlignHCenter, true, QLatin1String("Test"));
- return testScrollBarSubControls();
+ return testScrollBarSubControls(style);
}
-bool tst_QStyle::testScrollBarSubControls()
+bool tst_QStyle::testScrollBarSubControls(const QStyle *style)
{
- const auto *style = testWidget->style();
- const bool isMacStyle = style->objectName().toLower() == "macintosh";
+ const bool isMacStyle = style->objectName().compare(QLatin1String("macintosh"),
+ Qt::CaseInsensitive) == 0;
QScrollBar scrollBar;
setFrameless(&scrollBar);
scrollBar.show();
const QStyleOptionSlider opt = qt_qscrollbarStyleOption(&scrollBar);
- foreach (int sc, QList<int>() << 1 << 2 << 4 << 8) {
+ for (int sc : {1, 2, 4, 8}) {
const auto subControl = static_cast<QStyle::SubControl>(sc);
const QRect sr = style->subControlRect(QStyle::CC_ScrollBar, &opt, subControl, &scrollBar);
if (sr.isNull()) {
@@ -310,34 +298,34 @@ bool tst_QStyle::testScrollBarSubControls()
#ifndef QT_NO_STYLE_FUSION
void tst_QStyle::testFusionStyle()
{
- QStyle *fstyle = QStyleFactory::create("Fusion");
- QVERIFY(testAllFunctions(fstyle));
- lineUpLayoutTest(fstyle);
- delete fstyle;
+ QScopedPointer<QStyle> fstyle(QStyleFactory::create("Fusion"));
+ QVERIFY(!fstyle.isNull());
+ QVERIFY(testAllFunctions(fstyle.data()));
+ lineUpLayoutTest(fstyle.data());
}
#endif
void tst_QStyle::testWindowsStyle()
{
- QStyle *wstyle = QStyleFactory::create("Windows");
- QVERIFY(testAllFunctions(wstyle));
- lineUpLayoutTest(wstyle);
+ QScopedPointer<QStyle> wstyle(QStyleFactory::create("Windows"));
+ QVERIFY(!wstyle.isNull());
+ QVERIFY(testAllFunctions(wstyle.data()));
+ lineUpLayoutTest(wstyle.data());
// Tests drawing indeterminate progress with 0 size: QTBUG-15973
QStyleOptionProgressBar pb;
pb.rect = QRect(0,0,-9,0);
QPixmap surface(QSize(200, 200));
QPainter painter(&surface);
- wstyle->drawControl(QStyle::CE_ProgressBar, &pb, &painter, 0);
- delete wstyle;
+ wstyle->drawControl(QStyle::CE_ProgressBar, &pb, &painter, nullptr);
}
#if defined(Q_OS_WIN) && !defined(QT_NO_STYLE_WINDOWSVISTA) && !defined(Q_OS_WINRT)
void tst_QStyle::testWindowsVistaStyle()
{
- QStyle *vistastyle = QStyleFactory::create("WindowsVista");
- QVERIFY(testAllFunctions(vistastyle));
- delete vistastyle;
+ QScopedPointer<QStyle> vistastyle(QStyleFactory::create("WindowsVista"));
+ QVERIFY(!vistastyle.isNull());
+ QVERIFY(testAllFunctions(vistastyle.data()));
}
#endif
@@ -351,14 +339,7 @@ void tst_QStyle::testMacStyle()
#endif
// Helper class...
-
-MyWidget::MyWidget( QWidget* parent, const char* name )
- : QWidget( parent )
-{
- setObjectName(name);
-}
-
-void MyWidget::paintEvent( QPaintEvent* )
+void MyWidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
QPixmap big(400,400);
@@ -371,20 +352,12 @@ class Qt42Style : public QCommonStyle
{
Q_OBJECT
public:
- Qt42Style() : QCommonStyle()
- {
- margin_toplevel = 10;
- margin = 5;
- spacing = 0;
- }
-
- virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0,
- const QWidget * widget = 0 ) const;
-
- int margin_toplevel;
- int margin;
- int spacing;
+ int pixelMetric(PixelMetric metric, const QStyleOption *option = nullptr,
+ const QWidget *widget = nullptr) const override;
+ int margin_toplevel = 10;
+ int margin = 5;
+ int spacing = 0;
};
int Qt42Style::pixelMetric(PixelMetric metric, const QStyleOption * /* option = 0*/,
@@ -393,13 +366,10 @@ int Qt42Style::pixelMetric(PixelMetric metric, const QStyleOption * /* option =
switch (metric) {
case QStyle::PM_DefaultTopLevelMargin:
return margin_toplevel;
- break;
case QStyle::PM_DefaultChildMargin:
return margin;
- break;
case QStyle::PM_DefaultLayoutSpacing:
return spacing;
- break;
default:
break;
}
@@ -409,7 +379,7 @@ int Qt42Style::pixelMetric(PixelMetric metric, const QStyleOption * /* option =
void tst_QStyle::pixelMetric()
{
- Qt42Style *style = new Qt42Style();
+ QScopedPointer<Qt42Style> style(new Qt42Style);
QCOMPARE(style->pixelMetric(QStyle::PM_DefaultTopLevelMargin), 10);
QCOMPARE(style->pixelMetric(QStyle::PM_DefaultChildMargin), 5);
QCOMPARE(style->pixelMetric(QStyle::PM_DefaultLayoutSpacing), 0);
@@ -427,8 +397,6 @@ void tst_QStyle::pixelMetric()
QCOMPARE(style->pixelMetric(QStyle::PM_DefaultTopLevelMargin), -1);
QCOMPARE(style->pixelMetric(QStyle::PM_DefaultChildMargin), -1);
QCOMPARE(style->pixelMetric(QStyle::PM_DefaultLayoutSpacing), -1);
-
- delete style;
}
#if !defined(QT_NO_STYLE_WINDOWS) && !defined(QT_NO_STYLE_FUSION)
@@ -474,10 +442,11 @@ void tst_QStyle::lineUpLayoutTest(QStyle *style)
layout.addWidget(&lineedit);
layout.addWidget(&combo);
widget.setLayout(&layout);
- widget.setStyle(style);
- // propagate the style.
- foreach (QWidget *w, widget.findChildren<QWidget *>())
- w->setStyle(style);
+ widget.setStyle(style);
+ // propagate the style.
+ const auto children = widget.findChildren<QWidget *>();
+ for (QWidget *w : children)
+ w->setStyle(style);
widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget));
@@ -498,31 +467,32 @@ void tst_QStyle::lineUpLayoutTest(QStyle *style)
void tst_QStyle::defaultFont()
{
- QFont defaultFont = qApp->font();
+ QFont defaultFont = QApplication::font();
QFont pointFont = defaultFont;
pointFont.setPixelSize(9);
- qApp->setFont(pointFont);
+ QApplication::setFont(pointFont);
QPushButton button;
setFrameless(&button);
button.show();
- qApp->processEvents();
- qApp->setFont(defaultFont);
+ QCoreApplication::processEvents();
+ QApplication::setFont(defaultFont);
}
class DrawTextStyle : public QProxyStyle
{
Q_OBJECT
public:
- DrawTextStyle(QStyle *base = 0) : QProxyStyle(), alignment(0) { setBaseStyle(base); }
+ using QProxyStyle::QProxyStyle;
+
void drawItemText(QPainter *painter, const QRect &rect,
- int flags, const QPalette &pal, bool enabled,
- const QString &text, QPalette::ColorRole textRole = QPalette::NoRole) const
+ int flags, const QPalette &pal, bool enabled,
+ const QString &text, QPalette::ColorRole textRole = QPalette::NoRole) const override
{
- DrawTextStyle *that = (DrawTextStyle *)this;
- that->alignment = flags;
+ alignment = flags;
QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, text, textRole);
}
- int alignment;
+
+ mutable int alignment = 0;
};
@@ -533,46 +503,48 @@ void tst_QStyle::testDrawingShortcuts()
setFrameless(&w);
QToolButton *tb = new QToolButton(&w);
tb->setText("&abc");
- DrawTextStyle *dts = new DrawTextStyle;
+ QScopedPointer<DrawTextStyle> dts(new DrawTextStyle);
w.show();
- tb->setStyle(dts);
+ tb->setStyle(dts.data());
tb->grab();
QStyleOptionToolButton sotb;
sotb.initFrom(tb);
bool showMnemonic = dts->styleHint(QStyle::SH_UnderlineShortcut, &sotb, tb);
QVERIFY(dts->alignment & (showMnemonic ? Qt::TextShowMnemonic : Qt::TextHideMnemonic));
- delete dts;
}
{
QToolBar w;
setFrameless(&w);
QToolButton *tb = new QToolButton(&w);
tb->setText("&abc");
- DrawTextStyle *dts = new DrawTextStyle;
+ QScopedPointer<DrawTextStyle> dts(new DrawTextStyle);
w.addWidget(tb);
w.show();
- tb->setStyle(dts);
+ tb->setStyle(dts.data());
tb->grab();
QStyleOptionToolButton sotb;
sotb.initFrom(tb);
bool showMnemonic = dts->styleHint(QStyle::SH_UnderlineShortcut, &sotb, tb);
QVERIFY(dts->alignment & (showMnemonic ? Qt::TextShowMnemonic : Qt::TextHideMnemonic));
- delete dts;
}
}
-#define SCROLLBAR_SPACING 33
+static const int SCROLLBAR_SPACING = 33;
class FrameTestStyle : public QProxyStyle {
public:
FrameTestStyle() : QProxyStyle("Windows") { }
- int styleHint(StyleHint hint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const {
+
+ int styleHint(StyleHint hint, const QStyleOption *opt, const QWidget *widget,
+ QStyleHintReturn *returnData) const override
+ {
if (hint == QStyle::SH_ScrollView_FrameOnlyAroundContents)
return 1;
return QProxyStyle ::styleHint(hint, opt, widget, returnData);
}
- int pixelMetric(PixelMetric pm, const QStyleOption *option, const QWidget *widget) const {
+ int pixelMetric(PixelMetric pm, const QStyleOption *option, const QWidget *widget) const override
+ {
if (pm == QStyle::PM_ScrollView_ScrollBarSpacing)
return SCROLLBAR_SPACING;
return QProxyStyle ::pixelMetric(pm, option ,widget);
@@ -583,12 +555,12 @@ void tst_QStyle::testFrameOnlyAroundContents()
{
QScrollArea area;
area.setGeometry(0, 0, 200, 200);
- QStyle *winStyle = QStyleFactory::create("Windows");
+ QScopedPointer<QStyle> winStyle(QStyleFactory::create("Windows"));
FrameTestStyle frameStyle;
QWidget *widget = new QWidget(&area);
widget->setGeometry(0, 0, 400, 400);
- area.setStyle(winStyle);
- area.verticalScrollBar()->setStyle(winStyle);
+ area.setStyle(winStyle.data());
+ area.verticalScrollBar()->setStyle(winStyle.data());
area.setWidget(widget);
area.setVisible(true);
int viewPortWidth = area.viewport()->width();
@@ -598,8 +570,7 @@ void tst_QStyle::testFrameOnlyAroundContents()
#ifdef Q_OS_WINRT
QEXPECT_FAIL("", "QWidget::setGeometry does not work on WinRT", Continue);
#endif
- QVERIFY(viewPortWidth == area.viewport()->width() + SCROLLBAR_SPACING);
- delete winStyle;
+ QCOMPARE(viewPortWidth, area.viewport()->width() + SCROLLBAR_SPACING);
}
@@ -607,16 +578,16 @@ class ProxyTest: public QProxyStyle
{
Q_OBJECT
public:
- ProxyTest(QStyle *style = 0)
- :QProxyStyle(style)
- , called(false)
- {}
+ using QProxyStyle::QProxyStyle;
- void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override {
+ void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
+ const QWidget *w) const override
+ {
called = true;
return QProxyStyle::drawPrimitive(pe, opt, p, w);
}
- mutable bool called;
+
+ mutable bool called = false;
};
@@ -630,17 +601,16 @@ void tst_QStyle::testProxyCalled()
QPixmap surface(QSize(200, 200));
QPainter painter(&surface);
- QStringList keys = QStyleFactory::keys();
+ const QStringList keys = QStyleFactory::keys();
QVector<QStyle*> styles;
styles.reserve(keys.size() + 1);
styles << new QCommonStyle();
- Q_FOREACH (const QString &key, keys) {
+ for (const QString &key : keys)
styles << QStyleFactory::create(key);
- }
- Q_FOREACH (QStyle *style, styles) {
+ for (QStyle *style : styles) {
ProxyTest testStyle;
testStyle.setBaseStyle(style);
style->drawControl(QStyle::CE_ToolButtonLabel, &opt, &painter, &b);
@@ -654,11 +624,9 @@ class TestStyleOptionInitProxy: public QProxyStyle
{
Q_OBJECT
public:
- mutable bool invalidOptionsDetected;
- explicit TestStyleOptionInitProxy(QStyle *style = nullptr)
- : QProxyStyle(style),
- invalidOptionsDetected(false)
- {}
+ mutable bool invalidOptionsDetected = false;
+
+ using QProxyStyle::QProxyStyle;
void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override {
checkStyleEnum<QStyle::PrimitiveElement>(pe, opt);
@@ -743,7 +711,7 @@ void tst_QStyle::testStyleOptionInit()
QStringList keys = QStyleFactory::keys();
keys.prepend(QString()); // QCommonStyle marker
- Q_FOREACH (const QString &key, keys) {
+ for (const QString &key : qAsConst(keys)) {
QStyle* style = key.isEmpty() ? new QCommonStyle : QStyleFactory::create(key);
TestStyleOptionInitProxy testStyle;
testStyle.setBaseStyle(style);
diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp
index a671a6c4d8..400e46cb97 100644
--- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp
+++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@@ -25,12 +25,40 @@
** $QT_END_LICENSE$
**
****************************************************************************/
-#include <QtCore>
-#include <QtGui>
-#include <QtWidgets>
+
+#include <QtWidgets/QApplication>
+#include <QtWidgets/QCheckBox>
+#include <QtWidgets/QComboBox>
+#include <QtWidgets/QDateEdit>
+#include <QtWidgets/QDialog>
+#include <QtWidgets/QDialogButtonBox>
+#include <QtWidgets/QGridLayout>
+#include <QtWidgets/QGroupBox>
+#include <QtWidgets/QLabel>
+#include <QtWidgets/QHeaderView>
+#include <QtWidgets/QLineEdit>
+#include <QtWidgets/QMainWindow>
+#include <QtWidgets/QMenu>
+#include <QtWidgets/QPushButton>
+#include <QtWidgets/QProgressBar>
+#include <QtWidgets/QSpinBox>
+#include <QtWidgets/QSplitter>
+#include <QtWidgets/QStyle>
+#include <QtWidgets/QStyleFactory>
+#include <QtWidgets/QTableWidget>
+#include <QtWidgets/QToolButton>
+#include <QtWidgets/QToolTip>
+#include <QtWidgets/QTreeView>
+#include <QtWidgets/QVBoxLayout>
+
+#include <QtGui/QPainter>
+#include <QtGui/QScreen>
+
#include <QtTest/QtTest>
-#include <QtDebug>
-#include <QMetaObject>
+
+#include <QtCore/QDebug>
+#include <QtCore/QMetaObject>
+#include <QtCore/QScopedPointer>
#include <private/qstylesheetstyle_p.h>
#include <private/qhighdpiscaling_p.h>
@@ -43,10 +71,12 @@ class tst_QStyleSheetStyle : public QObject
Q_OBJECT
public:
tst_QStyleSheetStyle();
- ~tst_QStyleSheetStyle();
+
+ static void initMain();
private slots:
void init();
+ void cleanup();
void repolish();
void repolish_without_crashing();
void numinstances();
@@ -77,7 +107,7 @@ private slots:
void hoverColors();
#endif
void background();
- void tabAlignement();
+ void tabAlignment();
void attributesList();
void minmaxSizes();
void task206238_twice();
@@ -107,37 +137,53 @@ private slots:
void highdpiImages();
private:
- QColor COLOR(const QWidget& w) {
+ static QColor COLOR(const QWidget &w)
+ {
w.ensurePolished();
return w.palette().color(w.foregroundRole());
}
- QColor APPCOLOR(const QWidget& w) {
+
+ static QColor APPCOLOR(const QWidget &w)
+ {
w.ensurePolished();
- return qApp->palette(&w).color(w.foregroundRole());
+ return QApplication::palette(&w).color(w.foregroundRole());
}
- QColor BACKGROUND(const QWidget& w) {
+
+ static QColor BACKGROUND(const QWidget &w)
+ {
w.ensurePolished();
return w.palette().color(w.backgroundRole());
}
- QColor APPBACKGROUND(const QWidget& w) {
+
+ static QColor APPBACKGROUND(const QWidget &w)
+ {
w.ensurePolished();
- return qApp->palette(&w).color(w.backgroundRole());
+ return QApplication::palette(&w).color(w.backgroundRole());
}
- int FONTSIZE(const QWidget &w) {
+
+ static int FONTSIZE(const QWidget &w)
+ {
w.ensurePolished();
return w.font().pointSize();
}
- int APPFONTSIZE(const QWidget &w) {
- return qApp->font(&w).pointSize();
- }
+
+ static int APPFONTSIZE(const QWidget &w) { return QApplication::font(&w).pointSize(); }
+
+ const QRect m_availableGeometry = QGuiApplication::primaryScreen()->availableGeometry();
+ QSize m_testSize;
};
-tst_QStyleSheetStyle::tst_QStyleSheetStyle()
+// highdpiImages() tests HighDPI scaling; disable initially.
+void tst_QStyleSheetStyle::initMain()
{
+ QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
}
-tst_QStyleSheetStyle::~tst_QStyleSheetStyle()
+tst_QStyleSheetStyle::tst_QStyleSheetStyle()
{
+ const int testSize = qMax(200, m_availableGeometry.width() / 10);
+ m_testSize.setWidth(testSize);
+ m_testSize.setHeight(testSize);
}
void tst_QStyleSheetStyle::init()
@@ -146,10 +192,16 @@ void tst_QStyleSheetStyle::init()
QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, false);
}
+void tst_QStyleSheetStyle::cleanup()
+{
+ QTRY_VERIFY(QApplication::topLevelWidgets().isEmpty());
+}
+
void tst_QStyleSheetStyle::numinstances()
{
QWidget w;
- w.resize(200, 200);
+ w.setWindowTitle(QTest::currentTestFunction());
+ w.resize(m_testSize);
centerOnScreen(&w);
QCommonStyle *style = new QCommonStyle;
style->setParent(&w);
@@ -175,7 +227,7 @@ void tst_QStyleSheetStyle::numinstances()
QCOMPARE(QStyleSheetStyle::numinstances, 0);
// set and unset widget stylesheet
- w.setStyle(0);
+ w.setStyle(nullptr);
w.setStyleSheet("color: red");
QCOMPARE(QStyleSheetStyle::numinstances, 1);
c.setStyle(style);
@@ -325,7 +377,7 @@ void tst_QStyleSheetStyle::reparentWithNoChildStyleSheet()
QCOMPARE(COLOR(c1), red);
qApp->setStyleSheet("* { color: blue }");
- c1.setParent(0);
+ c1.setParent(nullptr);
QCOMPARE(COLOR(c1), blue);
delete pb;
}
@@ -372,6 +424,8 @@ void tst_QStyleSheetStyle::repolish_without_crashing()
{
// This used to crash, QTBUG-69204
QMainWindow w;
+ w.resize(m_testSize);
+ w.setWindowTitle(QTest::currentTestFunction());
QScopedPointer<QSplitter> splitter1(new QSplitter(w.centralWidget()));
QScopedPointer<QSplitter> splitter2(new QSplitter);
QScopedPointer<QSplitter> splitter3(new QSplitter);
@@ -408,7 +462,7 @@ void tst_QStyleSheetStyle::widgetStyle()
QPointer<QStyle> style1 = QStyleFactory::create("Windows");
QPointer<QStyle> style2 = QStyleFactory::create("Windows");
- QStyle *appStyle = qApp->style();
+ QStyle *appStyle = QApplication::style();
// Sanity: By default, a window inherits the application style
QCOMPARE(appStyle, window1->style());
@@ -423,7 +477,7 @@ void tst_QStyleSheetStyle::widgetStyle()
QVERIFY(!style1.isNull()); // case we have not already crashed
// Setting null style must make it follow the qApp style
- window1->setStyle(0);
+ window1->setStyle(nullptr);
QCOMPARE(window1->style(), appStyle);
QVERIFY(!style2.isNull()); // case we have not already crashed
QVERIFY(!style2.isNull()); // case we have not already crashed
@@ -431,16 +485,15 @@ void tst_QStyleSheetStyle::widgetStyle()
// Sanity: Set the stylesheet
window1->setStyleSheet(":x { }");
- QPointer<QStyleSheetStyle> proxy = (QStyleSheetStyle *)window1->style();
+ QPointer<QStyleSheetStyle> proxy = qobject_cast<QStyleSheetStyle *>(window1->style());
QVERIFY(!proxy.isNull());
- QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle"); // must be our proxy
- QVERIFY(proxy->base == 0); // and follows the application
+ QCOMPARE(proxy->base, nullptr); // and follows the application
// Set the stylesheet
window1->setStyle(style1);
QVERIFY(proxy.isNull()); // we create a new one each time
- proxy = (QStyleSheetStyle *)window1->style();
- QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle"); // it is a proxy
+ proxy = qobject_cast<QStyleSheetStyle *>(window1->style());
+ QVERIFY(!proxy.isNull()); // it is a proxy
QCOMPARE(proxy->baseStyle(), style1.data()); // must have been replaced with the new one
// Update the stylesheet and check nothing changes
@@ -449,15 +502,15 @@ void tst_QStyleSheetStyle::widgetStyle()
QCOMPARE(proxy->baseStyle(), style1.data()); // the same guy
// Remove the stylesheet
- proxy = (QStyleSheetStyle *)window1->style();
+ proxy = qobject_cast<QStyleSheetStyle *>(window1->style());
window1->setStyleSheet(QString());
QVERIFY(proxy.isNull()); // should have disappeared
QCOMPARE(window1->style(), style1.data()); // its restored
// Style Sheet existing children propagation
window1->setStyleSheet(":z { }");
- proxy = (QStyleSheetStyle *)window1->style();
- QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle");
+ proxy = qobject_cast<QStyleSheetStyle *>(window1->style());
+ QVERIFY(!proxy.isNull()); // it is a proxy
QCOMPARE(window1->style(), widget1->style()); // proxy must have propagated
QCOMPARE(widget2->style(), appStyle); // widget2 is following the app style
@@ -473,55 +526,57 @@ void tst_QStyleSheetStyle::widgetStyle()
// Style Sheet propagation on a child widget with a custom style
widget2->setStyle(style1);
window2->setStyleSheet(":x { }");
- proxy = (QStyleSheetStyle *)widget2->style();
- QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle");
+ proxy = qobject_cast<QStyleSheetStyle *>(widget2->style());
+ QVERIFY(!proxy.isNull()); // it is a proxy
QCOMPARE(proxy->baseStyle(), style1.data());
// Style Sheet propagation on a child widget with a custom style already set
window2->setStyleSheet(QString());
QCOMPARE(window2->style(), style2.data());
QCOMPARE(widget2->style(), style1.data());
- widget2->setStyle(0);
+ widget2->setStyle(nullptr);
window2->setStyleSheet(":x { }");
widget2->setStyle(style1);
- proxy = (QStyleSheetStyle *)widget2->style();
- QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle");
+ proxy = qobject_cast<QStyleSheetStyle *>(widget2->style());
+ QVERIFY(!proxy.isNull()); // it is a proxy
// QApplication, QWidget both having a style sheet
// clean everything out
- window1->setStyle(0);
+ window1->setStyle(nullptr);
window1->setStyleSheet(QString());
- window2->setStyle(0);
+ window2->setStyle(nullptr);
window2->setStyleSheet(QString());
- qApp->setStyle(0);
+ QApplication::setStyle(nullptr);
qApp->setStyleSheet("may_insanity_prevail { }"); // app has stylesheet
- QCOMPARE(window1->style(), qApp->style());
+ QCOMPARE(window1->style(), QApplication::style());
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle");
QCOMPARE(widget1->style()->metaObject()->className(), "QStyleSheetStyle"); // check the child
window1->setStyleSheet("may_more_insanity_prevail { }"); // window has stylesheet
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle"); // a new one
QCOMPARE(widget1->style(), window1->style()); // child follows...
- proxy = (QStyleSheetStyle *) window1->style();
+ proxy = qobject_cast<QStyleSheetStyle *>(window1->style());
+ QVERIFY(!proxy.isNull());
QStyle *newStyle = QStyleFactory::create("Windows");
- qApp->setStyle(newStyle); // set a custom style on app
- proxy = (QStyleSheetStyle *) window1->style();
+ QApplication::setStyle(newStyle); // set a custom style on app
+ proxy = qobject_cast<QStyleSheetStyle *>(window1->style());
+ QVERIFY(!proxy.isNull()); // it is a proxy
QCOMPARE(proxy->baseStyle(), newStyle); // magic ;) the widget still follows the application
QCOMPARE(static_cast<QStyle *>(proxy), widget1->style()); // child still follows...
window1->setStyleSheet(QString()); // remove stylesheet
- QCOMPARE(window1->style(), qApp->style()); // is this cool or what
- QCOMPARE(widget1->style(), qApp->style()); // annoying child follows...
+ QCOMPARE(window1->style(), QApplication::style()); // is this cool or what
+ QCOMPARE(widget1->style(), QApplication::style()); // annoying child follows...
QScopedPointer<QStyle> wndStyle(QStyleFactory::create("Windows"));
window1->setStyle(wndStyle.data());
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle"); // auto wraps it
QCOMPARE(widget1->style(), window1->style()); // and auto propagates to child
qApp->setStyleSheet(QString()); // remove the app stylesheet
QCOMPARE(window1->style(), wndStyle.data()); // auto dewrap
- QCOMPARE(widget1->style(), qApp->style()); // and child state is restored
- window1->setStyle(0); // let sanity prevail
- qApp->setStyle(0);
+ QCOMPARE(widget1->style(), QApplication::style()); // and child state is restored
+ window1->setStyle(nullptr); // let sanity prevail
+ QApplication::setStyle(nullptr);
delete window1;
delete widget2;
@@ -534,32 +589,32 @@ void tst_QStyleSheetStyle::appStyle()
{
qApp->setStyleSheet(QString());
// qApp style can never be 0
- QVERIFY(QApplication::style() != 0);
+ QVERIFY(QApplication::style() != nullptr);
QPointer<QStyle> style1 = QStyleFactory::create("Windows");
QPointer<QStyle> style2 = QStyleFactory::create("Windows");
- qApp->setStyle(style1);
+ QApplication::setStyle(style1);
// Basic sanity
QCOMPARE(QApplication::style(), style1.data());
- qApp->setStyle(style2);
+ QApplication::setStyle(style2);
QVERIFY(style1.isNull()); // qApp must have taken ownership and deleted it
// Setting null should not crash
- qApp->setStyle(0);
+ QApplication::setStyle(nullptr);
QCOMPARE(QApplication::style(), style2.data());
// Set the stylesheet
qApp->setStyleSheet("whatever");
- QPointer<QStyleSheetStyle> sss = (QStyleSheetStyle *)qApp->style();
+ QPointer<QStyleSheetStyle> sss = static_cast<QStyleSheetStyle *>(QApplication::style());
QVERIFY(!sss.isNull());
QCOMPARE(sss->metaObject()->className(), "QStyleSheetStyle"); // must be our proxy now
QVERIFY(!style2.isNull()); // this should exist as it is the base of the proxy
QCOMPARE(sss->baseStyle(), style2.data());
style1 = QStyleFactory::create("Windows");
- qApp->setStyle(style1);
+ QApplication::setStyle(style1);
QVERIFY(style2.isNull()); // should disappear automatically
QVERIFY(sss.isNull()); // should disappear automatically
// Update the stylesheet and check nothing changes
- sss = (QStyleSheetStyle *)qApp->style();
+ sss = static_cast<QStyleSheetStyle *>(QApplication::style());
qApp->setStyleSheet("whatever2");
QCOMPARE(QApplication::style(), sss.data());
QCOMPARE(sss->baseStyle(), style1.data());
@@ -577,14 +632,15 @@ void tst_QStyleSheetStyle::dynamicProperty()
{
qApp->setStyleSheet(QString());
- QString appStyle = qApp->style()->metaObject()->className();
+ QString appStyle = QApplication::style()->metaObject()->className();
QPushButton pb1(QStringLiteral("dynamicProperty_pb1"));
- pb1.setMinimumWidth(160);
- pb1.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(20, 100));
+ pb1.setMinimumWidth(m_testSize.width());
+ pb1.move(m_availableGeometry.topLeft() + QPoint(20, 100));
QPushButton pb2(QStringLiteral("dynamicProperty_pb2"));
- pb2.setMinimumWidth(160);
- pb2.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(20, 200));
+ pb2.setWindowTitle(QTest::currentTestFunction());
+ pb2.setMinimumWidth(m_testSize.width());
+ pb2.move(m_availableGeometry.topLeft() + QPoint(20, m_testSize.width() + 40));
pb1.setProperty("type", "critical");
qApp->setStyleSheet("*[class~=\"QPushButton\"] { color: red; } *[type=\"critical\"] { background: white; }");
@@ -625,7 +681,7 @@ namespace ns {
class PushButton1 : public QPushButton {
Q_OBJECT
public:
- PushButton1() { }
+ using QPushButton::QPushButton;
};
class PushButton2 : public PushButton1 {
Q_OBJECT
@@ -781,7 +837,7 @@ void tst_QStyleSheetStyle::onWidgetDestroyed()
qApp->setStyleSheet(QString());
QLabel *l = new QLabel;
l->setStyleSheet("QLabel { color: red }");
- QPointer<QStyleSheetStyle> ss = (QStyleSheetStyle *) l->style();
+ QPointer<QStyleSheetStyle> ss = static_cast<QStyleSheetStyle *>(l->style());
delete l;
QVERIFY(ss.isNull());
}
@@ -789,7 +845,8 @@ void tst_QStyleSheetStyle::onWidgetDestroyed()
void tst_QStyleSheetStyle::fontPrecedence()
{
QLineEdit edit;
- edit.setMinimumWidth(200);
+ edit.setWindowTitle(QTest::currentTestFunction());
+ edit.setMinimumWidth(m_testSize.width());
centerOnScreen(&edit);
edit.show();
QFont font;
@@ -817,23 +874,23 @@ void tst_QStyleSheetStyle::fontPrecedence()
}
// Ensure primary will only return true if the color covers more than 50% of pixels
-static bool testForColors(const QImage& image, const QColor& color, bool ensurePrimary=false)
+static bool testForColors(const QImage& image, const QColor &color, bool ensurePrimary = false)
{
int count = 0;
QRgb rgb = color.rgba();
- int totalCount = image.height()*image.width();
+ int totalCount = image.height() * image.width();
for (int y = 0; y < image.height(); ++y) {
for (int x = 0; x < image.width(); ++x) {
// Because of antialiasing we allow a certain range of errors here.
QRgb pixel = image.pixel(x, y);
- if (qAbs((int)(pixel & 0xff) - (int)(rgb & 0xff)) +
- qAbs((int)((pixel & 0xff00) >> 8) - (int)((rgb & 0xff00) >> 8)) +
- qAbs((int)((pixel & 0xff0000) >> 16) - (int)((rgb & 0xff0000) >> 16)) <= 50) {
+ if (qAbs(int(pixel & 0xff) - int(rgb & 0xff)) +
+ qAbs(int((pixel & 0xff00) >> 8) - int((rgb & 0xff00) >> 8)) +
+ qAbs(int((pixel & 0xff0000) >> 16) - int((rgb & 0xff0000) >> 16)) <= 50) {
count++;
if (!ensurePrimary && count >=10 )
return true;
- else if (count > totalCount/2)
+ if (count > totalCount / 2)
return true;
}
}
@@ -842,7 +899,8 @@ static bool testForColors(const QImage& image, const QColor& color, bool ensureP
return false;
}
-class TestDialog : public QDialog {
+class TestDialog : public QDialog
+{
public:
explicit TestDialog(const QString &styleSheet);
@@ -878,8 +936,8 @@ TestDialog::TestDialog(const QString &styleSheet) :
addWidget(spinbox);
QComboBox *combobox = new QComboBox;
combobox->setEditable(true);
- combobox->addItems(QStringList() << "TESTING TESTING");
- addWidget(spinbox);
+ combobox->addItems(QStringList{"TESTING TESTING"});
+ addWidget(combobox);
addWidget(new QLabel("<b>TESTING TESTING</b>"));
}
@@ -921,12 +979,12 @@ void tst_QStyleSheetStyle::focusColors()
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
(QString::fromLatin1(widget->metaObject()->className())
+ " did not contain background color #e8ff66, using style "
- + QString::fromLatin1(qApp->style()->metaObject()->className()))
+ + QString::fromLatin1(QApplication::style()->metaObject()->className()))
.toLocal8Bit().constData());
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
(QString::fromLatin1(widget->metaObject()->className())
+ " did not contain text color #ff0084, using style "
- + QString::fromLatin1(qApp->style()->metaObject()->className()))
+ + QString::fromLatin1(QApplication::style()->metaObject()->className()))
.toLocal8Bit().constData());
}
}
@@ -1007,20 +1065,14 @@ class SingleInheritanceDialog : public QDialog
{
Q_OBJECT
public:
- SingleInheritanceDialog(QWidget *w = 0) :
- QDialog(w)
- {
- }
+ using QDialog::QDialog;
};
class DoubleInheritanceDialog : public SingleInheritanceDialog
{
Q_OBJECT
public:
- DoubleInheritanceDialog(QWidget *w = 0) :
- SingleInheritanceDialog(w)
- {
- }
+ using SingleInheritanceDialog::SingleInheritanceDialog;
};
void tst_QStyleSheetStyle::background()
@@ -1029,24 +1081,25 @@ void tst_QStyleSheetStyle::background()
const QString styleSheet = QStringLiteral("* { background-color: #e8ff66; }");
QVector<WidgetPtr> widgets;
- const QPoint topLeft = QGuiApplication::primaryScreen()->availableGeometry().topLeft();
+ const QPoint topLeft = m_availableGeometry.topLeft();
// Testing inheritance styling of QDialog.
WidgetPtr toplevel(new SingleInheritanceDialog);
- toplevel->resize(200, 200);
+ toplevel->resize(m_testSize);
toplevel->move(topLeft + QPoint(20, 20));
toplevel->setStyleSheet(styleSheet);
widgets.append(toplevel);
toplevel = WidgetPtr(new DoubleInheritanceDialog);
- toplevel->resize(200, 200);
- toplevel->move(topLeft + QPoint(20, 320));
+ toplevel->resize(m_testSize);
+ toplevel->move(topLeft + QPoint(20, m_testSize.height() + 120));
toplevel->setStyleSheet(styleSheet);
widgets.append(toplevel);
// Testing gradients in QComboBox.
// First color
toplevel = WidgetPtr(new QDialog);
- toplevel->move(topLeft + QPoint(320, 20));
+ toplevel->resize(m_testSize);
+ toplevel->move(topLeft + QPoint(m_testSize.width() + 120, 20));
QGridLayout *layout = new QGridLayout(toplevel.data());
QComboBox* cb = new QComboBox;
cb->setMinimumWidth(160);
@@ -1055,7 +1108,8 @@ void tst_QStyleSheetStyle::background()
widgets.append(toplevel);
// Second color
toplevel = WidgetPtr(new QDialog);
- toplevel->move(topLeft + QPoint(320, 320));
+ toplevel->resize(m_testSize);
+ toplevel->move(topLeft + QPoint(m_testSize.width() + 120, m_testSize.height() + 120));
layout = new QGridLayout(toplevel.data());
cb = new QComboBox;
cb->setMinimumWidth(160);
@@ -1086,9 +1140,10 @@ void tst_QStyleSheetStyle::background()
}
}
-void tst_QStyleSheetStyle::tabAlignement()
+void tst_QStyleSheetStyle::tabAlignment()
{
QWidget topLevel;
+ topLevel.setWindowTitle(QTest::currentTestFunction());
QTabWidget tabWidget(&topLevel);
tabWidget.addTab(new QLabel("tab1"),"tab1");
tabWidget.resize(QSize(400,400));
@@ -1148,6 +1203,8 @@ void tst_QStyleSheetStyle::attributesList()
void tst_QStyleSheetStyle::minmaxSizes()
{
QTabWidget tabWidget;
+ tabWidget.resize(m_testSize);
+ tabWidget.setWindowTitle(QTest::currentTestFunction());
tabWidget.setObjectName("tabWidget");
int index1 = tabWidget.addTab(new QLabel("Tab1"),"a");
@@ -1187,6 +1244,8 @@ void tst_QStyleSheetStyle::task206238_twice()
{
const QColor red(Qt::red);
QMainWindow w;
+ w.resize(m_testSize);
+ w.setWindowTitle(QTest::currentTestFunction());
QTabWidget* tw = new QTabWidget;
tw->addTab(new QLabel("foo"), "test");
w.setCentralWidget(tw);
@@ -1220,6 +1279,8 @@ void tst_QStyleSheetStyle::transparent()
class ProxyStyle : public QStyle
{
+ Q_OBJECT
+
public:
ProxyStyle(QStyle *s)
{
@@ -1227,19 +1288,19 @@ class ProxyStyle : public QStyle
}
void drawControl(ControlElement ce, const QStyleOption *opt,
- QPainter *painter, const QWidget *widget = 0) const;
+ QPainter *painter, const QWidget *widget = nullptr) const override;
void drawPrimitive(QStyle::PrimitiveElement pe,
const QStyleOption* opt,
- QPainter* p ,
- const QWidget* w) const
+ QPainter *p,
+ const QWidget *w) const override
{
style->drawPrimitive(pe, opt, p, w);
}
QRect subElementRect(QStyle::SubElement se,
- const QStyleOption* opt,
- const QWidget* w) const
+ const QStyleOption *opt,
+ const QWidget *w) const override
{
Q_UNUSED(se);
Q_UNUSED(opt);
@@ -1248,64 +1309,64 @@ class ProxyStyle : public QStyle
}
void drawComplexControl(QStyle::ComplexControl cc,
- const QStyleOptionComplex* opt,
- QPainter* p,
- const QWidget* w) const
+ const QStyleOptionComplex *opt,
+ QPainter *p,
+ const QWidget *w) const override
{
style->drawComplexControl(cc, opt, p, w);
}
SubControl hitTestComplexControl(QStyle::ComplexControl cc,
- const QStyleOptionComplex* opt,
- const QPoint& pt,
- const QWidget* w) const
+ const QStyleOptionComplex *opt,
+ const QPoint &pt,
+ const QWidget *w) const override
{
return style->hitTestComplexControl(cc, opt, pt, w);
}
QRect subControlRect(QStyle::ComplexControl cc,
- const QStyleOptionComplex* opt,
+ const QStyleOptionComplex *opt,
QStyle::SubControl sc,
- const QWidget* w) const
+ const QWidget *w) const override
{
return style->subControlRect(cc, opt, sc, w);
}
int pixelMetric(QStyle::PixelMetric pm,
- const QStyleOption* opt,
- const QWidget* w) const
+ const QStyleOption *opt,
+ const QWidget *w) const override
{
return style->pixelMetric(pm, opt, w);
}
QSize sizeFromContents(QStyle::ContentsType ct,
- const QStyleOption* opt,
- const QSize& size,
- const QWidget* w) const
+ const QStyleOption *opt,
+ const QSize &size,
+ const QWidget *w) const override
{
return style->sizeFromContents(ct, opt, size, w);
}
int styleHint(QStyle::StyleHint sh,
- const QStyleOption* opt,
- const QWidget* w,
- QStyleHintReturn* shr) const
+ const QStyleOption *opt,
+ const QWidget *w,
+ QStyleHintReturn *shr) const override
{
return style->styleHint(sh, opt, w, shr);
}
QPixmap standardPixmap(QStyle::StandardPixmap spix,
- const QStyleOption* opt,
- const QWidget* w) const
+ const QStyleOption *opt,
+ const QWidget *w) const override
{
return style->standardPixmap(spix, opt, w);
}
QPixmap generatedIconPixmap(QIcon::Mode mode,
- const QPixmap& pix,
- const QStyleOption* opt) const
+ const QPixmap &pix,
+ const QStyleOption *opt) const override
{
return style->generatedIconPixmap(mode, pix, opt);
}
@@ -1314,14 +1375,14 @@ class ProxyStyle : public QStyle
QSizePolicy::ControlType c2,
Qt::Orientation ori,
const QStyleOption *opt,
- const QWidget *w) const
+ const QWidget *w) const override
{
return style->layoutSpacing(c1, c2, ori, opt, w);
}
QIcon standardIcon(StandardPixmap si,
const QStyleOption *opt,
- const QWidget *w) const
+ const QWidget *w) const override
{
return style->standardIcon(si, opt, w);
}
@@ -1357,15 +1418,15 @@ void tst_QStyleSheetStyle::proxyStyle()
{
//Should not crash; task 158984
- ProxyStyle *proxy = new ProxyStyle(qApp->style());
+ ProxyStyle *proxy = new ProxyStyle(QApplication::style());
QString styleSheet("QPushButton {background-color: red; }");
QWidget *w = new QWidget;
- w->setMinimumWidth(160);
+ w->setMinimumWidth(m_testSize.width());
centerOnScreen(w);
QVBoxLayout *layout = new QVBoxLayout(w);
- QPushButton *pb1 = new QPushButton(qApp->style()->objectName(), w);
+ QPushButton *pb1 = new QPushButton(QApplication::style()->objectName(), w);
layout->addWidget(pb1);
QPushButton *pb2 = new QPushButton("ProxyStyle", w);
@@ -1383,7 +1444,7 @@ void tst_QStyleSheetStyle::proxyStyle()
// In this case it would be the QStyleSheetStyle that is deleted
// later on. We need to get access to the "real" QStyle to be able to
// draw correctly.
- ProxyStyle* newProxy = new ProxyStyle(qApp->style());
+ ProxyStyle *newProxy = new ProxyStyle(QApplication::style());
pb4->setStyle(newProxy);
layout->addWidget(pb4);
@@ -1421,6 +1482,7 @@ void tst_QStyleSheetStyle::emptyStyleSheet()
//empty stylesheet should not change anything
qApp->setStyleSheet(QString());
QWidget w;
+ w.setWindowTitle(QTest::currentTestFunction());
QHBoxLayout layout(&w);
w.setLayout(&layout);
layout.addWidget(new QPushButton("push", &w));
@@ -1479,6 +1541,8 @@ void tst_QStyleSheetStyle::toolTip()
{
qApp->setStyleSheet(QString());
QWidget w;
+ w.resize(m_testSize);
+ w.setWindowTitle(QTest::currentTestFunction());
// Use "Fusion" to prevent the Vista style from clobbering the tooltip palette in polish().
QStyle *fusionStyle = QStyleFactory::create(QLatin1String("Fusion"));
QVERIFY(fusionStyle);
@@ -1512,28 +1576,27 @@ void tst_QStyleSheetStyle::toolTip()
centerOnScreen(&w);
w.show();
- qApp->setActiveWindow(&w);
+ QApplication::setActiveWindow(&w);
QVERIFY(QTest::qWaitForWindowActive(&w));
const QColor normalToolTip = QToolTip::palette().color(QPalette::Inactive, QPalette::ToolTipBase);
- QList<QWidget *> widgets;
- QList<QColor> colors;
-
-
- //tooltip on the widget without stylesheet, then to othes widget, including one without stylesheet
- //(the tooltip will be reused but his colour must change)
- widgets << wid4 << wid1 << wid2 << wid3 << wid4;
- colors << normalToolTip << "#ae2" << "#f81" << "#0b8" << normalToolTip;
-
- for (int i = 0; i < widgets.count() ; i++)
- {
+ // Tooltip on the widget without stylesheet, then to other widget,
+ // including one without stylesheet (the tooltip will be reused,
+ // but its color must change)
+ const QWidgetList widgets{wid4, wid1, wid2, wid3, wid4};
+ const QVector<QColor> colors{normalToolTip, QColor("#ae2"), QColor("#f81"),
+ QColor("#0b8"), normalToolTip};
+
+ QWidgetList topLevels;
+ for (int i = 0; i < widgets.count() ; ++i) {
QWidget *wid = widgets.at(i);
QColor col = colors.at(i);
QToolTip::showText( QPoint(0,0) , "This is " + wid->objectName(), wid);
- QWidget *tooltip = 0;
- foreach (QWidget *widget, QApplication::topLevelWidgets()) {
+ topLevels = QApplication::topLevelWidgets();
+ QWidget *tooltip = nullptr;
+ for (QWidget *widget : qAsConst(topLevels)) {
if (widget->inherits("QTipLabel")) {
tooltip = widget;
break;
@@ -1548,17 +1611,18 @@ void tst_QStyleSheetStyle::toolTip()
QTest::qWait(100);
delete wid3; //should not crash;
QTest::qWait(10);
- foreach (QWidget *widget, QApplication::topLevelWidgets()) {
+ topLevels = QApplication::topLevelWidgets();
+ for (QWidget *widget : qAsConst(topLevels))
widget->update(); //should not crash either
- }
}
void tst_QStyleSheetStyle::embeddedFonts()
{
//task 235622 and 210551
QSpinBox spin;
- spin.setMinimumWidth(160);
- spin.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(20, 20));
+ spin.setWindowTitle(QTest::currentTestFunction());
+ spin.setMinimumWidth(m_testSize.width());
+ spin.move(m_availableGeometry.topLeft() + QPoint(20, 20));
spin.show();
spin.setStyleSheet("QSpinBox { font-size: 32px; }");
QTest::qWait(20);
@@ -1579,7 +1643,7 @@ void tst_QStyleSheetStyle::embeddedFonts()
//task 242556
QComboBox box;
box.setMinimumWidth(160);
- box.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(20, 120));
+ box.move(m_availableGeometry.topLeft() + QPoint(20, 120));
box.setEditable(true);
box.addItems(QStringList() << "First" << "Second" << "Third");
box.setStyleSheet("QComboBox { font-size: 32px; }");
@@ -1636,19 +1700,17 @@ void tst_QStyleSheetStyle::complexWidgetFocus()
// For this reason, we use unusual and extremely ugly colors! :-)
QDialog frame;
+ frame.setWindowTitle(QTest::currentTestFunction());
frame.setStyleSheet("*:focus { background: black; color: black } "
"QSpinBox::up-arrow:focus, QSpinBox::down-arrow:focus { width: 7px; height: 7px; background: #ff0084 } "
"QComboBox::down-arrow:focus { width: 7px; height: 7px; background: #ff0084 }"
"QSlider::handle:horizontal:focus { width: 7px; height: 7px; background: #ff0084 } ");
- QList<QWidget *> widgets;
- widgets << new QSpinBox;
- widgets << new QComboBox;
- widgets << new QSlider(Qt::Horizontal);
+ const QWidgetList widgets{new QSpinBox, new QComboBox, new QSlider(Qt::Horizontal)};
QLayout* layout = new QGridLayout;
layout->addWidget(new QLineEdit); // Avoids initial focus.
- foreach (QWidget *widget, widgets)
+ for (QWidget *widget : widgets)
layout->addWidget(widget);
frame.setLayout(layout);
@@ -1656,7 +1718,7 @@ void tst_QStyleSheetStyle::complexWidgetFocus()
frame.show();
QApplication::setActiveWindow(&frame);
QVERIFY(QTest::qWaitForWindowActive(&frame));
- foreach (QWidget *widget, widgets) {
+ for (QWidget *widget : widgets) {
widget->setFocus();
QApplication::processEvents();
@@ -1668,7 +1730,7 @@ void tst_QStyleSheetStyle::complexWidgetFocus()
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
(QString::fromLatin1(widget->metaObject()->className())
+ " did not contain text color #ff0084, using style "
- + QString::fromLatin1(qApp->style()->metaObject()->className()))
+ + QString::fromLatin1(QApplication::style()->metaObject()->className()))
.toLocal8Bit().constData());
}
}
@@ -1676,8 +1738,9 @@ void tst_QStyleSheetStyle::complexWidgetFocus()
void tst_QStyleSheetStyle::task188195_baseBackground()
{
QTreeView tree;
+ tree.setWindowTitle(QTest::currentTestFunction());
tree.setStyleSheet( "QTreeView:disabled { background-color:#ab1251; }" );
- tree.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(20, 100));
+ tree.setGeometry(QRect(m_availableGeometry.topLeft() + QPoint(20, 100), m_testSize));
tree.show();
QVERIFY(QTest::qWaitForWindowActive(&tree));
QImage image(tree.width(), tree.height(), QImage::Format_ARGB32);
@@ -1698,7 +1761,8 @@ void tst_QStyleSheetStyle::task188195_baseBackground()
QTableWidget table(12, 12);
table.setItem(0, 0, new QTableWidgetItem());
table.setStyleSheet( "QTableView {background-color: #ff0000}" );
- table.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(300, 100));
+ // This needs to be large so that >50% (excluding header rows/columns) are red.
+ table.setGeometry(QRect(m_availableGeometry.topLeft() + QPoint(300, 100), m_testSize * 2));
table.show();
QVERIFY(QTest::qWaitForWindowActive(&table));
image = QImage(table.width(), table.height(), QImage::Format_ARGB32);
@@ -1720,6 +1784,7 @@ void tst_QStyleSheetStyle::task232085_spinBoxLineEditBg()
spinbox->setValue(8888);
QDialog frame;
+ frame.setWindowTitle(QTest::currentTestFunction());
QLayout* layout = new QGridLayout;
QLineEdit* dummy = new QLineEdit; // Avoids initial focus.
@@ -1746,18 +1811,19 @@ void tst_QStyleSheetStyle::task232085_spinBoxLineEditBg()
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
(QString::fromLatin1(spinbox->metaObject()->className())
+ " did not contain background color #e8ff66, using style "
- + QString::fromLatin1(qApp->style()->metaObject()->className()))
+ + QString::fromLatin1(QApplication::style()->metaObject()->className()))
.toLocal8Bit().constData());
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
(QString::fromLatin1(spinbox->metaObject()->className())
+ " did not contain text color #ff0084, using style "
- + QString::fromLatin1(qApp->style()->metaObject()->className()))
+ + QString::fromLatin1(QApplication::style()->metaObject()->className()))
.toLocal8Bit().constData());
}
class ChangeEventWidget : public QWidget
-{ public:
- void changeEvent(QEvent * event)
+{
+protected:
+ void changeEvent(QEvent *event) override
{
if(event->type() == QEvent::StyleChange) {
static bool recurse = false;
@@ -1789,10 +1855,10 @@ void tst_QStyleSheetStyle::QTBUG11658_cachecrash()
class Widget : public QWidget
{
public:
- Widget(QWidget *parent = 0)
+ Widget(int minimumWidth, QWidget *parent = nullptr)
: QWidget(parent)
{
- setMinimumWidth(160);
+ setMinimumWidth(minimumWidth);
QVBoxLayout* pLayout = new QVBoxLayout(this);
QCheckBox* pCheckBox = new QCheckBox(this);
pLayout->addWidget(pCheckBox);
@@ -1800,13 +1866,14 @@ void tst_QStyleSheetStyle::QTBUG11658_cachecrash()
QString szStyleSheet = QLatin1String("* { color: red; }");
qApp->setStyleSheet(szStyleSheet);
- qApp->setStyle(QStyleFactory::create(QLatin1String("Windows")));
+ QApplication::setStyle(QStyleFactory::create(QLatin1String("Windows")));
}
};
- Widget *w = new Widget();
+ Widget *w = new Widget(m_testSize.width());
delete w;
- w = new Widget();
+ w = new Widget(m_testSize.width());
+ w->setWindowTitle(QTest::currentTestFunction());
centerOnScreen(w);
w->show();
@@ -1818,15 +1885,17 @@ void tst_QStyleSheetStyle::QTBUG11658_cachecrash()
void tst_QStyleSheetStyle::QTBUG15910_crashNullWidget()
{
struct Widget : QWidget {
- virtual void paintEvent(QPaintEvent* ) {
+ void paintEvent(QPaintEvent *) override
+ {
QStyleOption opt;
opt.init(this);
QPainter p(this);
- style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, 0);
- style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, 0);
- style()->drawControl(QStyle::CE_PushButton, &opt, &p, 0);
+ style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, nullptr);
+ style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, nullptr);
+ style()->drawControl(QStyle::CE_PushButton, &opt, &p, nullptr);
}
} w;
+ w.setWindowTitle(QTest::currentTestFunction());
w.setStyleSheet("* { background-color: white; color:black; border 3px solid yellow }");
w.setMinimumWidth(160);
centerOnScreen(&w);
@@ -1840,10 +1909,13 @@ void tst_QStyleSheetStyle::QTBUG36933_brokenPseudoClassLookup()
const int columnCount = 10;
QTableWidget widget(rowCount, columnCount);
+ widget.resize(m_testSize);
+ widget.setWindowTitle(QTest::currentTestFunction());
for (int row = 0; row < rowCount; ++row) {
+ const QString rowNumber = QLatin1String("row ") + QString::number(row + 1);
for (int column = 0; column < columnCount; ++column) {
- const QString t = QLatin1String("row ") + QString::number(row + 1)
+ const QString t = rowNumber
+ QLatin1String(" column ") + QString::number(column + 1);
widget.setItem(row, column, new QTableWidgetItem(t));
}
@@ -1874,14 +1946,15 @@ void tst_QStyleSheetStyle::QTBUG36933_brokenPseudoClassLookup()
void tst_QStyleSheetStyle::styleSheetChangeBeforePolish()
{
QWidget widget;
+ widget.setWindowTitle(QTest::currentTestFunction());
QVBoxLayout *vbox = new QVBoxLayout(&widget);
QFrame *frame = new QFrame(&widget);
- frame->setFixedSize(200, 200);
+ frame->setFixedSize(m_testSize);
frame->setStyleSheet("background-color: #FF0000;");
frame->setStyleSheet("background-color: #00FF00;");
vbox->addWidget(frame);
QFrame *frame2 = new QFrame(&widget);
- frame2->setFixedSize(200, 200);
+ frame2->setFixedSize(m_testSize);
frame2->setStyleSheet("background-color: #FF0000;");
frame2->setStyleSheet("background-color: #00FF00;");
vbox->addWidget(frame);
@@ -2106,6 +2179,8 @@ void tst_QStyleSheetStyle::highdpiImages()
QFETCH(QColor, color);
QWidget w;
+ w.setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QLatin1String("::")
+ + QLatin1String(QTest::currentDataTag()));
QScreen *screen = QGuiApplication::primaryScreen();
w.move(screen->availableGeometry().topLeft());
QHighDpiScaling::setScreenFactor(screen, screenFactor);