PDA

View Full Version : Background-image in top level widget



alitoh
24th October 2012, 23:20
I'm trying to set the background image to my top-level widget and it's not working as expected. Am I doing something wrong here or is it just that you can't set a background image to the top level widget?

This is a (very) small example where my issue is clear; I have a QWidget (StyleTest) in whose constructor I try to set its stylesheet. It does nothing.
I also instantiate another QWidget* (for testing) and I set the very same styleSheet to it. THAT ONE displays perfectly.

What am I doing wrong here?

main.cpp

#include "styletest.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StyleTest w;
w.show();
return a.exec();
}


.h

#ifndef STYLETEST_H
#define STYLETEST_H

#include <QtGui/QMainWindow>
#include "qwidget.h"
#include "ui_styletest.h"

class StyleTest : public QWidget
{
Q_OBJECT

public:
StyleTest(QWidget *parent = 0, Qt::WFlags flags = 0);
~StyleTest();

private:
Ui::StyleTestClass ui;
};

#endif // STYLETEST_H


.cpp

#include "styletest.h"

StyleTest::StyleTest(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
QString style = " QWidget { background: url(./picture.png); } ";
setStyleSheet(style);

QWidget * w = new QWidget();
w->setStyleSheet(style);
w->show();
}

StyleTest::~StyleTest()
{

}



8353


as a side note background-image has the same output/result.

Thanks for your help.

ChrisW67
24th October 2012, 23:38
Relative path to image... relative to where exactly?

alitoh
24th October 2012, 23:42
Relative to the .vcproj file.

ChrisW67
24th October 2012, 23:49
No, relative to the current working directory of the running program wherever that might be.

alitoh
24th October 2012, 23:51
Which is my cvproj file Path.

alitoh
1st November 2012, 20:29
Update:

If I remove the Q_OBJECT macro, the background-image works properly. Of course I lose important stuff like Signals and slots in the process, so this is not a good enough answer to my problem, but at least there's another piece to the puzzle.

Any ideas?

ChrisW67
2nd November 2012, 03:22
The Q_OBJECT macro has nothing to do with the problem.

The background cannot be shown if it cannot be found in the relative location you have given it. The location is relative to the current working directory of the running process, which is both a movable location and almost certainly not where you think it is much of the time. Put the background image in a resource or be more explicit about where it is to be found and your problem will go away.

alitoh
2nd November 2012, 03:32
If that's the case, how is it possible for one widget in the same application the stylesheet (hence the path) is working properly and in the other one it is not?

My screen shot is a single application instance, not two. The relative path for both widgets is the same, but they do not yield the same result.

And removing the Q_OBJECT *is* allowing me to see the background image.