📄 qpalette.cpp
字号:
\img palette.png Color Roles The ColorRole enum defines the different symbolic color roles used in current GUIs. The central roles are: \value Window A general background color. \value Background This value is obsolete. Use Window instead. \value WindowText A general foreground color. \value Foreground This value is obsolete. Use WindowText instead. \value Base Used mostly as the background color for text entry widgets, but can also be used for other painting - such as the background of combobox drop down lists and toolbar handles. It is usually white or another light color. \value AlternateBase Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()). \value Text The foreground color used with \c Base. This is usually the same as the \c WindowText, in which case it must provide good contrast with \c Window and \c Base. \value Button The general button background color. This background can be different from \c Window as some styles require a different background color for buttons. \value ButtonText A foreground color used with the \c Button color. \value BrightText A text color that is very different from \c WindowText, and contrasts well with e.g. \c Dark. Typically used for text that needs to be drawn where \c Text or \c WindowText would give poor contrast, such as on pressed push buttons. Note that text colors can be used for things other than just words; text colors are \e usually used for text, but it's quite common to use the text color roles for lines, icons, etc. There are some color roles used mostly for 3D bevel and shadow effects. All of these are normally derived from \c Window, and used in ways that depend on that relationship. For example, buttons depend on it to make the bevels look attractive, and Motif scroll bars depend on \c Mid to be slightly different from \c Window. \value Light Lighter than \c Button color. \value Midlight Between \c Button and \c Light. \value Dark Darker than \c Button. \value Mid Between \c Button and \c Dark. \value Shadow A very dark color. By default, the shadow color is Qt::black. Selected (marked) items have two roles: \value Highlight A color to indicate a selected item or the current item. By default, the highlight color is Qt::darkBlue. \value HighlightedText A text color that contrasts with \c Highlight. By default, the highlighted text color is Qt::white. There are two color roles related to hyperlinks: \value Link A text color used for unvisited hyperlinks. By default, the link color is Qt::blue. \value LinkVisited A text color used for already visited hyperlinks. By default, the linkvisited color is Qt::magenta. Note that we do not use the \c Link and \c LinkVisited roles when rendering rich text in Qt, and that we recommend that you use CSS and the QTextDocument::setDefaultStyleSheet() function to alter the appearance of links. For example: \quotefromfile doc/src/snippets/textdocument-css/main.cpp \skipto QTextBrowser \printuntil browser.document \omitvalue NColorRoles \omitvalue NoRole*//*! Constructs a palette object that uses the application's default palette. \sa QApplication::setPalette(), QApplication::palette()*/QPalette::QPalette() : d(QApplication::palette().d), current_group(Active), resolve_mask(0){ d->ref.ref();}static void qt_palette_from_color(QPalette &pal, const QColor & button){ QColor bg = button, btn = button, fg, base; int h, s, v; bg.getHsv(&h, &s, &v); if(v > 128) { fg = Qt::black; base = Qt::white; } else { fg = Qt::white; base = Qt::black; } //inactive and active are the same.. pal.setColorGroup(QPalette::Active, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white), QBrush(base), QBrush(bg)); pal.setColorGroup(QPalette::Inactive, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white), QBrush(base), QBrush(bg)); pal.setColorGroup(QPalette::Disabled, QBrush(btn.darker()), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(btn.darker()), QBrush(Qt::white), QBrush(bg), QBrush(bg));}/*! Constructs a palette from the \a button color. The other colors are automatically calculated, based on this color. \c Window will be the button color as well.*/QPalette::QPalette(const QColor &button){ init(); qt_palette_from_color(*this, button);}/*! Constructs a palette from the \a button color. The other colors are automatically calculated, based on this color. \c Window will be the button color as well.*/QPalette::QPalette(Qt::GlobalColor button){ init(); qt_palette_from_color(*this, button);}/*! Constructs a palette. You can pass either brushes, pixmaps or plain colors for \a windowText, \a button, \a light, \a dark, \a mid, \a text, \a bright_text, \a base and \a window. \sa QBrush*/QPalette::QPalette(const QBrush &windowText, const QBrush &button, const QBrush &light, const QBrush &dark, const QBrush &mid, const QBrush &text, const QBrush &bright_text, const QBrush &base, const QBrush &window){ init(); setColorGroup(All, windowText, button, light, dark, mid, text, bright_text, base, window);}/*!\obsolete Constructs a palette with the specified \a windowText, \a window, \a light, \a dark, \a mid, \a text, and \a base colors. The button color will be set to the window color.*/QPalette::QPalette(const QColor &windowText, const QColor &window, const QColor &light, const QColor &dark, const QColor &mid, const QColor &text, const QColor &base){ init(); setColorGroup(All, QBrush(windowText), QBrush(window), QBrush(light), QBrush(dark), QBrush(mid), QBrush(text), QBrush(light), QBrush(base), QBrush(window));}/*! Constructs a palette from a \a button color and a \a window. The other colors are automatically calculated, based on these colors.*/QPalette::QPalette(const QColor &button, const QColor &window){ init(); QColor bg = window, btn = button, fg, base, disfg; int h, s, v; bg.getHsv(&h, &s, &v); if(v > 128) { fg = Qt::black; base = Qt::white; disfg = Qt::darkGray; } else { fg = Qt::white; base = Qt::black; disfg = Qt::darkGray; } //inactive and active are identical setColorGroup(Inactive, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white), QBrush(base), QBrush(bg)); setColorGroup(Active, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white), QBrush(base), QBrush(bg)); setColorGroup(Disabled, QBrush(disfg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(disfg), QBrush(Qt::white), QBrush(base), QBrush(bg));}/*! Constructs a copy of \a p. This constructor is fast thanks to \l{implicit sharing}.*/QPalette::QPalette(const QPalette &p){ d = p.d; d->ref.ref(); resolve_mask = p.resolve_mask; current_group = p.current_group;}/*! Destroys the palette.*/QPalette::~QPalette(){ if(!d->ref.deref()) delete d;}/*!\internal*/void QPalette::init() { d = new QPalettePrivate; resolve_mask = 0; current_group = Active; //as a default..}/*! Assigns \a p to this palette and returns a reference to this palette. This operation is fast thanks to \l{implicit sharing}.*/QPalette &QPalette::operator=(const QPalette &p){ QPalettePrivate *x = p.d; x->ref.ref(); resolve_mask = p.resolve_mask; current_group = p.current_group; x = qAtomicSetPtr(&d, x); if(!x->ref.deref()) delete x; return *this;}/*! Returns the palette as a QVariant*/QPalette::operator QVariant() const{ return QVariant(QVariant::Palette, this);}/*! \fn const QColor &QPalette::color(ColorGroup group, ColorRole role) const Returns the color in the specified color \a group, used for the given color \a role. \sa brush() setColor() ColorRole*//*! \fn const QBrush &QPalette::brush(ColorGroup group, ColorRole role) const Returns the brush in the specified color \a group, used for the given color \a role. \sa color() setBrush() ColorRole*/const QBrush &QPalette::brush(ColorGroup gr, ColorRole cr) const{ Q_ASSERT(cr < NColorRoles); if(gr >= (int)NColorGroups) { if(gr == Current) { gr = (ColorGroup)current_group; } else { qWarning("QPalette::brush: Unknown ColorGroup: %d", (int)gr); gr = Active; } } return d->br[gr][cr];}/*! \fn void QPalette::setColor(ColorGroup group, ColorRole role, const QColor &color) Sets the brush in the specified color \a group, used for the given color \a role, to the specified solid \a color. \sa setBrush() color() ColorRole*//*! \fn void QPalette::setBrush(ColorGroup group, ColorRole role, const QBrush &brush) \overload Sets the brush in the specified color \a group, used for the given color \a role, to \a brush. \sa brush() setColor() ColorRole*/void QPalette::setBrush(ColorGroup cg, ColorRole cr, const QBrush &b){ Q_ASSERT(cr < NColorRoles); detach(); if(cg >= (int)NColorGroups) { if(cg == All) { for(int i = 0; i < (int)NColorGroups; i++) d->br[i][cr] = b; resolve_mask |= (1<<cr); return; } else if(cg == Current) { cg = (ColorGroup)current_group; } else { qWarning("QPalette::setBrush: Unknown ColorGroup: %d", (int)cg); cg = Active; } } d->br[cg][cr] = b; resolve_mask |= (1<<cr);}/*! \since 4.2 Returns true if the ColorGroup \a cg and ColorRole \a cr has been set previously on this palette; otherwise returns false. \sa setBrush()*/bool QPalette::isBrushSet(ColorGroup cg, ColorRole cr) const{ Q_UNUSED(cg); return (resolve_mask & (1<<cr));}/*! \internal*/void QPalette::detach(){ if (d->ref != 1) { QPalettePrivate *x = new QPalettePrivate; for(int grp = 0; grp < (int)NColorGroups; grp++) { for(int role = 0; role < (int)NColorRoles; role++) x->br[grp][role] = d->br[grp][role]; } x = qAtomicSetPtr(&d, x); if(!x->ref.deref()) delete x; } ++d->detach_no;}/*! \fn bool QPalette::operator!=(const QPalette &p) const Returns true (slowly) if this palette is different from \a p; otherwise returns false (usually quickly).*//*! Returns true (usually quickly) if this palette is equal to \a p; otherwise returns false (slowly).*/bool QPalette::operator==(const QPalette &p) const{ if (isCopyOf(p)) return true; for(int grp = 0; grp < (int)NColorGroups; grp++) { for(int role = 0; role < (int)NColorRoles; role++) { if(d->br[grp][role] != p.d->br[grp][role]) return false; } } return true;}#ifdef QT3_SUPPORTbool QColorGroup::operator==(const QColorGroup &other) const{ if (isCopyOf(other)) return true; for (int role = 0; role < int(NColorRoles); role++) { if(d->br[current_group][role] != other.d->br[other.current_group][role]) return false; } return true;}/*! Returns the color group as a QVariant*/QColorGroup::operator QVariant() const{ return QVariant(QVariant::ColorGroup, this);}#endif/*! \fn bool QPalette::isEqual(ColorGroup cg1, ColorGroup cg2) const Returns true (usually quickly) if color group \a cg1 is equal to \a cg2; otherwise returns false.*/bool QPalette::isEqual(QPalette::ColorGroup group1, QPalette::ColorGroup group2) const{ if(group1 >= (int)NColorGroups) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -