Background image stretch for QWidget Screen - qt

Is it possible to stretch the background image.
Actually I have a image of size 320X480, I want to use this image as background for a QWidget screen with size 360X640.
I tried with the followings:
1). Here the image gets stretched for the screen but a button with another image is loaded along with the Main screen image:
ui->MainScreen->setStyleSheet("border-image: url(:/images/Main-screen.png);");
2). Here the image is not stretched, but displayed as tiles:
ui->MainScreen->setStyleSheet("background-image: url(:/images/Main-screen.png); border: none");
ui->MainScreen->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));
ui->MainScreen->setBackgroundRole(QPalette::Background);
Any suggestions for this....!
Thanks...

I think the reason that your button is getting the same image as the background is because style sheets are applied to child widgets too (I think). After doing the same code you used in #1, try setting the button's style sheet to something else. The child's style sheet should override the parent's style sheet.

Try the following:
ui->MainScreen->setStyleSheet("QMainWindow {border-image: url(:/images/Main-screen.png);}");
This tells Qt to change the background image of all of the QMainWindows in the child tree. from ui->MainScreen. If you do not specify the class type or object name, no filter will be applied, and every child will get the background image.

QWidget#QMainWindow { style; }
#subclass after mainclass (QWidget) sets style to JUST the subclass
It will do exactly what your trying to do.

Related

Qt:Unable to set Background color for qComboBox properly

I have a QCombobox and I want to set a white background color.This is my code.
QComboBox *cBox = new QComboBox;
cBox->addItem("Text1");
cBox->setStyleSheet("background-color:white");
This combobox has a parent widget whose background is an image and is set as given below:
ui->centralWidget->setStyleSheet("border-image:url(./image.png)");
When I set the parent Widget[centralWidget] background as some other color,then the white BG works properly for the combobox.But when I set an image as the parent Widget background,the UI looks like this.
In the above pic,the black Bg is an image.Could someone highlight me what am I missing.Any help will be really helpful.
When you do not indicate to which widget you are going to apply some property, they will be applied to all your children, for this reason the same QComboBox background image is applied to the child of centralWidget.
In your case you want to apply only to the centralWidget, and by default Qt Designer uses the same name for the name of the variable that represents the widget and the objectName.
So if you want to apply to a widget we can use the objectName as selector:
QWidget#centralWidget{ border-image:url(./image.png)}

cn1 - replace background image

I have a button on which I set the background image using css, and then sometimes need to change it dynamically in the code. But if I change it, I see both images, one on top of the other.
Here's my css:
ExpertVideoButton{
cn1-derive: Label;
background-image: url(images/play-video.png);
cn1-source-dpi: 180;
cn1-background-type: cn1-image-scaled-fit;
}
And my code:
mStateMachine.findViewVideoBtn(f).getAllStyles().setBgImage(mStateMachine.res.getImage("no_video.png"));
The previous background image doesn't get replaced by the new one. Any ideas on how to solve this?
Update
The original background image wasn't actually being set from the css - there was a css style that was unused, and the image was being set from the code, twice. The first time I had made it the Label's icon, and the second time its background image. Unfortunately, this was a case of me having trouble reading my own code!
Since you use transparent images you need to set the bgTransparency of the style to transparent (0) in the CSS (not sure how you do it there).

How do I set the background position and size of a background image I set with setBackgroundBrush?

I used Qt's setBackgroundBrush function to set a background image. How do I resize the image or set it's position?
For example, I'd like it to be centered and for it to fill the entire region.
I don't think it's possible with setBackgroundBrush, but you can also set background image with style sheets if you want more control. Sample code from Qt docs:
QLabel {
background-image: url(dense6pattern.png);
background-repeat: repeat-xy;
}
More info available here.

BitmapData with Scale9Grid

I have a Skin for SkinnableContainer.
Skin contains only the original contentGroup and s:BitmapImage as background.
Background image stretchers out according to width and height content.
The used image is .png with transparent regions.
To create hitarea, I used this algorithm:
http://www.webverwirklichung.com/en/blog/programming/flex/creating-hitarea-png-image-transparent-alpha-regions-flex
Everything's working fine until I start to stretch the SkinnableContainer (along with the image in the skin).
I have a scale9Grid for the image.
The problem is, that when stretching the image, the bitmapData of the image is still the same (same width and height). Therefore I need to somehow obtain the bitmapData of the image for the scale9Grid application.
The background of the component is an image, which has some transparent areas. It is only possible to click on the visible part of the image. The image will stretch according to the content in contentGroup.
Need an advice, please.
How about using the BitmapData.draw() method?
After the container is resized and the 9 slice scaling has been applied (or whatever the appropriate trigger is) do:
var b:BitmapData = new BitmapData(container.width, container.height);
b.draw(container);
Then use this new bitmap with the algorithm that creates the hit area.

Flex scrollbar styling issue

I'm trying to style vscrollbar and hscrollbar inside a Vbox.But there's always a white square thing at the right bottom cornor which can not be styled.
My CSS is:
ScrollBar{
downArrowUpSkin: Embed(source="assets/images/scrollbar/arrow_down.png");
downArrowOverSkin: Embed(source="assets/images/scrollbar/arrow_down.png");
downArrowDownSkin: Embed(source="assets/images/scrollbar/arrow_down.png");
upArrowUpSkin: Embed(source="assets/images/scrollbar/arrow_up.png");
upArrowOverSkin: Embed(source="assets/images/scrollbar/arrow_up.png");
upArrowDownSkin: Embed(source="assets/images/scrollbar/arrow_up.png");
thumbDownSkin: Embed(source="assets/images/scrollbar/thumb.png");
thumbUpSkin: Embed(source="assets/images/scrollbar/thumb.png");
thumbOverSkin: Embed(source="assets/images/scrollbar/thumb.png");
trackSkin:Embed(source="assets/images/scrollbar/track.png");
fillAlphas:0,0,0,0;}
Could anyone help me out?Much Thanks!
This is a weird one. The white box at the bottom right is actually a (raw) child of the container.
To get around this you need to subclass whatever container you want to add your styled scrollbars to and remove the child called "whitebox":
var whitebox:DisplayObject = rawChildren.getChildByName('whiteBox');
if (whitebox)
rawChildren.removeChild(whitebox);
IIRC you need to do the above in two places: an override of createChildren and an override of validateDisplayList. In both cases remember to call the super class method first!
That area isn't controlled by the scroll bar(s), it's part of the original container. Does the VBox have it's background colour set to black?

Resources