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.
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.