关于加背景图片的问题似乎在论坛里有很多,但是给QPushButton加入背景图的似乎没有见过,今天我看了
==Practical Qt系列1.1:给widget设置背景图片==
这个主题的内容后,对给Widget添加背景图片很感兴趣,于是将附件的QT4部分的主程序改写了一下,就是加入了一个QPushButton,希望也能加入背景图片
#include <QtGui>
int main( int argc, char ** argv )
{
  QApplication app(argc, argv);
  // Setting a background image in a QListView
  QListWidget* lv = new QListWidget( 0 );
  
  QPalette palette;
  palette.setBrush(QPalette::Base, QBrush(QPixmap("logo.png")));
  lv->setPalette(palette);
  
  lv->insertItems(0, QStringList()<<"test data"<<"test data 2");
  lv->setFixedSize( QPixmap("logo.png").size() );
  lv->show();
  
  QWidget *w = new QWidget(0);
  QPalette palette2;
  palette2.setBrush(QPalette::Base, QBrush(QPixmap("logo.png")));
  palette2.setBrush(QPalette::Window, QBrush(QPixmap("logo.png")));
  palette2.setBrush(QPalette::Button, QBrush(QPixmap("logo.png")));  //这句是加上的
  w->setPalette(palette2);
  QVBoxLayout *lout = new QVBoxLayout(w);
  QTextEdit* edit = new QTextEdit( );
  QLabel* label = new QLabel( "This is a transparent label" );
  QCheckBox* cb = new QCheckBox( "Check me" );
  QPushButton *pb=new QPushButton("pushbutton");    //这句是加上的
  lout->addWidget(edit);
  lout->addWidget(label);
  lout->addWidget(cb);
  lout->addWidget(pb); //这句是加上的
  
  w->show();
  QObject::connect( &app, SIGNAL( lastWindowClosed() ),
                    &app, SLOT( quit() ) );
  return app.exec();
}
其他没有改变,编译成功,运行后发现pushbutton还是默认的样子,没有加入背景图片,不知道是哪里出了错,欢迎大家指教!