设置QPushButton的背景图片(或者Window、WindowText等的背景图片/颜色)

来源:互联网 发布:剑灵人男捏脸数据图 编辑:程序博客网 时间:2024/06/07 07:54

设置QPushButton的背景图片

分类: Qt
QtQPushButton

目录(?)[-]

  1. void QPalettesetBrush ColorRole role const QBrush brush
  2. enum QPaletteColorRole
setIcon(QIcon("toolbutton.png"));
      setIconSize(QSize(48, 48));

如果没有下面那句话,该图片是被缩放的放到图片上

如果加上下面那句话这该图片的背景图被设置为当前尺寸

 

如果不是按钮可以用调色板来实现

      QPalette p = palette();
      p.setBrush(QPalette::Button, QBrush(QPixmap("toolbutton.png")));
      setPalette(p);

注意:setBrush的第一个参数文档上是这样说的:

void QPalette::setBrush ( ColorRole role, const QBrush & brush );


enum QPalette::ColorRole

The ColorRole enum defines the different symbolic color roles used in current GUIs.

The central roles are:

ConstantValueDescriptionQPalette::Window10A general background color.QPalette::BackgroundWindowThis value is obsolete. Use Window instead.QPalette::WindowText0A general foreground color.QPalette::ForegroundWindowTextThis value is obsolete. Use WindowText instead.QPalette::Base9Used 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.QPalette::AlternateBase16Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).QPalette::ToolTipBase18Used as the background color for QToolTip and QWhatsThis.QPalette::ToolTipText19Used as the foreground color for QToolTip and QWhatsThis.QPalette::Text6The foreground color used with Base. This is usually the same as the WindowText, in which case it must provide good contrast withWindow and Base.QPalette::Button1The general button background color. This background can be different from Window as some styles require a different background color for buttons.QPalette::ButtonText8A foreground color used with the Button color.QPalette::BrightText7A text color that is very different from WindowText, and contrasts well with e.g. Dark. Typically used for text that needs to be drawn where Text or 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 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 fromWindow, 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 Mid to be slightly different from Window.

ConstantValueDescriptionQPalette::Light2Lighter than Button color.QPalette::Midlight3Between Button and Light.QPalette::Dark4Darker than Button.QPalette::Mid5Between Button and Dark.QPalette::Shadow11A very dark color. By default, the shadow color is Qt::black.

Selected (marked) items have two roles:

ConstantValueDescriptionQPalette::Highlight12A color to indicate a selected item or the current item. By default, the highlight color is Qt::darkBlue.QPalette::HighlightedText13A text color that contrasts with Highlight. By default, the highlighted text color is Qt::white.

There are two color roles related to hyperlinks:

ConstantValueDescriptionQPalette::Link14A text color used for unvisited hyperlinks. By default, the link color is Qt::blue.QPalette::LinkVisited15A text color used for already visited hyperlinks. By default, the linkvisited color is Qt::magenta.

Note that we do not use the Link and 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:

     QTextBrowser browser;     QColor linkColor(Qt::red);     QString sheet = QString::fromLatin1("a { text-decoration: underline; color: %1 }").arg(linkColor.name());     browser.document()->setDefaultStyleSheet(sheet);
ConstantValueDescriptionQPalette::NoRole17No role; this special role is often used to indicate that a role has not been assigned.

 说明:每次遇到有点坑爹的问题时,不要一上来到网上狂搜,先看一下api文档嘛!这不?别人说的很清除的。

0 0
原创粉丝点击