Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SetStyleSheet background-color doesn't work on a derived widget
Forum Updated to NodeBB v4.3 + New Features

SetStyleSheet background-color doesn't work on a derived widget

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 10.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ReHa1
    wrote on last edited by
    #1

    If i set a background-color by setStyleSheet() on a QWidget directly that works perfect.
    But if i create an own widget, derived by QWidget, the background-color doesn't work in it.
    Does anybody have an idea why??? Many Thanks!!!

    Here the code which shows the difference:

    main.cpp:
    @#include <QtGui/QApplication>
    #include "mywidgets.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWidget w;
    w.show();

    return a.exec();
    }
    @

    mywidgets.h:
    @#include <QWidget>

    class MainWidget : public QWidget
    {
    Q_OBJECT

    public:
    explicit MainWidget(QWidget *parent = 0);
    };

    class RedWidget : public QWidget
    {
    Q_OBJECT

    public:
    explicit RedWidget(QWidget *parent = 0);
    };

    class YellowWidget : public QWidget
    {
    Q_OBJECT

    public:
    explicit YellowWidget(QWidget *parent = 0);
    };@

    mywidgets.cpp:
    @#include "mywidgets.h"
    #include <QHBoxLayout>
    #include <QWidget>

    RedWidget::RedWidget(QWidget *parent) :
    QWidget(parent)
    {
    this->setStyleSheet("background-color:red");
    }

    YellowWidget::YellowWidget(QWidget *parent) :
    QWidget(parent)
    {
    this->setStyleSheet("background-color:yellow");
    }

    MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
    {
    this->setMinimumWidth(800);
    this->setMinimumHeight(600);
    this->setStyleSheet("background-color:grey");

    QHBoxLayout* layout = new QHBoxLayout(this);

    /*
    // This works fine:
    QWidget* wYellow = new QWidget(this);
    QWidget* wRed = new QWidget(this);
    wYellow->setStyleSheet("background-color:yellow");
    wRed->setStyleSheet("background-color:red");
    */

    // But not this version:
    YellowWidget* wYellow = new YellowWidget(this);
    RedWidget* wRed = new RedWidget(this);

    layout->addWidget(wYellow);
    layout->addWidget(wRed);

    this->setLayout(layout);

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Read the documentation for QWidget.

      http://doc.qt.nokia.com/4.7/stylesheet-reference.html

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ReHa1
        wrote on last edited by
        #3

        You mean


        If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

        @void CustomWidget::paintEvent(QPaintEvent *)
        {
        QStyleOption opt;
        opt.init(this);
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
        }@

        The above code is a no-operation if there is no stylesheet set.

        Warning: Make sure you define the Q_OBJECT macro for your custom widget.

        Yes, thanks - that works.
        But i wonder, to have to do such overhead. Why isn't it implemented in QWidget itself?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved