PDA

View Full Version : Background image of widget in QWidgetStack



el33t
14th March 2011, 09:55
Hi,

I have a QStackedWidget with two widgets in it. Now, I want to apply a background image to only the first widget without changing anything in the second widget. How do I do it?

Anyways here's the code:


qs = new QStackedWidget(this);
qs->resize(880,558);
qs->addWidget(men);
qs->addWidget(qur);

*I tried the following but didn't work/got unexpected results.*

1) men->setStyleSheet("QWidget {background-image: url(:/file/back_menu.png)}");

In this the main background of 'men' did not change. Only the background of men's children widget like QPushButton changed.

2) qs->setStyleSheet("QStackWidget {background-image: url(:/file/back_menu.png)}");

This changed the background of both the first and second widget

3) qs->setStyleSheet("QWidget#men {background-image: url(:/file/back_menu.png)}");

Nothing changes.

wysota
14th March 2011, 10:36
How is "men" implemented?

el33t
14th March 2011, 10:56
Ok, edited the question. Bolded the edited part.

@wysota : Mmmm, I'm not sure I understand what you are asking, but here is the complete code. I hope this helps.

mainwindow.cpp


mainwindow::mainwindow()
{

#include <QtGui>
#include <iostream>
#include "mainwindow.h"
#include "game.h"
#include "menu1.h"

men = new menu1(this);
qur = new game(this);

qs = new QStackedWidget(this);
qs->resize(880,558);
qs->addWidget(men);
qs->addWidget(qur);

qs->setCurrentIndex(0);

show();
}


menu1.cpp


#include "menu1.h"
#include <QtGui>

menu1::menu1(QWidget *parent) :
QWidget(parent)
{

resize(880,558);

}

void menu1::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPainterPath path;
path.moveTo(qrand() % 80, qrand() % 320);
path.cubicTo(200, 200, 320, 80, 480, 320);
painter.setPen(QPen(Qt::green, 8));
painter.drawPath(path);
}

wysota
14th March 2011, 11:01
You're doing painting yourself so stylesheets won't work. You either have to reenable stylesheets by adding some code to the paint event or set the image as the background role in the palette and enable autoFillBackground for your widget.

el33t
14th March 2011, 12:05
Ok, thanks. I'll try out your solutions and report back as soon as possible. :)

Added after 41 minutes:


You're doing painting yourself so stylesheets won't work.

Hey, I removed all the paintevent code from menu1.cpp and menu1.h . Still the stylesheet doesn't work. I mean, only the child widgets of 'men' like pushbuttons have their backgrounds changed and not men itself.


You either have to reenable stylesheets by adding some code to the paint event or set the image as the background role in the palette and enable autoFillBackground for your widget.

Can you please show me some code for the above two solutions? I'm very new to QT, so please pardon my lack of knowledge.


By the way, can you suggest some solutions through whcih I can change the background through stylesheet and not through QPalette.

Regards.

el33t
15th March 2011, 04:24
Bump... Please help me guys...

wysota
15th March 2011, 11:45
Hey, I removed all the paintevent code from menu1.cpp and menu1.h .
Did you comment out the paintEvent() method?


Can you please show me some code for the above two solutions? I'm very new to QT, so please pardon my lack of knowledge.
This is no excuse. Search the forum, the answers are there.

el33t
15th March 2011, 21:38
1) Did you comment out the paintEvent() method?


2) This is no excuse. Search the forum, the answers are there.

1) Yes. Why? that won't work?

2) Sorry. I solved the problem using the QPalllete method but I wanted to use StyleSheet rather than QPallette.

wysota
15th March 2011, 22:15
1) Yes. Why? that won't work?
If your widget class has the Q_OBJECT macro then it won't work. I have recently "solved" this "problem" twice on this forum so the answer is there, just look for it.

el33t
16th March 2011, 04:10
If your widget class has the Q_OBJECT macro then it won't work. I have recently "solved" this "problem" twice on this forum so the answer is there, just look for it.

Ok, thanks for the assistance.