0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
#include <QtCore>
|
|
42 |
#include <QtGui>
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
#include <QtDebug>
|
|
45 |
#include <QMetaObject>
|
|
46 |
#ifdef QT3_SUPPORT
|
|
47 |
#include <Q3ProgressBar>
|
|
48 |
#endif
|
|
49 |
|
|
50 |
#include <private/qstylesheetstyle_p.h>
|
|
51 |
|
|
52 |
//TESTED_CLASS=
|
|
53 |
//TESTED_FILES=
|
|
54 |
|
|
55 |
class tst_QStyleSheetStyle : public QObject
|
|
56 |
{
|
|
57 |
Q_OBJECT
|
|
58 |
public:
|
|
59 |
tst_QStyleSheetStyle();
|
|
60 |
~tst_QStyleSheetStyle();
|
|
61 |
|
|
62 |
private slots:
|
|
63 |
void repolish();
|
|
64 |
void numinstances();
|
|
65 |
void widgetsBeforeAppStyleSheet();
|
|
66 |
void widgetsAfterAppStyleSheet();
|
|
67 |
void applicationStyleSheet();
|
|
68 |
void windowStyleSheet();
|
|
69 |
void widgetStyleSheet();
|
|
70 |
void reparentWithNoChildStyleSheet();
|
|
71 |
void reparentWithChildStyleSheet();
|
|
72 |
void dynamicProperty();
|
|
73 |
// NB! Invoking this slot after layoutSpacing crashes on Mac.
|
|
74 |
void namespaces();
|
|
75 |
#ifdef Q_OS_MAC
|
|
76 |
void layoutSpacing();
|
|
77 |
#endif
|
|
78 |
void qproperty();
|
|
79 |
void palettePropagation();
|
|
80 |
void fontPropagation();
|
|
81 |
void onWidgetDestroyed();
|
|
82 |
void fontPrecedence();
|
|
83 |
void focusColors();
|
|
84 |
void hoverColors();
|
|
85 |
void background();
|
|
86 |
void tabAlignement();
|
|
87 |
void attributesList();
|
|
88 |
void minmaxSizes();
|
|
89 |
void task206238_twice();
|
|
90 |
void transparent();
|
|
91 |
void proxyStyle();
|
|
92 |
void dialogButtonBox();
|
|
93 |
void emptyStyleSheet();
|
|
94 |
void toolTip();
|
|
95 |
void embeddedFonts();
|
|
96 |
void opaquePaintEvent_data();
|
|
97 |
void opaquePaintEvent();
|
|
98 |
void complexWidgetFocus();
|
|
99 |
void task188195_baseBackground();
|
|
100 |
void task232085_spinBoxLineEditBg();
|
|
101 |
|
|
102 |
//at the end because it mess with the style.
|
|
103 |
void widgetStyle();
|
|
104 |
void appStyle();
|
|
105 |
private:
|
|
106 |
QColor COLOR(const QWidget& w) {
|
|
107 |
w.ensurePolished();
|
|
108 |
return w.palette().color(w.foregroundRole());
|
|
109 |
}
|
|
110 |
QColor APPCOLOR(const QWidget& w) {
|
|
111 |
w.ensurePolished();
|
|
112 |
return qApp->palette(&w).color(w.foregroundRole());
|
|
113 |
}
|
|
114 |
QColor BACKGROUND(const QWidget& w) {
|
|
115 |
w.ensurePolished();
|
|
116 |
return w.palette().color(w.backgroundRole());
|
|
117 |
}
|
|
118 |
QColor APPBACKGROUND(const QWidget& w) {
|
|
119 |
w.ensurePolished();
|
|
120 |
return qApp->palette(&w).color(w.backgroundRole());
|
|
121 |
}
|
|
122 |
int FONTSIZE(const QWidget &w) {
|
|
123 |
w.ensurePolished();
|
|
124 |
return w.font().pointSize();
|
|
125 |
}
|
|
126 |
int APPFONTSIZE(const QWidget &w) {
|
|
127 |
return qApp->font(&w).pointSize();
|
|
128 |
}
|
|
129 |
};
|
|
130 |
|
|
131 |
tst_QStyleSheetStyle::tst_QStyleSheetStyle()
|
|
132 |
{
|
|
133 |
}
|
|
134 |
|
|
135 |
tst_QStyleSheetStyle::~tst_QStyleSheetStyle()
|
|
136 |
{
|
|
137 |
}
|
|
138 |
|
|
139 |
void tst_QStyleSheetStyle::numinstances()
|
|
140 |
{
|
|
141 |
QWidget w;
|
|
142 |
QCommonStyle *style = new QCommonStyle;
|
|
143 |
style->setParent(&w);
|
|
144 |
QWidget c(&w);
|
|
145 |
w.show();
|
|
146 |
|
|
147 |
// set and unset application stylesheet
|
|
148 |
QCOMPARE(QStyleSheetStyle::numinstances, 0);
|
|
149 |
qApp->setStyleSheet("* { color: red; }");
|
|
150 |
QCOMPARE(QStyleSheetStyle::numinstances, 1);
|
|
151 |
qApp->setStyleSheet("");
|
|
152 |
QCOMPARE(QStyleSheetStyle::numinstances, 0);
|
|
153 |
|
|
154 |
// set and unset application stylesheet+widget
|
|
155 |
qApp->setStyleSheet("* { color: red; }");
|
|
156 |
w.setStyleSheet("color: red;");
|
|
157 |
QCOMPARE(QStyleSheetStyle::numinstances, 2);
|
|
158 |
w.setStyle(style);
|
|
159 |
QCOMPARE(QStyleSheetStyle::numinstances, 2);
|
|
160 |
qApp->setStyleSheet("");
|
|
161 |
QCOMPARE(QStyleSheetStyle::numinstances, 1);
|
|
162 |
w.setStyleSheet("");
|
|
163 |
QCOMPARE(QStyleSheetStyle::numinstances, 0);
|
|
164 |
|
|
165 |
// set and unset widget stylesheet
|
|
166 |
w.setStyle(0);
|
|
167 |
w.setStyleSheet("color: red");
|
|
168 |
QCOMPARE(QStyleSheetStyle::numinstances, 1);
|
|
169 |
c.setStyle(style);
|
|
170 |
QCOMPARE(QStyleSheetStyle::numinstances, 2);
|
|
171 |
w.setStyleSheet("");
|
|
172 |
QCOMPARE(QStyleSheetStyle::numinstances, 0);
|
|
173 |
}
|
|
174 |
|
|
175 |
void tst_QStyleSheetStyle::widgetsBeforeAppStyleSheet()
|
|
176 |
{
|
|
177 |
QPushButton w1; // widget with no stylesheet
|
|
178 |
qApp->setStyleSheet("* { color: red; }");
|
|
179 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
180 |
w1.setStyleSheet("color: white");
|
|
181 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
182 |
qApp->setStyleSheet("");
|
|
183 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
184 |
w1.setStyleSheet("");
|
|
185 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
186 |
}
|
|
187 |
|
|
188 |
class FriendlySpinBox : public QSpinBox { friend class tst_QStyleSheetStyle; };
|
|
189 |
|
|
190 |
void tst_QStyleSheetStyle::widgetsAfterAppStyleSheet()
|
|
191 |
{
|
|
192 |
qApp->setStyleSheet("* { color: red; font-size: 32pt; }");
|
|
193 |
QPushButton w1;
|
|
194 |
FriendlySpinBox spin;
|
|
195 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
196 |
QVERIFY(COLOR(spin) == QColor("red"));
|
|
197 |
QVERIFY(COLOR(*spin.lineEdit()) == QColor("red"));
|
|
198 |
QCOMPARE(FONTSIZE(w1), 32);
|
|
199 |
QCOMPARE(FONTSIZE(spin), 32);
|
|
200 |
QCOMPARE(FONTSIZE(*spin.lineEdit()), 32);
|
|
201 |
w1.setStyleSheet("color: white");
|
|
202 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
203 |
QVERIFY(COLOR(spin) == QColor("red"));
|
|
204 |
QVERIFY(COLOR(*spin.lineEdit()) == QColor("red"));
|
|
205 |
w1.setStyleSheet("");
|
|
206 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
207 |
QVERIFY(COLOR(spin) == QColor("red"));
|
|
208 |
QVERIFY(COLOR(*spin.lineEdit()) == QColor("red"));
|
|
209 |
w1.setStyleSheet("color: white");
|
|
210 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
211 |
qApp->setStyleSheet("");
|
|
212 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
213 |
QVERIFY(COLOR(spin) == APPCOLOR(spin));
|
|
214 |
QVERIFY(COLOR(*spin.lineEdit()) == APPCOLOR(*spin.lineEdit()));
|
|
215 |
w1.setStyleSheet("");
|
|
216 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
217 |
// QCOMPARE(FONTSIZE(w1), APPFONTSIZE(w1)); //### task 244261
|
|
218 |
QCOMPARE(FONTSIZE(spin), APPFONTSIZE(spin));
|
|
219 |
//QCOMPARE(FONTSIZE(*spin.lineEdit()), APPFONTSIZE(*spin.lineEdit())); //### task 244261
|
|
220 |
}
|
|
221 |
|
|
222 |
void tst_QStyleSheetStyle::applicationStyleSheet()
|
|
223 |
{
|
|
224 |
QPushButton w1;
|
|
225 |
qApp->setStyleSheet("* { color: red; }");
|
|
226 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
227 |
qApp->setStyleSheet("* { color: white; }");
|
|
228 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
229 |
qApp->setStyleSheet("");
|
|
230 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
231 |
qApp->setStyleSheet("* { color: red }");
|
|
232 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
233 |
}
|
|
234 |
|
|
235 |
void tst_QStyleSheetStyle::windowStyleSheet()
|
|
236 |
{
|
|
237 |
QPushButton w1;
|
|
238 |
qApp->setStyleSheet("");
|
|
239 |
w1.setStyleSheet("* { color: red; }");
|
|
240 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
241 |
w1.setStyleSheet("* { color: white; }");
|
|
242 |
QVERIFY(COLOR(w1) == QColor("white"));
|
|
243 |
w1.setStyleSheet("");
|
|
244 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
245 |
w1.setStyleSheet("* { color: red }");
|
|
246 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
247 |
|
|
248 |
qApp->setStyleSheet("* { color: green }");
|
|
249 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
250 |
w1.setStyleSheet("");
|
|
251 |
QVERIFY(COLOR(w1) == QColor("green"));
|
|
252 |
qApp->setStyleSheet("");
|
|
253 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
254 |
}
|
|
255 |
|
|
256 |
void tst_QStyleSheetStyle::widgetStyleSheet()
|
|
257 |
{
|
|
258 |
QPushButton w1;
|
|
259 |
QPushButton *pb = new QPushButton(&w1);
|
|
260 |
QPushButton &w2 = *pb;
|
|
261 |
|
|
262 |
qApp->setStyleSheet("");
|
|
263 |
w1.setStyleSheet("* { color: red }");
|
|
264 |
QVERIFY(COLOR(w1) == QColor("red"));
|
|
265 |
QVERIFY(COLOR(w2) == QColor("red"));
|
|
266 |
|
|
267 |
w2.setStyleSheet("* { color: white }");
|
|
268 |
QVERIFY(COLOR(w2) == QColor("white"));
|
|
269 |
|
|
270 |
w1.setStyleSheet("* { color: blue }");
|
|
271 |
QVERIFY(COLOR(w1) == QColor("blue"));
|
|
272 |
QVERIFY(COLOR(w2) == QColor("white"));
|
|
273 |
|
|
274 |
w1.setStyleSheet("");
|
|
275 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
276 |
QVERIFY(COLOR(w2) == QColor("white"));
|
|
277 |
|
|
278 |
w2.setStyleSheet("");
|
|
279 |
QVERIFY(COLOR(w1) == APPCOLOR(w1));
|
|
280 |
QVERIFY(COLOR(w2) == APPCOLOR(w2));
|
|
281 |
}
|
|
282 |
|
|
283 |
void tst_QStyleSheetStyle::reparentWithNoChildStyleSheet()
|
|
284 |
{
|
|
285 |
QPushButton p1, p2;
|
|
286 |
QPushButton *pb = new QPushButton(&p1);
|
|
287 |
QPushButton &c1 = *pb; // child with no stylesheet
|
|
288 |
|
|
289 |
qApp->setStyleSheet("");
|
|
290 |
p1.setStyleSheet("* { color: red }");
|
|
291 |
QVERIFY(COLOR(c1) == QColor("red"));
|
|
292 |
c1.setParent(&p2);
|
|
293 |
QVERIFY(COLOR(c1) == APPCOLOR(c1));
|
|
294 |
|
|
295 |
p2.setStyleSheet("* { color: white }");
|
|
296 |
QVERIFY(COLOR(c1) == QColor("white"));
|
|
297 |
|
|
298 |
c1.setParent(&p1);
|
|
299 |
QVERIFY(COLOR(c1) == QColor("red"));
|
|
300 |
|
|
301 |
qApp->setStyleSheet("* { color: blue }");
|
|
302 |
c1.setParent(0);
|
|
303 |
QVERIFY(COLOR(c1) == QColor("blue"));
|
|
304 |
delete pb;
|
|
305 |
}
|
|
306 |
|
|
307 |
void tst_QStyleSheetStyle::reparentWithChildStyleSheet()
|
|
308 |
{
|
|
309 |
qApp->setStyleSheet("");
|
|
310 |
QPushButton p1, p2;
|
|
311 |
QPushButton *pb = new QPushButton(&p1);
|
|
312 |
QPushButton &c1 = *pb;
|
|
313 |
|
|
314 |
c1.setStyleSheet("background: gray");
|
|
315 |
QVERIFY(BACKGROUND(c1) == QColor("gray"));
|
|
316 |
c1.setParent(&p2);
|
|
317 |
QVERIFY(BACKGROUND(c1) == QColor("gray"));
|
|
318 |
|
|
319 |
qApp->setStyleSheet("* { color: white }");
|
|
320 |
c1.setParent(&p1);
|
|
321 |
QVERIFY(BACKGROUND(c1) == QColor("gray"));
|
|
322 |
QVERIFY(COLOR(c1) == QColor("white"));
|
|
323 |
}
|
|
324 |
|
|
325 |
void tst_QStyleSheetStyle::repolish()
|
|
326 |
{
|
|
327 |
qApp->setStyleSheet("");
|
|
328 |
QPushButton p1;
|
|
329 |
p1.setStyleSheet("color: red; background: white");
|
|
330 |
QVERIFY(BACKGROUND(p1) == QColor("white"));
|
|
331 |
p1.setStyleSheet("background: white");
|
|
332 |
QVERIFY(COLOR(p1) == APPCOLOR(p1));
|
|
333 |
p1.setStyleSheet("color: red");
|
|
334 |
QVERIFY(COLOR(p1) == QColor("red"));
|
|
335 |
QVERIFY(BACKGROUND(p1) == APPBACKGROUND(p1));
|
|
336 |
p1.setStyleSheet("");
|
|
337 |
QVERIFY(COLOR(p1) == APPCOLOR(p1));
|
|
338 |
QVERIFY(BACKGROUND(p1) == APPBACKGROUND(p1));
|
|
339 |
}
|
|
340 |
|
|
341 |
void tst_QStyleSheetStyle::widgetStyle()
|
|
342 |
{
|
|
343 |
qApp->setStyleSheet("");
|
|
344 |
|
|
345 |
QWidget *window1 = new QWidget;
|
|
346 |
window1->setObjectName("window1");
|
|
347 |
QWidget *widget1 = new QWidget(window1);
|
|
348 |
widget1->setObjectName("widget1");
|
|
349 |
QWidget *widget2 = new QWidget;
|
|
350 |
widget2->setObjectName("widget2");
|
|
351 |
QWidget *window2 = new QWidget;
|
|
352 |
window2->setObjectName("window2");
|
|
353 |
window1->ensurePolished();
|
|
354 |
window2->ensurePolished();
|
|
355 |
widget1->ensurePolished();
|
|
356 |
widget2->ensurePolished();
|
|
357 |
|
|
358 |
QWindowsStyle style1, style2;
|
|
359 |
QPointer<QStyle> pstyle1 = &style1;
|
|
360 |
QPointer<QStyle> pstyle2 = &style2;
|
|
361 |
|
|
362 |
QStyle *appStyle = qApp->style();
|
|
363 |
|
|
364 |
// Sanity: By default, a window inherits the application style
|
|
365 |
QCOMPARE(appStyle, window1->style());
|
|
366 |
|
|
367 |
// Setting a custom style on a widget
|
|
368 |
window1->setStyle(&style1);
|
|
369 |
QCOMPARE(static_cast<QStyle *>(&style1), window1->style());
|
|
370 |
|
|
371 |
// Setting another style must not delete the older style
|
|
372 |
window1->setStyle(&style2);
|
|
373 |
QCOMPARE(static_cast<QStyle *>(&style2), window1->style());
|
|
374 |
QVERIFY(!pstyle1.isNull()); // case we have not already crashed
|
|
375 |
|
|
376 |
// Setting null style must make it follow the qApp style
|
|
377 |
window1->setStyle(0);
|
|
378 |
QCOMPARE(window1->style(), appStyle);
|
|
379 |
QVERIFY(!pstyle2.isNull()); // case we have not already crashed
|
|
380 |
QVERIFY(!pstyle2.isNull()); // case we have not already crashed
|
|
381 |
|
|
382 |
// Sanity: Set the stylesheet
|
|
383 |
window1->setStyleSheet(":x { }");
|
|
384 |
|
|
385 |
QPointer<QStyleSheetStyle> proxy = (QStyleSheetStyle *)window1->style();
|
|
386 |
QVERIFY(!proxy.isNull());
|
|
387 |
QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle"); // must be our proxy
|
|
388 |
QVERIFY(proxy->base == 0); // and follows the application
|
|
389 |
|
|
390 |
// Set the stylesheet
|
|
391 |
window1->setStyle(&style1);
|
|
392 |
QVERIFY(proxy.isNull()); // we create a new one each time
|
|
393 |
proxy = (QStyleSheetStyle *)window1->style();
|
|
394 |
QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle"); // it is a proxy
|
|
395 |
QCOMPARE(proxy->baseStyle(), static_cast<QStyle *>(&style1)); // must have been replaced with the new one
|
|
396 |
|
|
397 |
// Update the stylesheet and check nothing changes
|
|
398 |
window1->setStyleSheet(":y { }");
|
|
399 |
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle"); // it is a proxy
|
|
400 |
QCOMPARE(proxy->baseStyle(), static_cast<QStyle *>(&style1)); // the same guy
|
|
401 |
|
|
402 |
// Remove the stylesheet
|
|
403 |
proxy = (QStyleSheetStyle *)window1->style();
|
|
404 |
window1->setStyleSheet("");
|
|
405 |
QVERIFY(proxy.isNull()); // should have disappeared
|
|
406 |
QCOMPARE(window1->style(), static_cast<QStyle *>(&style1)); // its restored
|
|
407 |
|
|
408 |
// Style Sheet existing children propagation
|
|
409 |
window1->setStyleSheet(":z { }");
|
|
410 |
proxy = (QStyleSheetStyle *)window1->style();
|
|
411 |
QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle");
|
|
412 |
QCOMPARE(window1->style(), widget1->style()); // proxy must have propagated
|
|
413 |
QCOMPARE(widget2->style(), appStyle); // widget2 is following the app style
|
|
414 |
|
|
415 |
// Style Sheet automatic propagation to new children
|
|
416 |
widget2->setParent(window1); // reparent in!
|
|
417 |
QCOMPARE(window1->style(), widget2->style()); // proxy must have propagated
|
|
418 |
|
|
419 |
// Style Sheet automatic removal from children who abandoned their parents
|
|
420 |
window2->setStyle(&style2);
|
|
421 |
widget2->setParent(window2); // reparent
|
|
422 |
QCOMPARE(widget2->style(), appStyle); // widget2 is following the app style
|
|
423 |
|
|
424 |
// Style Sheet propagation on a child widget with a custom style
|
|
425 |
widget2->setStyle(&style1);
|
|
426 |
window2->setStyleSheet(":x { }");
|
|
427 |
proxy = (QStyleSheetStyle *)widget2->style();
|
|
428 |
QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle");
|
|
429 |
QCOMPARE(proxy->baseStyle(), static_cast<QStyle *>(&style1));
|
|
430 |
|
|
431 |
// Style Sheet propagation on a child widget with a custom style already set
|
|
432 |
window2->setStyleSheet("");
|
|
433 |
QCOMPARE(window2->style(), static_cast<QStyle *>(&style2));
|
|
434 |
QCOMPARE(widget2->style(), static_cast<QStyle *>(&style1));
|
|
435 |
widget2->setStyle(0);
|
|
436 |
window2->setStyleSheet(":x { }");
|
|
437 |
widget2->setStyle(&style1);
|
|
438 |
proxy = (QStyleSheetStyle *)widget2->style();
|
|
439 |
QCOMPARE(proxy->metaObject()->className(), "QStyleSheetStyle");
|
|
440 |
|
|
441 |
// QApplication, QWidget both having a style sheet
|
|
442 |
|
|
443 |
// clean everything out
|
|
444 |
window1->setStyle(0);
|
|
445 |
window1->setStyleSheet("");
|
|
446 |
window2->setStyle(0);
|
|
447 |
window2->setStyleSheet("");
|
|
448 |
qApp->setStyle(0);
|
|
449 |
|
|
450 |
qApp->setStyleSheet("may_insanity_prevail { }"); // app has styleshet
|
|
451 |
QCOMPARE(window1->style(), qApp->style());
|
|
452 |
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle");
|
|
453 |
QCOMPARE(widget1->style()->metaObject()->className(), "QStyleSheetStyle"); // check the child
|
|
454 |
window1->setStyleSheet("may_more_insanity_prevail { }"); // window has stylesheet
|
|
455 |
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle"); // a new one
|
|
456 |
QCOMPARE(widget1->style(), window1->style()); // child follows...
|
|
457 |
proxy = (QStyleSheetStyle *) window1->style();
|
|
458 |
QWindowsStyle *newStyle = new QWindowsStyle;
|
|
459 |
qApp->setStyle(newStyle); // set a custom style on app
|
|
460 |
proxy = (QStyleSheetStyle *) window1->style();
|
|
461 |
QCOMPARE(proxy->baseStyle(), static_cast<QStyle *>(newStyle)); // magic ;) the widget still follows the application
|
|
462 |
QCOMPARE(static_cast<QStyle *>(proxy), widget1->style()); // child still follows...
|
|
463 |
|
|
464 |
window1->setStyleSheet(""); // remove stylesheet
|
|
465 |
QCOMPARE(window1->style(), qApp->style()); // is this cool or what
|
|
466 |
QCOMPARE(widget1->style(), qApp->style()); // annoying child follows...
|
|
467 |
QWindowsStyle wndStyle;
|
|
468 |
window1->setStyle(&wndStyle);
|
|
469 |
QCOMPARE(window1->style()->metaObject()->className(), "QStyleSheetStyle"); // auto wraps it
|
|
470 |
QCOMPARE(widget1->style(), window1->style()); // and auto propagates to child
|
|
471 |
qApp->setStyleSheet(""); // remove the app stylesheet
|
|
472 |
QCOMPARE(window1->style(), static_cast<QStyle *>(&wndStyle)); // auto dewrap
|
|
473 |
QCOMPARE(widget1->style(), qApp->style()); // and child state is restored
|
|
474 |
window1->setStyle(0); // let sanity prevail
|
|
475 |
qApp->setStyle(0);
|
|
476 |
|
|
477 |
delete window1;
|
|
478 |
delete widget2;
|
|
479 |
delete window2;
|
|
480 |
}
|
|
481 |
|
|
482 |
void tst_QStyleSheetStyle::appStyle()
|
|
483 |
{
|
|
484 |
qApp->setStyleSheet("");
|
|
485 |
// qApp style can never be 0
|
|
486 |
QVERIFY(QApplication::style() != 0);
|
|
487 |
QPointer<QStyle> style1 = new QWindowsStyle;
|
|
488 |
QPointer<QStyle> style2 = new QWindowsStyle;
|
|
489 |
qApp->setStyle(style1);
|
|
490 |
// Basic sanity
|
|
491 |
QVERIFY(qApp->style() == style1);
|
|
492 |
qApp->setStyle(style2);
|
|
493 |
QVERIFY(style1.isNull()); // qApp must have taken ownership and deleted it
|
|
494 |
// Setting null should not crash
|
|
495 |
qApp->setStyle(0);
|
|
496 |
QVERIFY(qApp->style() == style2);
|
|
497 |
|
|
498 |
// Set the stylesheet
|
|
499 |
qApp->setStyleSheet("whatever");
|
|
500 |
QPointer<QStyleSheetStyle> sss = (QStyleSheetStyle *)qApp->style();
|
|
501 |
QVERIFY(!sss.isNull());
|
|
502 |
QCOMPARE(sss->metaObject()->className(), "QStyleSheetStyle"); // must be our proxy now
|
|
503 |
QVERIFY(!style2.isNull()); // this should exist as it is the base of the proxy
|
|
504 |
QVERIFY(sss->baseStyle() == style2);
|
|
505 |
style1 = new QWindowsStyle;
|
|
506 |
qApp->setStyle(style1);
|
|
507 |
QVERIFY(style2.isNull()); // should disappear automatically
|
|
508 |
QVERIFY(sss.isNull()); // should disappear automatically
|
|
509 |
|
|
510 |
// Update the stylesheet and check nothing changes
|
|
511 |
sss = (QStyleSheetStyle *)qApp->style();
|
|
512 |
qApp->setStyleSheet("whatever2");
|
|
513 |
QVERIFY(qApp->style() == sss);
|
|
514 |
QVERIFY(sss->baseStyle() == style1);
|
|
515 |
|
|
516 |
// Revert the stylesheet
|
|
517 |
qApp->setStyleSheet("");
|
|
518 |
QVERIFY(sss.isNull()); // should have disappeared
|
|
519 |
QVERIFY(qApp->style() == style1);
|
|
520 |
|
|
521 |
qApp->setStyleSheet("");
|
|
522 |
QVERIFY(qApp->style() == style1);
|
|
523 |
}
|
|
524 |
|
|
525 |
void tst_QStyleSheetStyle::dynamicProperty()
|
|
526 |
{
|
|
527 |
qApp->setStyleSheet(QString());
|
|
528 |
|
|
529 |
QString appStyle = qApp->style()->metaObject()->className();
|
|
530 |
QPushButton pb1, pb2;
|
|
531 |
pb1.setProperty("type", "critical");
|
|
532 |
qApp->setStyleSheet("*[class~=\"QPushButton\"] { color: red; } *[type=\"critical\"] { background: white; }");
|
|
533 |
QVERIFY(COLOR(pb1) == Qt::red);
|
|
534 |
QVERIFY(BACKGROUND(pb1) == Qt::white);
|
|
535 |
|
|
536 |
pb2.setProperty("class", "critical"); // dynamic class
|
|
537 |
pb2.setStyleSheet(QLatin1String(".critical[style~=\"") + appStyle + "\"] { color: blue }");
|
|
538 |
pb2.show();
|
|
539 |
|
|
540 |
QVERIFY(COLOR(pb2) == Qt::blue);
|
|
541 |
}
|
|
542 |
|
|
543 |
#ifdef Q_OS_MAC
|
|
544 |
#include <QtGui/QMacStyle>
|
|
545 |
void tst_QStyleSheetStyle::layoutSpacing()
|
|
546 |
{
|
|
547 |
qApp->setStyleSheet("* { color: red }");
|
|
548 |
QCheckBox ck1;
|
|
549 |
QCheckBox ck2;
|
|
550 |
QWidget window;
|
|
551 |
int spacing_widgetstyle = window.style()->layoutSpacing(ck1.sizePolicy().controlType(), ck2.sizePolicy().controlType(), Qt::Vertical);
|
|
552 |
int spacing_style = window.style()->layoutSpacing(ck1.sizePolicy().controlType(), ck2.sizePolicy().controlType(), Qt::Vertical);
|
|
553 |
QCOMPARE(spacing_widgetstyle, spacing_style);
|
|
554 |
}
|
|
555 |
#endif
|
|
556 |
|
|
557 |
void tst_QStyleSheetStyle::qproperty()
|
|
558 |
{
|
|
559 |
QPushButton pb;
|
|
560 |
pb.setStyleSheet("QPushButton { qproperty-text: hello; qproperty-checkable: true; qproperty-checked: 1}");
|
|
561 |
pb.ensurePolished();
|
|
562 |
QCOMPARE(pb.text(), QString("hello"));
|
|
563 |
QCOMPARE(pb.isCheckable(), true);
|
|
564 |
QCOMPARE(pb.isChecked(), true);
|
|
565 |
}
|
|
566 |
|
|
567 |
namespace ns {
|
|
568 |
class PushButton1 : public QPushButton {
|
|
569 |
Q_OBJECT
|
|
570 |
public:
|
|
571 |
PushButton1() { }
|
|
572 |
};
|
|
573 |
class PushButton2 : public PushButton1 {
|
|
574 |
Q_OBJECT
|
|
575 |
public:
|
|
576 |
PushButton2() { setProperty("class", "PushButtonTwo PushButtonDuo"); }
|
|
577 |
};
|
|
578 |
}
|
|
579 |
|
|
580 |
void tst_QStyleSheetStyle::namespaces()
|
|
581 |
{
|
|
582 |
ns::PushButton1 pb1;
|
|
583 |
qApp->setStyleSheet("ns--PushButton1 { background: white }");
|
|
584 |
QVERIFY(BACKGROUND(pb1) == QColor("white"));
|
|
585 |
qApp->setStyleSheet(".ns--PushButton1 { background: red }");
|
|
586 |
QVERIFY(BACKGROUND(pb1) == QColor("red"));
|
|
587 |
|
|
588 |
ns::PushButton2 pb2;
|
|
589 |
qApp->setStyleSheet("ns--PushButton1 { background: blue}");
|
|
590 |
QVERIFY(BACKGROUND(pb2) == QColor("blue"));
|
|
591 |
qApp->setStyleSheet("ns--PushButton2 { background: magenta }");
|
|
592 |
QVERIFY(BACKGROUND(pb2) == QColor("magenta"));
|
|
593 |
qApp->setStyleSheet(".PushButtonTwo { background: white; }");
|
|
594 |
QVERIFY(BACKGROUND(pb2) == QColor("white"));
|
|
595 |
qApp->setStyleSheet(".PushButtonDuo { background: red; }");
|
|
596 |
QVERIFY(BACKGROUND(pb2) == QColor("red"));
|
|
597 |
}
|
|
598 |
|
|
599 |
void tst_QStyleSheetStyle::palettePropagation()
|
|
600 |
{
|
|
601 |
qApp->setStyleSheet("");
|
|
602 |
QGroupBox gb;
|
|
603 |
QPushButton *push = new QPushButton(&gb);
|
|
604 |
QPushButton &pb = *push;
|
|
605 |
push->setText("AsdF");
|
|
606 |
|
|
607 |
gb.setStyleSheet("QGroupBox { color: red }");
|
|
608 |
QVERIFY(COLOR(gb) == Qt::red);
|
|
609 |
QVERIFY(COLOR(pb) == APPCOLOR(pb)); // palette shouldn't propagate
|
|
610 |
gb.setStyleSheet("QGroupBox * { color: red }");
|
|
611 |
|
|
612 |
QVERIFY(COLOR(pb) == Qt::red);
|
|
613 |
QVERIFY(COLOR(gb) == APPCOLOR(gb));
|
|
614 |
|
|
615 |
QWidget window;
|
|
616 |
window.setStyleSheet("* { color: white; }");
|
|
617 |
pb.setParent(&window);
|
|
618 |
QVERIFY(COLOR(pb) == Qt::white);
|
|
619 |
}
|
|
620 |
|
|
621 |
void tst_QStyleSheetStyle::fontPropagation()
|
|
622 |
{
|
|
623 |
qApp->setStyleSheet("");
|
|
624 |
QComboBox cb;
|
|
625 |
cb.addItem("item1");
|
|
626 |
cb.addItem("item2");
|
|
627 |
|
|
628 |
QAbstractItemView *popup = cb.view();
|
|
629 |
int viewFontSize = FONTSIZE(*popup);
|
|
630 |
|
|
631 |
cb.setStyleSheet("QComboBox { font-size: 20pt; }");
|
|
632 |
QVERIFY(FONTSIZE(cb) == 20);
|
|
633 |
QVERIFY(FONTSIZE(*popup) == viewFontSize);
|
|
634 |
QGroupBox gb;
|
|
635 |
QPushButton *push = new QPushButton(&gb);
|
|
636 |
QPushButton &pb = *push;
|
|
637 |
int buttonFontSize = FONTSIZE(pb);
|
|
638 |
int gbFontSize = FONTSIZE(gb);
|
|
639 |
|
|
640 |
gb.setStyleSheet("QGroupBox { font-size: 20pt }");
|
|
641 |
QVERIFY(FONTSIZE(gb) == 20);
|
|
642 |
QVERIFY(FONTSIZE(pb) == buttonFontSize); // font does not propagate
|
|
643 |
gb.setStyleSheet("QGroupBox * { font-size: 20pt; }");
|
|
644 |
QVERIFY(FONTSIZE(gb) == gbFontSize);
|
|
645 |
QVERIFY(FONTSIZE(pb) == 20);
|
|
646 |
|
|
647 |
QWidget window;
|
|
648 |
window.setStyleSheet("* { font-size: 10pt }");
|
|
649 |
pb.setParent(&window);
|
|
650 |
QCOMPARE(FONTSIZE(pb), 10);
|
|
651 |
window.setStyleSheet("");
|
|
652 |
QVERIFY(FONTSIZE(pb) == buttonFontSize);
|
|
653 |
|
|
654 |
QTabWidget tw;
|
|
655 |
tw.setStyleSheet("QTabWidget { font-size: 20pt; }");
|
|
656 |
QVERIFY(FONTSIZE(tw) == 20);
|
|
657 |
QWidget *child = qFindChild<QWidget *>(&tw, "qt_tabwidget_tabbar");
|
|
658 |
QVERIFY2(child, "QTabWidget did not contain a widget named \"qt_tabwidget_tabbar\"");
|
|
659 |
QVERIFY(FONTSIZE(*child) == 20);
|
|
660 |
}
|
|
661 |
|
|
662 |
void tst_QStyleSheetStyle::onWidgetDestroyed()
|
|
663 |
{
|
|
664 |
qApp->setStyleSheet("");
|
|
665 |
QLabel *l = new QLabel;
|
|
666 |
l->setStyleSheet("QLabel { color: red }");
|
|
667 |
QPointer<QStyleSheetStyle> ss = (QStyleSheetStyle *) l->style();
|
|
668 |
delete l;
|
|
669 |
QVERIFY(ss.isNull());
|
|
670 |
}
|
|
671 |
|
|
672 |
void tst_QStyleSheetStyle::fontPrecedence()
|
|
673 |
{
|
|
674 |
QLineEdit edit;
|
|
675 |
edit.show();
|
|
676 |
QFont font;
|
|
677 |
QVERIFY(FONTSIZE(edit) != 22); // Sanity check to make sure this test makes sense.
|
|
678 |
edit.setStyleSheet("QLineEdit { font-size: 22pt; }");
|
|
679 |
QCOMPARE(FONTSIZE(edit), 22);
|
|
680 |
font.setPointSize(16);
|
|
681 |
edit.setFont(font);
|
|
682 |
QCOMPARE(FONTSIZE(edit), 22);
|
|
683 |
edit.setStyleSheet("");
|
|
684 |
QCOMPARE(FONTSIZE(edit), 16);
|
|
685 |
font.setPointSize(18);
|
|
686 |
edit.setFont(font);
|
|
687 |
QCOMPARE(FONTSIZE(edit), 18);
|
|
688 |
edit.setStyleSheet("QLineEdit { font-size: 20pt; }");
|
|
689 |
QCOMPARE(FONTSIZE(edit), 20);
|
|
690 |
edit.setStyleSheet("");
|
|
691 |
QCOMPARE(FONTSIZE(edit), 18);
|
|
692 |
edit.hide();
|
|
693 |
|
|
694 |
QLineEdit edit2;
|
|
695 |
edit2.setReadOnly(true);
|
|
696 |
edit2.setStyleSheet("QLineEdit { font-size: 24pt; } QLineEdit:read-only { font-size: 26pt; }");
|
|
697 |
QCOMPARE(FONTSIZE(edit2), 26);
|
|
698 |
}
|
|
699 |
|
|
700 |
static bool testForColors(const QImage& image, const QColor& color)
|
|
701 |
{
|
|
702 |
int count = 0;
|
|
703 |
QRgb rgb = color.rgba();
|
|
704 |
for (int y = 0; y < image.height(); ++y) {
|
|
705 |
for (int x = 0; x < image.width(); ++x) {
|
|
706 |
// Because of antialiasing we allow a certain range of errors here.
|
|
707 |
QRgb pixel = image.pixel(x, y);
|
|
708 |
if (qAbs((int)(pixel & 0xff) - (int)(rgb & 0xff)) +
|
|
709 |
qAbs((int)((pixel & 0xff00) >> 8) - (int)((rgb & 0xff00) >> 8)) +
|
|
710 |
qAbs((int)((pixel & 0xff0000) >> 16) - (int)((rgb & 0xff0000) >> 16)) <= 50) {
|
|
711 |
if (++count >= 10) {
|
|
712 |
return true;
|
|
713 |
}
|
|
714 |
}
|
|
715 |
}
|
|
716 |
}
|
|
717 |
|
|
718 |
return false;
|
|
719 |
}
|
|
720 |
|
|
721 |
void tst_QStyleSheetStyle::focusColors()
|
|
722 |
{
|
|
723 |
// Tests if colors can be changed by altering the focus of the widget.
|
|
724 |
// To avoid messy pixel-by-pixel comparison, we assume that the goal
|
|
725 |
// is reached if at least ten pixels of the right color can be found in
|
|
726 |
// the image.
|
|
727 |
// For this reason, we use unusual and extremely ugly colors! :-)
|
|
728 |
|
|
729 |
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC) && !(defined(Q_OS_LINUX) && defined(Q_CC_GNU) \
|
|
730 |
&& !defined(Q_CC_INTEL))
|
|
731 |
QSKIP("This is a fragile test which fails on many esoteric platforms "
|
|
732 |
"because of focus problems. "
|
|
733 |
"That doesn't mean that the feature doesn't work in practice.", SkipAll);
|
|
734 |
#endif
|
|
735 |
/* Disabled changing style in order not to mess with the application style
|
|
736 |
#ifdef Q_OS_MAC
|
|
737 |
int styleCount = 3;
|
|
738 |
QStyle *styles[3];
|
|
739 |
#else
|
|
740 |
int styleCount = 2;
|
|
741 |
QStyle *styles[2];
|
|
742 |
#endif
|
|
743 |
|
|
744 |
styles[0] = new QPlastiqueStyle;
|
|
745 |
styles[1] = new QWindowsStyle;
|
|
746 |
|
|
747 |
#ifdef Q_OS_MAC
|
|
748 |
styles[2] = new QMacStyle;
|
|
749 |
#endif
|
|
750 |
|
|
751 |
|
|
752 |
for (int i = 0; i < styleCount; ++i) {
|
|
753 |
qApp->setStyle(styles[i]);
|
|
754 |
*/
|
|
755 |
QList<QWidget *> widgets;
|
|
756 |
widgets << new QPushButton("TESTING");
|
|
757 |
widgets << new QLineEdit("TESTING");
|
|
758 |
widgets << new QLabel("TESTING");
|
|
759 |
QSpinBox *spinbox = new QSpinBox;
|
|
760 |
spinbox->setValue(8888);
|
|
761 |
widgets << spinbox;
|
|
762 |
QComboBox *combobox = new QComboBox;
|
|
763 |
combobox->setEditable(true);
|
|
764 |
combobox->addItems(QStringList() << "TESTING");
|
|
765 |
widgets << combobox;
|
|
766 |
widgets << new QLabel("TESTING");
|
|
767 |
|
|
768 |
#ifdef Q_WS_QWS
|
|
769 |
// QWS has its own special focus logic which is slightly different
|
|
770 |
// and I don't dare change it at this point, because someone will
|
|
771 |
// be relying on it. It means that setFocus() on a NoFocus widget (i.e.
|
|
772 |
// a QLabel) will not work before the window is activated.
|
|
773 |
widgets[2]->setFocusPolicy(Qt::StrongFocus);
|
|
774 |
#endif
|
|
775 |
|
|
776 |
foreach (QWidget *widget, widgets) {
|
|
777 |
QDialog frame;
|
|
778 |
QLayout* layout = new QGridLayout;
|
|
779 |
|
|
780 |
QLineEdit* dummy = new QLineEdit; // Avoids initial focus.
|
|
781 |
|
|
782 |
widget->setStyleSheet("*:focus { border:none; background: #e8ff66; color: #ff0084 }");
|
|
783 |
|
|
784 |
layout->addWidget(dummy);
|
|
785 |
layout->addWidget(widget);
|
|
786 |
frame.setLayout(layout);
|
|
787 |
|
|
788 |
frame.show();
|
|
789 |
#ifdef Q_WS_X11
|
|
790 |
qt_x11_wait_for_window_manager(&frame);
|
|
791 |
#endif
|
|
792 |
QApplication::setActiveWindow(&frame);
|
|
793 |
widget->setFocus();
|
|
794 |
QApplication::processEvents();
|
|
795 |
|
|
796 |
QImage image(frame.width(), frame.height(), QImage::Format_ARGB32);
|
|
797 |
frame.render(&image);
|
|
798 |
if (image.depth() < 24) {
|
|
799 |
QSKIP("Test doesn't support color depth < 24", SkipAll);
|
|
800 |
}
|
|
801 |
|
|
802 |
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
|
|
803 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
804 |
+ " did not contain background color #e8ff66, using style "
|
|
805 |
+ QString::fromLatin1(qApp->style()->metaObject()->className()))
|
|
806 |
.toLocal8Bit().constData());
|
|
807 |
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
|
|
808 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
809 |
+ " did not contain text color #ff0084, using style "
|
|
810 |
+ QString::fromLatin1(qApp->style()->metaObject()->className()))
|
|
811 |
.toLocal8Bit().constData());
|
|
812 |
}
|
|
813 |
// }
|
|
814 |
}
|
|
815 |
|
|
816 |
|
|
817 |
void tst_QStyleSheetStyle::hoverColors()
|
|
818 |
{
|
|
819 |
QList<QWidget *> widgets;
|
|
820 |
widgets << new QPushButton("TESTING");
|
|
821 |
widgets << new QLineEdit("TESTING");
|
|
822 |
widgets << new QLabel("TESTING");
|
|
823 |
QSpinBox *spinbox = new QSpinBox;
|
|
824 |
spinbox->setValue(8888);
|
|
825 |
widgets << spinbox;
|
|
826 |
QComboBox *combobox = new QComboBox;
|
|
827 |
combobox->setEditable(true);
|
|
828 |
combobox->addItems(QStringList() << "TESTING");
|
|
829 |
widgets << combobox;
|
|
830 |
widgets << new QLabel("<b>TESTING</b>");
|
|
831 |
|
|
832 |
foreach (QWidget *widget, widgets) {
|
|
833 |
//without Qt::X11BypassWindowManagerHint the window manager may move the window after we moved the cursor
|
|
834 |
QDialog frame(0, Qt::X11BypassWindowManagerHint);
|
|
835 |
QLayout* layout = new QGridLayout;
|
|
836 |
|
|
837 |
QLineEdit* dummy = new QLineEdit;
|
|
838 |
|
|
839 |
widget->setStyleSheet("*:hover { border:none; background: #e8ff66; color: #ff0084 }");
|
|
840 |
|
|
841 |
layout->addWidget(dummy);
|
|
842 |
layout->addWidget(widget);
|
|
843 |
frame.setLayout(layout);
|
|
844 |
|
|
845 |
frame.show();
|
|
846 |
#ifdef Q_WS_QWS
|
|
847 |
//QWS does not implement enter/leave when windows are shown underneath the cursor
|
|
848 |
QCursor::setPos(QPoint(0,0));
|
|
849 |
#endif
|
|
850 |
|
|
851 |
#ifdef Q_WS_X11
|
|
852 |
qt_x11_wait_for_window_manager(&frame);
|
|
853 |
#endif
|
|
854 |
QApplication::setActiveWindow(&frame);
|
|
855 |
QTest::qWait(60);
|
|
856 |
//move the mouse inside the widget, it should be colored
|
|
857 |
QTest::mouseMove ( widget, QPoint(5,5));
|
|
858 |
QTest::qWait(60);
|
|
859 |
|
|
860 |
QVERIFY(widget->testAttribute(Qt::WA_UnderMouse));
|
|
861 |
|
|
862 |
QImage image(frame.width(), frame.height(), QImage::Format_ARGB32);
|
|
863 |
frame.render(&image);
|
|
864 |
|
|
865 |
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
|
|
866 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
867 |
+ " did not contain background color #e8ff66").toLocal8Bit().constData());
|
|
868 |
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
|
|
869 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
870 |
+ " did not contain text color #ff0084").toLocal8Bit().constData());
|
|
871 |
|
|
872 |
//move the mouse outside the widget, it should NOT be colored
|
|
873 |
QTest::mouseMove ( dummy, QPoint(5,5));
|
|
874 |
QTest::qWait(60);
|
|
875 |
|
|
876 |
frame.render(&image);
|
|
877 |
|
|
878 |
QVERIFY2(!testForColors(image, QColor(0xe8, 0xff, 0x66)),
|
|
879 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
880 |
+ " did contain background color #e8ff66").toLocal8Bit().constData());
|
|
881 |
QVERIFY2(!testForColors(image, QColor(0xff, 0x00, 0x84)),
|
|
882 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
883 |
+ " did contain text color #ff0084").toLocal8Bit().constData());
|
|
884 |
|
|
885 |
//move the mouse again inside the widget, it should be colored
|
|
886 |
QTest::mouseMove (widget, QPoint(5,5));
|
|
887 |
QTest::qWait(60);
|
|
888 |
|
|
889 |
QVERIFY(widget->testAttribute(Qt::WA_UnderMouse));
|
|
890 |
|
|
891 |
frame.render(&image);
|
|
892 |
|
|
893 |
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
|
|
894 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
895 |
+ " did not contain background color #e8ff66").toLocal8Bit().constData());
|
|
896 |
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
|
|
897 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
898 |
+ " did not contain text color #ff0084").toLocal8Bit().constData());
|
|
899 |
}
|
|
900 |
|
|
901 |
}
|
|
902 |
|
|
903 |
class SingleInheritanceDialog : public QDialog
|
|
904 |
{
|
|
905 |
Q_OBJECT
|
|
906 |
public:
|
|
907 |
SingleInheritanceDialog(QWidget *w = 0) :
|
|
908 |
QDialog(w)
|
|
909 |
{
|
|
910 |
}
|
|
911 |
};
|
|
912 |
|
|
913 |
class DoubleInheritanceDialog : public SingleInheritanceDialog
|
|
914 |
{
|
|
915 |
Q_OBJECT
|
|
916 |
public:
|
|
917 |
DoubleInheritanceDialog(QWidget *w = 0) :
|
|
918 |
SingleInheritanceDialog(w)
|
|
919 |
{
|
|
920 |
}
|
|
921 |
};
|
|
922 |
|
|
923 |
void tst_QStyleSheetStyle::background()
|
|
924 |
{
|
|
925 |
const int number = 4;
|
|
926 |
QWidget* widgets[number];
|
|
927 |
// Testing inheritance styling of QDialog.
|
|
928 |
widgets[0] = new SingleInheritanceDialog;
|
|
929 |
widgets[0]->setStyleSheet("* { background-color: #e8ff66; }");
|
|
930 |
widgets[1] = new DoubleInheritanceDialog;
|
|
931 |
widgets[1]->setStyleSheet("* { background-color: #e8ff66; }");
|
|
932 |
|
|
933 |
// Testing gradients in QComboBox.
|
|
934 |
QLayout* layout;
|
|
935 |
QComboBox* cb;
|
|
936 |
// First color
|
|
937 |
widgets[2] = new QDialog;
|
|
938 |
layout = new QGridLayout;
|
|
939 |
cb = new QComboBox;
|
|
940 |
cb->setStyleSheet("* { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #e8ff66, stop:1 #000000); }");
|
|
941 |
layout->addWidget(cb);
|
|
942 |
widgets[2]->setLayout(layout);
|
|
943 |
// Second color
|
|
944 |
widgets[3] = new QDialog;
|
|
945 |
layout = new QGridLayout;
|
|
946 |
cb = new QComboBox;
|
|
947 |
cb->setStyleSheet("* { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop:0 #e8ff66, stop:1 #000000); }");
|
|
948 |
layout->addWidget(cb);
|
|
949 |
widgets[3]->setLayout(layout);
|
|
950 |
|
|
951 |
for (int c = 0; c < number; ++c) {
|
|
952 |
QWidget* widget = widgets[c];
|
|
953 |
|
|
954 |
widget->show();
|
|
955 |
#ifdef Q_WS_X11
|
|
956 |
qt_x11_wait_for_window_manager(widget);
|
|
957 |
#endif
|
|
958 |
|
|
959 |
QImage image(widget->width(), widget->height(), QImage::Format_ARGB32);
|
|
960 |
widget->render(&image);
|
|
961 |
if (image.depth() < 24) {
|
|
962 |
QSKIP("Test doesn't support color depth < 24", SkipAll);
|
|
963 |
}
|
|
964 |
|
|
965 |
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
|
|
966 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
967 |
+ " did not contain background image with color #e8ff66")
|
|
968 |
.toLocal8Bit().constData());
|
|
969 |
|
|
970 |
delete widget;
|
|
971 |
}
|
|
972 |
}
|
|
973 |
|
|
974 |
void tst_QStyleSheetStyle::tabAlignement()
|
|
975 |
{
|
|
976 |
QTabWidget tabWidget;
|
|
977 |
tabWidget.addTab(new QLabel("tab1"),"tab1");
|
|
978 |
tabWidget.resize(QSize(400,400));
|
|
979 |
tabWidget.show();
|
|
980 |
QTest::qWaitForWindowShown(&tabWidget);
|
|
981 |
QTest::qWait(50);
|
|
982 |
QTabBar *bar = qFindChild<QTabBar*>(&tabWidget);
|
|
983 |
QVERIFY(bar);
|
|
984 |
//check the tab is on the right
|
|
985 |
tabWidget.setStyleSheet("QTabWidget::tab-bar { alignment: right ; }");
|
|
986 |
qApp->processEvents();
|
|
987 |
QVERIFY(bar->geometry().right() > 380);
|
|
988 |
QVERIFY(bar->geometry().left() > 200);
|
|
989 |
//check the tab is on the middle
|
|
990 |
tabWidget.setStyleSheet("QTabWidget::tab-bar { alignment: center ; }");
|
|
991 |
QVERIFY(bar->geometry().right() < 300);
|
|
992 |
QVERIFY(bar->geometry().left() > 100);
|
|
993 |
//check the tab is on the left
|
|
994 |
tabWidget.setStyleSheet("QTabWidget::tab-bar { alignment: left ; }");
|
|
995 |
QVERIFY(bar->geometry().left() < 20);
|
|
996 |
QVERIFY(bar->geometry().right() < 200);
|
|
997 |
|
|
998 |
tabWidget.setTabPosition(QTabWidget::West);
|
|
999 |
//check the tab is on the top
|
|
1000 |
QVERIFY(bar->geometry().top() < 20);
|
|
1001 |
QVERIFY(bar->geometry().bottom() < 200);
|
|
1002 |
//check the tab is on the bottom
|
|
1003 |
tabWidget.setStyleSheet("QTabWidget::tab-bar { alignment: right ; }");
|
|
1004 |
QVERIFY(bar->geometry().bottom() > 380);
|
|
1005 |
QVERIFY(bar->geometry().top() > 200);
|
|
1006 |
//check the tab is on the middle
|
|
1007 |
tabWidget.setStyleSheet("QTabWidget::tab-bar { alignment: center ; }");
|
|
1008 |
QVERIFY(bar->geometry().bottom() < 300);
|
|
1009 |
QVERIFY(bar->geometry().top() > 100);
|
|
1010 |
}
|
|
1011 |
|
|
1012 |
void tst_QStyleSheetStyle::attributesList()
|
|
1013 |
{
|
|
1014 |
QWidget w;
|
|
1015 |
QPushButton *p1=new QPushButton(&w);
|
|
1016 |
QPushButton *p2=new QPushButton(&w);
|
|
1017 |
QPushButton *p3=new QPushButton(&w);
|
|
1018 |
QPushButton *p4=new QPushButton(&w);
|
|
1019 |
p1->setProperty("prop", QStringList() << "red");
|
|
1020 |
p2->setProperty("prop", QStringList() << "foo" << "red");
|
|
1021 |
p3->setProperty("prop", QStringList() << "foo" << "bar");
|
|
1022 |
|
|
1023 |
w.setStyleSheet(" QPushButton{ background-color:blue; } QPushButton[prop~=red] { background-color:red; }");
|
|
1024 |
QCOMPARE(BACKGROUND(*p1) , QColor("red"));
|
|
1025 |
QCOMPARE(BACKGROUND(*p2) , QColor("red"));
|
|
1026 |
QCOMPARE(BACKGROUND(*p3) , QColor("blue"));
|
|
1027 |
QCOMPARE(BACKGROUND(*p4) , QColor("blue"));
|
|
1028 |
}
|
|
1029 |
|
|
1030 |
void tst_QStyleSheetStyle::minmaxSizes()
|
|
1031 |
{
|
|
1032 |
QTabWidget tabWidget;
|
|
1033 |
tabWidget.setObjectName("tabWidget");
|
|
1034 |
int index1 = tabWidget.addTab(new QLabel("Tab1"),"a");
|
|
1035 |
|
|
1036 |
QWidget *page2=new QLabel("page2");
|
|
1037 |
page2->setObjectName("page2");
|
|
1038 |
page2->setStyleSheet("* {background-color: white; min-width: 250px; max-width:500px }");
|
|
1039 |
tabWidget.addTab(page2,"Tab2");
|
|
1040 |
QWidget *page3=new QLabel("plop");
|
|
1041 |
page3->setObjectName("Page3");
|
|
1042 |
page3->setStyleSheet("* {background-color: yellow; min-height: 250px; max-height:500px }");
|
|
1043 |
int index3 = tabWidget.addTab(page3,"very_long_long_long_long_caption");
|
|
1044 |
|
|
1045 |
tabWidget.setStyleSheet("QTabBar::tab { min-width:100px; max-width:130px; }");
|
|
1046 |
|
|
1047 |
tabWidget.show();
|
|
1048 |
QTest::qWait(50);
|
|
1049 |
//i allow 4px additional border from the native style (hence the -2, <=2)
|
|
1050 |
QVERIFY(qAbs(page2->maximumSize().width() - 500 - 2) <= 2);
|
|
1051 |
QVERIFY(qAbs(page2->minimumSize().width() - 250 - 2) <= 2);
|
|
1052 |
QVERIFY(qAbs(page3->maximumSize().height() - 500 - 2) <= 2);
|
|
1053 |
QVERIFY(qAbs(page3->minimumSize().height() - 250 - 2) <= 2);
|
|
1054 |
QVERIFY(qAbs(page3->minimumSize().height() - 250 - 2) <= 2);
|
|
1055 |
QTabBar *bar = qFindChild<QTabBar*>(&tabWidget);
|
|
1056 |
QVERIFY(bar);
|
|
1057 |
QVERIFY(qAbs(bar->tabRect(index1).width() - 100 - 2) <= 2);
|
|
1058 |
QVERIFY(qAbs(bar->tabRect(index3).width() - 130 - 2) <= 2);
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
void tst_QStyleSheetStyle::task206238_twice()
|
|
1062 |
{
|
|
1063 |
QMainWindow w;
|
|
1064 |
QTabWidget* tw = new QTabWidget;
|
|
1065 |
tw->addTab(new QLabel("foo"), "test");
|
|
1066 |
w.setCentralWidget(tw);
|
|
1067 |
w.setStyleSheet("background: red;");
|
|
1068 |
w.show();
|
|
1069 |
QTest::qWait(20);
|
|
1070 |
QCOMPARE(BACKGROUND(w) , QColor("red"));
|
|
1071 |
QCOMPARE(BACKGROUND(*tw), QColor("red"));
|
|
1072 |
w.setStyleSheet("background: red;");
|
|
1073 |
QTest::qWait(20);
|
|
1074 |
QCOMPARE(BACKGROUND(w) , QColor("red"));
|
|
1075 |
QCOMPARE(BACKGROUND(*tw), QColor("red"));
|
|
1076 |
}
|
|
1077 |
|
|
1078 |
void tst_QStyleSheetStyle::transparent()
|
|
1079 |
{
|
|
1080 |
QWidget w;
|
|
1081 |
QPushButton *p1=new QPushButton(&w);
|
|
1082 |
QPushButton *p2=new QPushButton(&w);
|
|
1083 |
QPushButton *p3=new QPushButton(&w);
|
|
1084 |
p1->setStyleSheet("background:transparent");
|
|
1085 |
p2->setStyleSheet("background-color:transparent");
|
|
1086 |
p3->setStyleSheet("background:rgb(0,0,0,0)");
|
|
1087 |
QCOMPARE(BACKGROUND(*p1) , QColor(0,0,0,0));
|
|
1088 |
QCOMPARE(BACKGROUND(*p2) , QColor(0,0,0,0));
|
|
1089 |
QCOMPARE(BACKGROUND(*p3) , QColor(0,0,0,0));
|
|
1090 |
}
|
|
1091 |
|
|
1092 |
|
|
1093 |
|
|
1094 |
class ProxyStyle : public QStyle
|
|
1095 |
{
|
|
1096 |
public:
|
|
1097 |
ProxyStyle(QStyle *s)
|
|
1098 |
{
|
|
1099 |
style = s;
|
|
1100 |
}
|
|
1101 |
|
|
1102 |
void drawControl(ControlElement ce, const QStyleOption *opt,
|
|
1103 |
QPainter *painter, const QWidget *widget = 0) const;
|
|
1104 |
|
|
1105 |
void drawPrimitive(QStyle::PrimitiveElement pe,
|
|
1106 |
const QStyleOption* opt,
|
|
1107 |
QPainter* p ,
|
|
1108 |
const QWidget* w) const
|
|
1109 |
{
|
|
1110 |
style->drawPrimitive(pe, opt, p, w);
|
|
1111 |
}
|
|
1112 |
|
|
1113 |
QRect subElementRect(QStyle::SubElement se,
|
|
1114 |
const QStyleOption* opt,
|
|
1115 |
const QWidget* w) const
|
|
1116 |
{
|
|
1117 |
return style->subElementRect(se, opt, w);
|
|
1118 |
}
|
|
1119 |
|
|
1120 |
void drawComplexControl(QStyle::ComplexControl cc,
|
|
1121 |
const QStyleOptionComplex* opt,
|
|
1122 |
QPainter* p,
|
|
1123 |
const QWidget* w) const
|
|
1124 |
{
|
|
1125 |
style->drawComplexControl(cc, opt, p, w);
|
|
1126 |
}
|
|
1127 |
|
|
1128 |
|
|
1129 |
SubControl hitTestComplexControl(QStyle::ComplexControl cc,
|
|
1130 |
const QStyleOptionComplex* opt,
|
|
1131 |
const QPoint& pt,
|
|
1132 |
const QWidget* w) const
|
|
1133 |
{
|
|
1134 |
return style->hitTestComplexControl(cc, opt, pt, w);
|
|
1135 |
}
|
|
1136 |
|
|
1137 |
QRect subControlRect(QStyle::ComplexControl cc,
|
|
1138 |
const QStyleOptionComplex* opt,
|
|
1139 |
QStyle::SubControl sc,
|
|
1140 |
const QWidget* w) const
|
|
1141 |
{
|
|
1142 |
return style->subControlRect(cc, opt, sc, w);
|
|
1143 |
}
|
|
1144 |
|
|
1145 |
int pixelMetric(QStyle::PixelMetric pm,
|
|
1146 |
const QStyleOption* opt,
|
|
1147 |
const QWidget* w) const
|
|
1148 |
{
|
|
1149 |
return style->pixelMetric(pm, opt, w);
|
|
1150 |
}
|
|
1151 |
|
|
1152 |
|
|
1153 |
QSize sizeFromContents(QStyle::ContentsType ct,
|
|
1154 |
const QStyleOption* opt,
|
|
1155 |
const QSize& size,
|
|
1156 |
const QWidget* w) const
|
|
1157 |
{
|
|
1158 |
return style->sizeFromContents(ct, opt, size, w);
|
|
1159 |
}
|
|
1160 |
|
|
1161 |
int styleHint(QStyle::StyleHint sh,
|
|
1162 |
const QStyleOption* opt,
|
|
1163 |
const QWidget* w,
|
|
1164 |
QStyleHintReturn* shr) const
|
|
1165 |
{
|
|
1166 |
return style->styleHint(sh, opt, w, shr);
|
|
1167 |
}
|
|
1168 |
|
|
1169 |
QPixmap standardPixmap(QStyle::StandardPixmap spix,
|
|
1170 |
const QStyleOption* opt,
|
|
1171 |
const QWidget* w) const
|
|
1172 |
{
|
|
1173 |
return style->standardPixmap(spix, opt, w);
|
|
1174 |
}
|
|
1175 |
|
|
1176 |
QPixmap generatedIconPixmap(QIcon::Mode mode,
|
|
1177 |
const QPixmap& pix,
|
|
1178 |
const QStyleOption* opt) const
|
|
1179 |
{
|
|
1180 |
return style->generatedIconPixmap(mode, pix, opt);
|
|
1181 |
}
|
|
1182 |
|
|
1183 |
private:
|
|
1184 |
QStyle *style;
|
|
1185 |
};
|
|
1186 |
|
|
1187 |
void ProxyStyle::drawControl(ControlElement ce, const QStyleOption *opt,
|
|
1188 |
QPainter *painter, const QWidget *widget) const
|
|
1189 |
{
|
|
1190 |
if(ce == CE_PushButton)
|
|
1191 |
{
|
|
1192 |
if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt))
|
|
1193 |
{
|
|
1194 |
QRect r = btn->rect;
|
|
1195 |
painter->fillRect(r, Qt::green);
|
|
1196 |
|
|
1197 |
if(btn->state & QStyle::State_HasFocus)
|
|
1198 |
painter->fillRect(r.adjusted(5, 5, -5, -5), Qt::yellow);
|
|
1199 |
|
|
1200 |
|
|
1201 |
painter->drawText(r, Qt::AlignCenter, btn->text);
|
|
1202 |
}
|
|
1203 |
}
|
|
1204 |
else
|
|
1205 |
{
|
|
1206 |
style->drawControl(ce, opt, painter, widget);
|
|
1207 |
}
|
|
1208 |
}
|
|
1209 |
|
|
1210 |
void tst_QStyleSheetStyle::proxyStyle()
|
|
1211 |
{
|
|
1212 |
//Should not crash; task 158984
|
|
1213 |
|
|
1214 |
ProxyStyle *proxy = new ProxyStyle(qApp->style());
|
|
1215 |
QString styleSheet("QPushButton {background-color: red; }");
|
|
1216 |
|
|
1217 |
QWidget *w = new QWidget;
|
|
1218 |
QVBoxLayout *layout = new QVBoxLayout(w);
|
|
1219 |
|
|
1220 |
QPushButton *pb1 = new QPushButton(qApp->style()->objectName(), w);
|
|
1221 |
layout->addWidget(pb1);
|
|
1222 |
|
|
1223 |
QPushButton *pb2 = new QPushButton("ProxyStyle", w);
|
|
1224 |
pb2->setStyle(proxy);
|
|
1225 |
layout->addWidget(pb2);
|
|
1226 |
|
|
1227 |
QPushButton *pb3 = new QPushButton("StyleSheet", w);
|
|
1228 |
pb3->setStyleSheet(styleSheet);
|
|
1229 |
layout->addWidget(pb3);
|
|
1230 |
|
|
1231 |
QPushButton *pb4 = new QPushButton("StyleSheet then ProxyStyle ", w);
|
|
1232 |
pb4->setStyleSheet(styleSheet);
|
|
1233 |
|
|
1234 |
// We are creating our Proxy based on current style...
|
|
1235 |
// In this case it would be the QStyleSheetStyle that is delete
|
|
1236 |
// later on. We need to get access to the "real" QStyle to be able to
|
|
1237 |
// draw correctly.
|
|
1238 |
ProxyStyle* newProxy = new ProxyStyle(qApp->style());
|
|
1239 |
pb4->setStyle(newProxy);
|
|
1240 |
|
|
1241 |
layout->addWidget(pb4);
|
|
1242 |
|
|
1243 |
QPushButton *pb5 = new QPushButton("ProxyStyle then StyleSheet ", w);
|
|
1244 |
pb5->setStyle(proxy);
|
|
1245 |
pb5->setStyleSheet(styleSheet);
|
|
1246 |
layout->addWidget(pb5);
|
|
1247 |
|
|
1248 |
w->show();
|
|
1249 |
|
|
1250 |
QTest::qWait(100);
|
|
1251 |
delete w;
|
|
1252 |
delete proxy;
|
|
1253 |
delete newProxy;
|
|
1254 |
}
|
|
1255 |
|
|
1256 |
void tst_QStyleSheetStyle::dialogButtonBox()
|
|
1257 |
{
|
|
1258 |
QDialogButtonBox box;
|
|
1259 |
box.addButton(QDialogButtonBox::Ok);
|
|
1260 |
box.addButton(QDialogButtonBox::Cancel);
|
|
1261 |
box.setStyleSheet("/** */ ");
|
|
1262 |
box.setStyleSheet(QString()); //should not crash
|
|
1263 |
}
|
|
1264 |
|
|
1265 |
void tst_QStyleSheetStyle::emptyStyleSheet()
|
|
1266 |
{
|
|
1267 |
//empty stylesheet should not change anything
|
|
1268 |
qApp->setStyleSheet(QString());
|
|
1269 |
QWidget w;
|
|
1270 |
QHBoxLayout layout(&w);
|
|
1271 |
w.setLayout(&layout);
|
|
1272 |
#ifdef QT3_SUPPORT
|
|
1273 |
layout.addWidget(new Q3ProgressBar(&w)); //task 231137
|
|
1274 |
#endif
|
|
1275 |
layout.addWidget(new QPushButton("push", &w));
|
|
1276 |
layout.addWidget(new QToolButton(&w));
|
|
1277 |
QLabel label("toto", &w);
|
|
1278 |
label.setFrameShape(QLabel::Panel);
|
|
1279 |
label.setFrameShadow(QLabel::Sunken);
|
|
1280 |
layout.addWidget(&label); //task 231137
|
|
1281 |
layout.addWidget(new QTableWidget(200,200, &w));
|
|
1282 |
layout.addWidget(new QProgressBar(&w));
|
|
1283 |
layout.addWidget(new QLineEdit(&w));
|
|
1284 |
layout.addWidget(new QSpinBox(&w));
|
|
1285 |
layout.addWidget(new QComboBox(&w));
|
|
1286 |
layout.addWidget(new QDateEdit(&w));
|
|
1287 |
layout.addWidget(new QGroupBox("some text", &w));
|
|
1288 |
|
|
1289 |
w.show();
|
|
1290 |
#ifdef Q_WS_X11
|
|
1291 |
qt_x11_wait_for_window_manager(&w);
|
|
1292 |
#endif
|
|
1293 |
//workaround the fact that the label sizehint is one pixel different the first time.
|
|
1294 |
label.setIndent(0); //force to recompute the sizeHint:
|
|
1295 |
w.setFocus();
|
|
1296 |
QTest::qWait(100);
|
|
1297 |
|
|
1298 |
QImage img1(w.size(), QImage::Format_ARGB32);
|
|
1299 |
w.render(&img1);
|
|
1300 |
|
|
1301 |
w.setStyleSheet("/* */");
|
|
1302 |
QTest::qWait(100);
|
|
1303 |
|
|
1304 |
QImage img2(w.size(), QImage::Format_ARGB32);
|
|
1305 |
w.render(&img2);
|
|
1306 |
|
|
1307 |
if(img1 != img2) {
|
|
1308 |
img1.save("emptyStyleSheet_img1.png");
|
|
1309 |
img2.save("emptyStyleSheet_img2.png");
|
|
1310 |
}
|
|
1311 |
|
|
1312 |
QCOMPARE(img1,img2);
|
|
1313 |
}
|
|
1314 |
|
|
1315 |
void tst_QStyleSheetStyle::toolTip()
|
|
1316 |
{
|
|
1317 |
qApp->setStyleSheet(QString());
|
|
1318 |
QWidget w;
|
|
1319 |
QHBoxLayout layout(&w);
|
|
1320 |
w.setLayout(&layout);
|
|
1321 |
|
|
1322 |
QWidget *wid1 = new QGroupBox(&w);
|
|
1323 |
layout.addWidget(wid1);
|
|
1324 |
wid1->setStyleSheet("QToolTip { background: #ae2; } #wid3 > QToolTip { background: #0b8; } ");
|
|
1325 |
QVBoxLayout *layout1 = new QVBoxLayout(wid1);
|
|
1326 |
wid1->setLayout(layout1);
|
|
1327 |
wid1->setToolTip("this is wid1");
|
|
1328 |
wid1->setObjectName("wid1");
|
|
1329 |
|
|
1330 |
QWidget *wid2 = new QPushButton("wid2", wid1);
|
|
1331 |
layout1->addWidget(wid2);
|
|
1332 |
wid2->setStyleSheet("QToolTip { background: #f81; } ");
|
|
1333 |
wid2->setToolTip("this is wid2");
|
|
1334 |
wid2->setObjectName("wid2");
|
|
1335 |
|
|
1336 |
QWidget *wid3 = new QPushButton("wid3", wid1);
|
|
1337 |
layout1->addWidget(wid3);
|
|
1338 |
wid3->setToolTip("this is wid3");
|
|
1339 |
wid3->setObjectName("wid3");
|
|
1340 |
|
|
1341 |
QWidget *wid4 = new QPushButton("wid4", &w);
|
|
1342 |
layout.addWidget(wid4);
|
|
1343 |
wid4->setToolTip("this is wid4");
|
|
1344 |
wid4->setObjectName("wid4");
|
|
1345 |
|
|
1346 |
w.show();
|
|
1347 |
QTest::qWait(100);
|
|
1348 |
|
|
1349 |
QColor normalToolTip = qApp->palette().toolTipBase().color();
|
|
1350 |
QList<QWidget *> widgets;
|
|
1351 |
QList<QColor> colors;
|
|
1352 |
|
|
1353 |
|
|
1354 |
//tooltip on the widget without stylesheet, then to othes widget, including one without stylesheet
|
|
1355 |
//(the tooltip will be reused but his colour must change)
|
|
1356 |
widgets << wid4 << wid1 << wid2 << wid3 << wid4;
|
|
1357 |
colors << normalToolTip << "#ae2" << "#f81" << "#0b8" << normalToolTip;
|
|
1358 |
|
|
1359 |
for (int i = 0; i < widgets.count() ; i++)
|
|
1360 |
{
|
|
1361 |
QWidget *wid = widgets.at(i);
|
|
1362 |
QColor col = colors.at(i);
|
|
1363 |
|
|
1364 |
QToolTip::showText( QPoint(0,0) , "This is " + wid->objectName(), wid);
|
|
1365 |
|
|
1366 |
QWidget *tooltip = 0;
|
|
1367 |
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
|
|
1368 |
if (widget->inherits("QTipLabel") && widget->isVisible()) {
|
|
1369 |
tooltip = widget;
|
|
1370 |
break;
|
|
1371 |
}
|
|
1372 |
}
|
|
1373 |
QVERIFY(tooltip);
|
|
1374 |
QCOMPARE(tooltip->palette().color(tooltip->backgroundRole()), col);
|
|
1375 |
}
|
|
1376 |
|
|
1377 |
QToolTip::showText( QPoint(0,0) , "This is " + wid3->objectName(), wid3);
|
|
1378 |
QTest::qWait(100);
|
|
1379 |
delete wid3; //should not crash;
|
|
1380 |
QTest::qWait(10);
|
|
1381 |
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
|
|
1382 |
widget->update(); //should not crash either
|
|
1383 |
}
|
|
1384 |
}
|
|
1385 |
|
|
1386 |
void tst_QStyleSheetStyle::embeddedFonts()
|
|
1387 |
{
|
|
1388 |
//task 235622 and 210551
|
|
1389 |
QSpinBox spin;
|
|
1390 |
spin.show();
|
|
1391 |
spin.setStyleSheet("QSpinBox { font-size: 32px; }");
|
|
1392 |
QTest::qWait(20);
|
|
1393 |
QLineEdit *embedded = spin.findChild<QLineEdit *>();
|
|
1394 |
QVERIFY(embedded);
|
|
1395 |
QCOMPARE(spin.font().pixelSize(), 32);
|
|
1396 |
QCOMPARE(embedded->font().pixelSize(), 32);
|
|
1397 |
|
|
1398 |
QMenu *menu = embedded->createStandardContextMenu();
|
|
1399 |
menu->show();
|
|
1400 |
QTest::qWait(20);
|
|
1401 |
QVERIFY(menu);
|
|
1402 |
QVERIFY(menu->font().pixelSize() != 32);
|
|
1403 |
QCOMPARE(menu->font().pixelSize(), qApp->font(menu).pixelSize());
|
|
1404 |
|
|
1405 |
//task 242556
|
|
1406 |
QComboBox box;
|
|
1407 |
box.setEditable(true);
|
|
1408 |
box.addItems(QStringList() << "First" << "Second" << "Third");
|
|
1409 |
box.setStyleSheet("QComboBox { font-size: 32px; }");
|
|
1410 |
box.show();
|
|
1411 |
embedded = box.findChild<QLineEdit *>();
|
|
1412 |
QVERIFY(embedded);
|
|
1413 |
QTest::qWait(20);
|
|
1414 |
QCOMPARE(box.font().pixelSize(), 32);
|
|
1415 |
QCOMPARE(embedded->font().pixelSize(), 32);
|
|
1416 |
}
|
|
1417 |
|
|
1418 |
void tst_QStyleSheetStyle::opaquePaintEvent_data()
|
|
1419 |
{
|
|
1420 |
QTest::addColumn<QString>("stylesheet");
|
|
1421 |
QTest::addColumn<bool>("transparent");
|
|
1422 |
QTest::addColumn<bool>("styled");
|
|
1423 |
|
|
1424 |
QTest::newRow("none") << QString::fromLatin1("/* */") << false << false;
|
|
1425 |
QTest::newRow("background black ") << QString::fromLatin1("background: black;") << false << true;
|
|
1426 |
QTest::newRow("background qrgba") << QString::fromLatin1("background: rgba(125,0,0,125);") << true << true;
|
|
1427 |
QTest::newRow("background transparent") << QString::fromLatin1("background: transparent;") << true << true;
|
|
1428 |
QTest::newRow("border native") << QString::fromLatin1("border: native;") << false << false;
|
|
1429 |
QTest::newRow("border solid") << QString::fromLatin1("border: 2px solid black;") << true << true;
|
|
1430 |
QTest::newRow("border transparent") << QString::fromLatin1("background: black; border: 2px solid rgba(125,0,0,125);") << true << true;
|
|
1431 |
QTest::newRow("margin") << QString::fromLatin1("margin: 25px;") << true << true;
|
|
1432 |
QTest::newRow("focus") << QString::fromLatin1("*:focus { background: rgba(125,0,0,125) }") << true << true;
|
|
1433 |
}
|
|
1434 |
|
|
1435 |
void tst_QStyleSheetStyle::opaquePaintEvent()
|
|
1436 |
{
|
|
1437 |
QFETCH(QString, stylesheet);
|
|
1438 |
QFETCH(bool, transparent);
|
|
1439 |
QFETCH(bool, styled);
|
|
1440 |
|
|
1441 |
QWidget tl;
|
|
1442 |
QWidget cl(&tl);
|
|
1443 |
cl.setAttribute(Qt::WA_OpaquePaintEvent, true);
|
|
1444 |
cl.setAutoFillBackground(true);
|
|
1445 |
cl.setStyleSheet(stylesheet);
|
|
1446 |
cl.ensurePolished();
|
|
1447 |
QCOMPARE(cl.testAttribute(Qt::WA_OpaquePaintEvent), !transparent);
|
|
1448 |
QCOMPARE(cl.testAttribute(Qt::WA_StyledBackground), styled);
|
|
1449 |
QCOMPARE(cl.autoFillBackground(), !styled );
|
|
1450 |
}
|
|
1451 |
|
|
1452 |
void tst_QStyleSheetStyle::complexWidgetFocus()
|
|
1453 |
{
|
|
1454 |
// This test is a simplified version of the focusColors() test above.
|
|
1455 |
|
|
1456 |
// Tests if colors can be changed by altering the focus of the widget.
|
|
1457 |
// To avoid messy pixel-by-pixel comparison, we assume that the goal
|
|
1458 |
// is reached if at least ten pixels of the right color can be found in
|
|
1459 |
// the image.
|
|
1460 |
// For this reason, we use unusual and extremely ugly colors! :-)
|
|
1461 |
|
|
1462 |
QDialog frame;
|
|
1463 |
frame.setStyleSheet("*:focus { background: black; color: black } "
|
|
1464 |
"QSpinBox::up-arrow:focus, QSpinBox::down-arrow:focus { width: 7px; height: 7px; background: #ff0084 } "
|
|
1465 |
"QComboBox::down-arrow:focus { width: 7px; height: 7px; background: #ff0084 }"
|
|
1466 |
"QSlider::handle:horizontal:focus { width: 7px; height: 7px; background: #ff0084 } ");
|
|
1467 |
|
|
1468 |
QList<QWidget *> widgets;
|
|
1469 |
widgets << new QSpinBox;
|
|
1470 |
widgets << new QComboBox;
|
|
1471 |
widgets << new QSlider(Qt::Horizontal);
|
|
1472 |
|
|
1473 |
QLayout* layout = new QGridLayout;
|
|
1474 |
layout->addWidget(new QLineEdit); // Avoids initial focus.
|
|
1475 |
foreach (QWidget *widget, widgets)
|
|
1476 |
layout->addWidget(widget);
|
|
1477 |
frame.setLayout(layout);
|
|
1478 |
|
|
1479 |
frame.show();
|
|
1480 |
QTest::qWaitForWindowShown(&frame);
|
|
1481 |
QApplication::setActiveWindow(&frame);
|
|
1482 |
foreach (QWidget *widget, widgets) {
|
|
1483 |
widget->setFocus();
|
|
1484 |
QApplication::processEvents();
|
|
1485 |
|
|
1486 |
QImage image(frame.width(), frame.height(), QImage::Format_ARGB32);
|
|
1487 |
frame.render(&image);
|
|
1488 |
if (image.depth() < 24) {
|
|
1489 |
QSKIP("Test doesn't support color depth < 24", SkipAll);
|
|
1490 |
}
|
|
1491 |
|
|
1492 |
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
|
|
1493 |
(QString::fromLatin1(widget->metaObject()->className())
|
|
1494 |
+ " did not contain text color #ff0084, using style "
|
|
1495 |
+ QString::fromLatin1(qApp->style()->metaObject()->className()))
|
|
1496 |
.toLocal8Bit().constData());
|
|
1497 |
}
|
|
1498 |
}
|
|
1499 |
|
|
1500 |
void tst_QStyleSheetStyle::task188195_baseBackground()
|
|
1501 |
{
|
|
1502 |
QTreeView tree;
|
|
1503 |
tree.setStyleSheet( "QTreeView:disabled { background-color:#ab1251; }" );
|
|
1504 |
tree.show();
|
|
1505 |
QTest::qWait(20);
|
|
1506 |
QImage image(tree.width(), tree.height(), QImage::Format_ARGB32);
|
|
1507 |
|
|
1508 |
tree.render(&image);
|
|
1509 |
QVERIFY(testForColors(image, tree.palette().base().color()));
|
|
1510 |
QVERIFY(!testForColors(image, QColor(0xab, 0x12, 0x51)));
|
|
1511 |
|
|
1512 |
tree.setEnabled(false);
|
|
1513 |
tree.render(&image);
|
|
1514 |
QVERIFY(testForColors(image, QColor(0xab, 0x12, 0x51)));
|
|
1515 |
|
|
1516 |
tree.setEnabled(true);
|
|
1517 |
tree.render(&image);
|
|
1518 |
QVERIFY(testForColors(image, tree.palette().base().color()));
|
|
1519 |
QVERIFY(!testForColors(image, QColor(0xab, 0x12, 0x51)));
|
|
1520 |
}
|
|
1521 |
|
|
1522 |
void tst_QStyleSheetStyle::task232085_spinBoxLineEditBg()
|
|
1523 |
{
|
|
1524 |
// This test is a simplified version of the focusColors() test above.
|
|
1525 |
|
|
1526 |
// Tests if colors can be changed by altering the focus of the widget.
|
|
1527 |
// To avoid messy pixel-by-pixel comparison, we assume that the goal
|
|
1528 |
// is reached if at least ten pixels of the right color can be found in
|
|
1529 |
// the image.
|
|
1530 |
// For this reason, we use unusual and extremely ugly colors! :-)
|
|
1531 |
|
|
1532 |
QSpinBox *spinbox = new QSpinBox;
|
|
1533 |
spinbox->setValue(8888);
|
|
1534 |
|
|
1535 |
QDialog frame;
|
|
1536 |
QLayout* layout = new QGridLayout;
|
|
1537 |
|
|
1538 |
QLineEdit* dummy = new QLineEdit; // Avoids initial focus.
|
|
1539 |
|
|
1540 |
// We only want to test the line edit colors.
|
|
1541 |
spinbox->setStyleSheet("QSpinBox:focus { background: #e8ff66; color: #ff0084 } "
|
|
1542 |
"QSpinBox::up-button, QSpinBox::down-button { background: black; }");
|
|
1543 |
|
|
1544 |
layout->addWidget(dummy);
|
|
1545 |
layout->addWidget(spinbox);
|
|
1546 |
frame.setLayout(layout);
|
|
1547 |
|
|
1548 |
frame.show();
|
|
1549 |
QTest::qWaitForWindowShown(&frame);
|
|
1550 |
QApplication::setActiveWindow(&frame);
|
|
1551 |
spinbox->setFocus();
|
|
1552 |
QApplication::processEvents();
|
|
1553 |
|
|
1554 |
QImage image(frame.width(), frame.height(), QImage::Format_ARGB32);
|
|
1555 |
frame.render(&image);
|
|
1556 |
if (image.depth() < 24) {
|
|
1557 |
QSKIP("Test doesn't support color depth < 24", SkipAll);
|
|
1558 |
}
|
|
1559 |
|
|
1560 |
QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)),
|
|
1561 |
(QString::fromLatin1(spinbox->metaObject()->className())
|
|
1562 |
+ " did not contain background color #e8ff66, using style "
|
|
1563 |
+ QString::fromLatin1(qApp->style()->metaObject()->className()))
|
|
1564 |
.toLocal8Bit().constData());
|
|
1565 |
QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)),
|
|
1566 |
(QString::fromLatin1(spinbox->metaObject()->className())
|
|
1567 |
+ " did not contain text color #ff0084, using style "
|
|
1568 |
+ QString::fromLatin1(qApp->style()->metaObject()->className()))
|
|
1569 |
.toLocal8Bit().constData());
|
|
1570 |
}
|
|
1571 |
|
|
1572 |
QTEST_MAIN(tst_QStyleSheetStyle)
|
|
1573 |
#include "tst_qstylesheetstyle.moc"
|
|
1574 |
|