Important: Please read the Qt Code of Conduct -

Minimal subclass of QWidget does not display like its ancestor... what more do I need to implement?

  • Hello everyone,

    I have made a minimal subclass of a QWidget. My subclasses widget does not display exactly the same as the QWidget it is derived from. since I have not changed any settings after deriving it I was expecting it appear and behave exactly like the default QWidget implementation. However, it appears to behave differently in my example code shown below. It does not display in the same way as the straight QWidget object does.

    Can someone please explain what is happening here and what I need to make my class behave like the parent class here?

    My thanks to all who offer suggestions.

    The header

    #include <QtWidgets/QWidget>

    class QWidget;

    class MyContentWidget : public QWidget

    explicit MyContentWidget(QWidget *parent=0);
    ~MyContentWidget() {;}


    #endif // MINIMALWIDGET_H

    The main code
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QWidget>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QPushButton>

    #include "MinimalWidget.h"

    MyContentWidget::MyContentWidget(QWidget *parent)
    : QWidget(parent)

    int main(int argc, char *argv[])
    QApplication a(argc,argv);

    // a transparent top level widget
    QWidget *widget = new QWidget;
    // a label so we can see the top widget
    QLabel *label = new QLabel(widget);
    label->setFixedSize(500, 500);
    label->setStyleSheet("border-radius: 20px;");
    // a close button
    QPushButton *button = new QPushButton(label);
    button->setStyleSheet("background-color: white; border-radius: 10px;");
    widget->connect(button, SIGNAL(pressed()), &a, SLOT(closeAllWindows()));
    button->setToolTip("Close the application (Qtrl-Q)"));
    // a widget for some content on the left side
    QWidget *contentWidget1 = new QWidget(label);
    // a label describing the widget on the left
    QLabel *label1 = new QLabel(contentWidget1);
    label1->setText("This is a QWidget");
    label1->setStyleSheet("color : black; background-color : white; border-radius: 0;");
    // my minimal subclassed widget on the right side
    // my problem is why does this not appear to be the same as the default widget on the left?
    MyContentWidget *contentWidget2 = new MyContentWidget(label);
    // a label describing my widget
    QLabel *label2 = new QLabel(contentWidget2);
    label2->setText("This is my minimal subclassed QWidget.\nSo what happened to its background colour?\nWhy is it not like the one on the left?\nWhat do I need to do to make it appear like the one on the left?");
    label2->setStyleSheet("color: black; background-color: white; border-radius: 0;");
    // get the show on the road
    return a.exec&#40;&#41;;


  • What happens in the constructor of you widget and how does your widget look like or what is the difference?

  • Hi butterface,

    The constructor for my widget is in the code posted above... currently it does nothing itself.

    MyContentWidget::MyContentWidget(QWidget *parent)
    : QWidget(parent)

    The difference from the default implementation is that is does not appear slightly darker like the default QWidget does. Also, It does not respond to changing the background colour with setStyleSheet() like the default one does.

    "Here is a screenshot of the output of the posted code" :

    As you can see the QWidget on the right does not have the same appearance as the one on the left. I expected it to appear as a similar darker area behind the text. It is the same shape and is defined in the same way as the one on the left. Why is it different?

    So I am interested to know member functions I must override to make it behave normally.

Log in to reply