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. QMainWindow->setStyleSheet behaves unexpected (Q_OBJECT causing problems?)
Forum Updated to NodeBB v4.3 + New Features

QMainWindow->setStyleSheet behaves unexpected (Q_OBJECT causing problems?)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.1k 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.
  • B Offline
    B Offline
    Batox
    wrote on last edited by
    #1

    Using Qt 4.8.4 on Windows 7 (MSVC 2010) I have a standard QMainWindow in my app with toolbar and menu. I want to achieve that toolbar and menu keep their grey color, but the central widget should have a white background. My test case is an app with just one label in the central widget. In order to see the problem, add or remove the Q_OBJECT line in test.h

    When Q_OBJECT is there, only the label gets a white bg. If Q_OBJECT is commented out, the whole central widget is white. Of course, I want the whole area white, but also need Q_OBJECT.

    Here's the files:
    main.cpp:
    @#include "test.h"

    class testwin : public QMainWindow {
    public:
    QWidget *centralWidget;
    QToolBar *mainToolBar;

    testwin(QWidget *parent = 0) : QMainWindow(parent) {
        centralWidget = new test(this);
        setCentralWidget(centralWidget);
        mainToolBar = new QToolBar(this);
        this->addToolBar(Qt::TopToolBarArea, mainToolBar);
    };
    
    ~testwin() {};
    

    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    testwin w;
    w.centralWidget->setStyleSheet("background-color: white;");
    w.show();
    return a.exec();
    }
    @

    test.h:
    @#ifndef TEST_H
    #define TEST_H

    #include <QtGui>

    class test : public QWidget
    {
    Q_OBJECT // remove this

    public:
    QLabel *label;

    test(QWidget *parent = 0&#41; {
        resize(400, 300);
        label = new QLabel(this);
        label->setText("Test");
    };
    

    };

    #endif // TEST_H
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Could you try with the latest Qt ? 4.8.5 in the 4 series, maybe even with Qt 5 to see if the behavior is the same.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        This can be reproduced in Qt5.1

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          Hi,

          From the documentation of Qt stylesheet, we can find the answer of the problem:

          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);
          }
          @

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Batox
            wrote on last edited by
            #5

            Thanks a lot - this solves the issue. I hope I'm not the only one who considers this kind of unexpected (I mean, why doesn't QWidget's default implementation of paintEvent deal with this?).

            Anyway, all is well, another RTFM solved :)

            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