source: trunk/doc/src/snippets/code/doc_src_styles.qdoc@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 3.0 KB
Line 
1//! [0]
2 opt.init(q);
3 if (down)
4 opt.state |= QStyle::State_Sunken;
5 if (tristate && noChange)
6 opt.state |= QStyle::State_NoChange;
7 else
8 opt.state |= checked ? QStyle::State_On :
9 QStyle::State_Off;
10 if (q->testAttribute(Qt::WA_Hover) && q->underMouse()) {
11 if (hovering)
12 opt.state |= QStyle::State_MouseOver;
13 else
14 opt.state &= ~QStyle::State_MouseOver;
15 }
16 opt.text = text;
17 opt.icon = icon;
18 opt.iconSize = q->iconSize();
19//! [0]
20
21
22//! [1]
23 state = QStyle::State_None;
24 if (widget->isEnabled())
25 state |= QStyle::State_Enabled;
26 if (widget->hasFocus())
27 state |= QStyle::State_HasFocus;
28 if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange))
29 state |= QStyle::State_KeyboardFocusChange;
30 if (widget->underMouse())
31 state |= QStyle::State_MouseOver;
32 if (widget->window()->isActiveWindow())
33 state |= QStyle::State_Active;
34#ifdef Q_WS_MAC
35 extern bool qt_mac_can_clickThrough(const QWidget *w); //qwidget_mac.cpp
36 if (!(state & QStyle::State_Active) && !qt_mac_can_clickThrough(widget))
37 state &= ~QStyle::State_Enabled;
38#endif
39#ifdef QT_KEYPAD_NAVIGATION
40 if (widget->hasEditFocus())
41 state |= QStyle::State_HasEditFocus;
42#endif
43
44 direction = widget->layoutDirection();
45 rect = widget->rect();
46 palette = widget->palette();
47 fontMetrics = widget->fontMetrics();
48//! [1]
49
50
51//! [2]
52 QStylePainter p(this);
53 QStyleOptionButton opt = d->getStyleOption();
54 p.drawControl(QStyle::CE_CheckBox, opt);
55//! [2]
56
57
58//! [3]
59 QStyleOptionButton subopt = *btn;
60 subopt.rect = subElementRect(SE_CheckBoxIndicator, btn, widget);
61 drawPrimitive(PE_IndicatorCheckBox, &subopt, p, widget);
62 subopt.rect = subElementRect(SE_CheckBoxContents, btn, widget);
63 drawControl(CE_CheckBoxLabel, &subopt, p, widget);
64
65 if (btn->state & State_HasFocus) {
66 QStyleOptionFocusRect fropt;
67 fropt.QStyleOption::operator=(*btn);
68 fropt.rect = subElementRect(SE_CheckBoxFocusRect, btn, widget);
69 drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
70 }
71//! [3]
72
73
74//! [4]
75 const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt);
76 uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);
77
78 if (!styleHint(SH_UnderlineShortcut, btn, widget))
79 alignment |= Qt::TextHideMnemonic;
80 QPixmap pix;
81 QRect textRect = btn->rect;
82 if (!btn->icon.isNull()) {
83 pix = btn->icon.pixmap(btn->iconSize, btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
84 drawItemPixmap(p, btn->rect, alignment, pix);
85 if (btn->direction == Qt::RightToLeft)
86 textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
87 else
88 textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
89 }
90 if (!btn->text.isEmpty()){
91 drawItemText(p, textRect, alignment | Qt::TextShowMnemonic,
92 btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
93 }
94//! [4]
Note: See TracBrowser for help on using the repository browser.