PDA

View Full Version : Setting Dynamic Back ground image on QTableWidget



dholliday
16th May 2011, 11:09
Hi There.

Simple question, but I can't figure out how to make it work.
I want to set an image to be always at the bottom right hand corner of my QTableWidget. I've got this to work by setting the following:


myTableWidget->viewport()->setStyleSheet("background-image: url(:/images/myimage.png); background-repeat:no-repeat;background-attachment:fixed;background-position:bottom right;");

This work exactly as I wanted. However I've now come to enhance some features and my image now needs to be dynamic. I have a class that return a QPixmap with the appropriate image. I don't want to save the physical file to a harddrive because I may have multiple version of the same control open at the same time all requireing different images, and I don't want to clog up the users hard drive with temporary images.

I've tried lots of different variation of the following all with little success.


QPalette pal = myTableWidget->viewport()->palette();
pal.setBrush( myTableWidget->viewport()->backgroundRole(), QBrush( QPixmap(":/images/myimage.png")) );
myTableWidget->viewport()->setPalette( pal );


This sort of works, but it's on the top right and when I scroll the table columns from left to right the image breaks up, also the image scrolls and is not fixed to possition.

Anyhelp would be apprechiated.

wysota
17th May 2011, 00:33
Override the paint event and draw the image yourself before calling the base class implementation.

dholliday
17th May 2011, 09:42
Override the paint event and draw the image yourself before calling the base class implementation.

Sorry didn't mention. The code that I said doesn't work correctly is being used inside the paint event before the base class implementation. It work OK. But when I add columns to my control and then scroll left and right, it breaks the image up. So I must be doing something wrong.

wysota
17th May 2011, 10:32
Hard to say without seeing this code.