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