PDA

View Full Version : How to set background of application??



kamlesh.sangani
5th May 2009, 06:03
Hi,

I am new bie to Qt. I want to set background (image) to my application. how can i do that??
I know QPixmap load the image but i dont know how to set it as background.

I am working on Qt for S60(Symbian OS). Using 4.5.0-garden release.

Thanks,
kamlesh.

wysota
5th May 2009, 08:27
Take a look at QPalette.

wagmare
5th May 2009, 10:55
hi friends ,
i am little confused with this question
which one is best to set the background ..
QPalette or StyleSheet...?

spirit
5th May 2009, 11:00
using QPalette is faster, but you still can use Style Sheets. :)

wagmare
5th May 2009, 11:53
thanks for the reply first ...
in a graphicsView i am animating an item .. when mouse hover over the item it will scale ... normally it works perfectly untill i setStyleSheet->background color .. the animation becomes very slow .. so i switch to QPalette and now it works fine ... why ...?

spirit
5th May 2009, 11:55
as I said, QPalette is faster then Style Sheets, because when you use Style Sheets all information about style must be parsed and this process takes a time.

wagmare
5th May 2009, 11:59
thanks .. i get it ...

kamlesh.sangani
5th May 2009, 12:23
Take a look at QPalette.

AFAIK QPalette will help to change colors of background, not to set image as background. I want to set image as background.

spirit
5th May 2009, 12:26
then you need to reimplement QWidget:: paintEvent and using QPainter::drawPixmap draw it.

wagmare
5th May 2009, 13:45
AFAIK QPalette will help to change colors of background, not to set image as background. I want to set image as background.

use styleSheet background-image:

wysota
5th May 2009, 20:47
AFAIK QPalette will help to change colors of background, not to set image as background. I want to set image as background.

Funny, I wonder what QPalette::setBrush() is for then :)


#include <QtGui>

int main(int argc, char **argv){
QApplication app(argc, argv);
QListView w;
QPalette p = w.palette();
p.setBrush(QPalette::Base, QPixmap("/usr/share/wallpapers/Air/contents/images/1024x768.jpg"));
w.setPalette(p);
w.show();
return app.exec();
}

kamlesh.sangani
6th May 2009, 08:05
Fantastic. its worked for me. thanks a lot wysota.

I ve dont it with slit modification.



p.setBrush(QPalette::Background, QPixmap("C://01.jpg"))

wysota
6th May 2009, 08:11
The role depends on the widget so with a listview you have to use Base while for a frame you'd use Window or Background.

IRON_MAN
8th July 2009, 11:29
But with palette if you have labels,buttons... you cover with the background image,isn't it?
How can it solve it?

wysota
8th July 2009, 12:31
You cover what with what?

IRON_MAN
8th July 2009, 13:07
I have set some buttons in a window and now I have to insert an image as a background.
I´m trying to do as you say but now the buttons don't appear.

wysota
8th July 2009, 17:55
Please show us the code.

IRON_MAN
9th July 2009, 10:29
It was a problem of my programm. I have solved it!

But, if I maximize the window , the image appears once,twice... and it doesn´t suit to the window. My image is .bmp

QPalette pal = widget.palette();
pal.setBrush( QPalette::Window, QPixmap("image.bmp"));
widget.setPalette( pal );

nish
9th July 2009, 10:36
It was a problem of my programm. I have solved it!

But, if I maximize the window , the image appears once,twice... and it doesn´t suit to the window. My image is .bmp

QPalette pal = widget.palette();
pal.setBrush( QPalette::Window, QPixmap("image.bmp"));
widget.setPalette( pal );

re implement resizeEvent and resize your image to desired size


void MyWidget::resizeEvent(QResizeEvent* e)
{
static QPixmap pix("image.bmp");
QPalette pal = widget.palette();
pal.setBrush( QPalette::Window, pix.scaled(e->size(),...,...) );
widget.setPalette( pal );
}

IRON_MAN
9th July 2009, 11:22
Thanks a lot!

tsuibin
9th July 2009, 12:04
in qt ,how can i enlarge and narrow a image (QPixmap)?

nish
9th July 2009, 12:12
take at look at QPixmap::scaled()