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)  
    {    
        Widget::setMinimumSize(300,300);	
        QHBoxLayout *layout1 = new QHBoxLayout;
        Widget::setLayout(layout1);
    
        green_widget *gWidget = new green_widget;
        layout1->addWidget(gWidget);
    }
    

    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;");
    layout1->addWidget(test_widget);
    
    

    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;
    green_widget::setLayout(a_layout);
    a_layout->setMargin(0);
    a_layout->addWidget(a_widget);
    

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


  • Moderators

    @qoja

    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

    @qoja
    then please show the complete code.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.