Important: Please read the Qt Code of Conduct -

Composite widget + Stylesheets and a weird bug(?)

  • Hi guys. I'm working on a desktop project with the help of ui designer and we found a very weird behavior of qt.
    We have a composite widget called PagerView (inherited from QWidget). this widget has a horizontal layout in which we place buttons for paging large data.
    Our problem is that we are not able to apply stylesheet to our composite widget. For example, if we assign "background-color:yellow" to PagerView, qt designer reflects the change normally, we see the yellow background. see image bellow
    but, in runtime, when I run the project, PagerView widget takes normal appearance (gray color), but buttons which are inside the widget becomes yellow. Looks like stylesheet is overridden by QPushButtons. See picture bellow:

    I'm scratching my head for several hours, but couldn't figure out what could be the problem. Is it a bug or I'm doing something wrong?
    This is the only code PagerView.cpp contains:
    @void PagerView::refresh()
    QLayoutItem *item;
    while((item = ui->hLayout->takeAt(0))) {
    delete item->widget();
    else if(item->spacerItem())
    delete item->spacerItem();

    if(!m_CurrentPage) ++m_CurrentPage;
    if(itemsCount() == 0 || pageSize() == 0) return;
    uint buttonsCount = ceil(static_cast<float>(itemsCount()) / static_cast<float>(pageSize()));
    ui->hLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
    if(currentPage() > buttonsCount)
        m_CurrentPage = buttonsCount;
    for(uint i = 0; i < buttonsCount; ++i) {
        QPushButton *button = new QPushButton(this);
        button->setText(QString::number(i + 1));
        button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
        if(i == currentPage() - 1)
        connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    ui->hLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));


    You can download the test project from "here":

    Your help is much appreciated.

  • As far as I know, it work only in KDE.
    You can't change color of button.

  • I don't want to change button colors. I want to change the color of the container, in this case PagerView, but in runtime, button colors are changed instead of PagerView's. The designer renders the widget correctly, but when I run the program, I get that strange behavior.

  • I already tried Palette, but I had same result. Actually, I mentioned background-color for demonstration purpose only, in real scenario, I want to set background image, which is not possible with palette. Anyway thanks for the answer.

  • =|
    If you want image - just paint it.
    @void PagerView::paintEvent(QPaintEvent *)
    QPainter painter(this);
    QPixmap pix("/path");

  • @RazrFalcon thanks again, I appreciate your help :)
    You are right, that's a solution in my situation and I already did that, but one of the powerful feature of Qt is that one don't need to do low level painting in most cases when we have stylesheets. At least, my designer can't for sure :)
    That's why I reported this (possibly) bug here.
    Thanks again :)

  • Fine :)

  • No reaction from Qt stuff? :|

Log in to reply