Important: Please read the Qt Code of Conduct -

Widget doesn't show inside another widget.

  • Hi,
    I created new project (Qt Widgets Application) with base class QWidget without a form.
    Then I created new C++ class green_widget (Base class: QWidget).
    Now I have 2 .h files (widget.h and green_widget.h) and 3 .cpp files (main.cpp, widget.cpp and green_widget.cpp).

    I want to place green_widget inside widget. Kind of square inside another square. My widget.cpp file is:

    Widget::Widget(QWidget *parent) : QWidget(parent)  
        QHBoxLayout *layout1 = new QHBoxLayout;
        green_widget *gWidget = new green_widget;

    The only change I made in green_widget.cpp is "green_widget::setStyleSheet("background-color: green;");".
    When I run my app I don't see green_widget.

    If I add another widget to widget.cpp file it takes half of window so green_widget is there but I don't see it.

    QWidget *test_widget = new QWidget;
    test_widget->setStyleSheet("background-color: red;");

    The only solution I found so far is to add new widget inside "green_widget.cpp".

    QWidget *a_widget = new QWidget;
    QHBoxLayout *a_layout = new QHBoxLayout;

    Then it shows correctly. But it doesn't look right for me.
    What did I do wrong?

  • Moderators


    1. dont call member methods with the classname prepended. You only (mostly) call static methods this way.
    2. do you have any changes (attributes, properties) set in the designer for your widget?

  • @raven-worx , thx for your reply.

    1. Ok, I changed it.
    2. I made it without designer.

  • Moderators

    then please show the complete code.

Log in to reply