设置QPushButton的背景图片

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:

Constant Value Description
QPalette::Window 10 A general background color.
QPalette::Background Window This value is obsolete. Use Window instead.
QPalette::WindowText 0 A general foreground color.
QPalette::Foreground WindowText This value is obsolete. Use WindowText instead.
QPalette::Base 9 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.
QPalette::AlternateBase 16 Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).
QPalette::ToolTipBase 18 Used as the background color for QToolTip and QWhatsThis.
QPalette::ToolTipText 19 Used as the foreground color for QToolTip and QWhatsThis.
QPalette::Text 6 The foreground color used with Base. This is usually the same as the WindowText, in which case it must provide good contrast with Window and Base.
QPalette::Button 1 The general button background color. This background can be different from Window as some styles require a different background color for buttons.
QPalette::ButtonText 8 A foreground color used with the Button color.
QPalette::BrightText 7 A 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 from 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 Mid to be slightly different from Window.

Constant Value Description
QPalette::Light 2 Lighter than Button color.
QPalette::Midlight 3 Between Button and Light.
QPalette::Dark 4 Darker than Button.
QPalette::Mid 5 Between Button and Dark.
QPalette::Shadow 11 A very dark color. By default, the shadow color is Qt::black.

Selected (marked) items have two roles:

Constant Value Description
QPalette::Highlight 12 A color to indicate a selected item or the current item. By default, the highlight color is Qt::darkBlue.
QPalette::HighlightedText 13 A text color that contrasts with Highlight. By default, the highlighted text color is Qt::white.

There are two color roles related to hyperlinks:

Constant Value Description
QPalette::Link 14 A text color used for unvisited hyperlinks. By default, the link color is Qt::blue.
QPalette::LinkVisited 15 A 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);
Constant Value Description
QPalette::NoRole 17 No role; this special role is often used to indicate that a role has not been assigned.

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

你可能感兴趣的:(Qt)