设置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:

ConstantValueDescription
QPalette::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 with Window 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 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.

ConstantValueDescription
QPalette::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:

ConstantValueDescription
QPalette::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:

ConstantValueDescription
QPalette::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);
ConstantValueDescription
QPalette::NoRole17No role; this special role is often used to indicate that a role has not been assigned.

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


转自http://blog.csdn.net/zgrjkflmkyc/article/details/9042333


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值