[Qt-interest] How can I change a button's background?

Alexandre Beraud aberaud at infflux.com
Fri Jun 19 11:56:19 CEST 2009


Hi,

Using the paintEvent() is fishy, because it will bypass the style. You 
will certainly end with a rectangular picture, probably deformed by the 
geometry of the button. Using stylesheets will probably lead to the same 
result since you will have to redefine all the visual parameters of your 
button in your stylesheet (background, but also border, text and so on).
The only easy way to change a button's background, afaik, is to change 
the brush of the palette for color role QPalette::Button.
But maybe the trolls have a magic solution.

Regards,

Alex


Kermit Mei a écrit :
> Hello community!
>
> How can I change a button's background? 
>
> I know I can use styleSheet, but, my problem is that there're so many
> buttons, and every button's background may be change by the users at
> running time, and their background may not be the same. Also, I must
> remember the last background changed by user, and reload them at the
> next running time.
>
> How Can I do it? I what to use paintEvent() to do it, but when I repaint
> the background, I can't see any different effect when I press the
> buttons. My code is like this: 
>
> //The Header file:
> #ifndef _REMOTEBUTTON_H
> #define _REMOTEBUTTON_H
>
> #include <QPushButton>
> #include <QString>
>
> class QPixmap;
>
> class RemoteButton : public QPushButton
> {
>   Q_OBJECT
> public:
>   RemoteButton(QWidget *parent = 0);
>   ~RemoteButton();
>
>   void setBackGround(const QString picFile);
>
> protected:
>   void paintEvent(QPaintEvent *event);
>   QPixmap *pixmap;
> };
>
> #endif //_REMOTEBUTTON_H
>
>
> //The source file:
> #include <QPen>
> #include <QPainter>
> #include <QLabel>
> #include <QPixmap>
>
> #include "RemoteButton.h"
>
> RemoteButton::RemoteButton(QWidget *parent)
>   :QPushButton(parent),pixmap(0)
> {
>   //Do Nothing Here!
> }
>
> RemoteButton::~RemoteButton() {
>   if(pixmap != 0)
>     delete pixmap;
> }
>
> void RemoteButton::setBackGround(const QString picFile) {
>   if( pixmap != 0)
>     delete pixmap;
>   pixmap = new QPixmap(picFile);
> }
>
> void RemoteButton::paintEvent(QPaintEvent *event) {
>   if(pixmap != 0) {
>     QPainter painter(this);
>     painter.drawPixmap(0,0,width(),height(),*pixmap);
>   }else
>     QPushButton::paintEvent(event);
> }
>
>
> //The test main.cpp file
> #include <QApplication>
> #include "RemoteButton.h"
>
> int main(int argc,char *argv[])
> {
>   QApplication app(argc,argv);
>   RemoteButton rBtn;
>   rBtn.setBackGround("./bk.png");
>   rBtn.show();
>   return app.exec();
> }
>
>
> It can works, but I can't see and different effects when I press it.
>
> Hope your help.
> Thanks:)
>
> Kermit
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
>   


-- 
BERAUD Alexandre
Ingénieur Développement

Infflux - Informatique & Flux
Tel: 01 49 57 92 00 - Fax : 01 49 57 92 01
Mail: aberaud at infflux.com
Visitez notre site :  www.infflux.com





More information about the Qt-interest-old mailing list