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. Widget doesn't show inside another widget.
Forum Updated to NodeBB v4.3 + New Features

Widget doesn't show inside another widget.

Scheduled Pinned Locked Moved Solved General and Desktop
newbiewidgetno show
4 Posts 2 Posters 4.3k Views
  • 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.
  • qojaQ Offline
    qojaQ Offline
    qoja
    wrote on last edited by qoja
    #1

    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?

    raven-worxR 1 Reply Last reply
    1
    • qojaQ qoja

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      qojaQ 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @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?
        qojaQ Offline
        qojaQ Offline
        qoja
        wrote on last edited by
        #3

        @raven-worx , thx for your reply.

        1. Ok, I changed it.
        2. I made it without designer.
        raven-worxR 1 Reply Last reply
        0
        • qojaQ qoja

          @raven-worx , thx for your reply.

          1. Ok, I changed it.
          2. I made it without designer.
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @qoja
          then please show the complete code.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          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