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.

Widget doesn't show inside another widget.

Scheduled Pinned Locked Moved Solved General and Desktop
newbiewidgetno show
4 Posts 2 Posters 4.1k 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.
  • Q Offline
    Q Offline
    qoja
    wrote on 1 Jun 2016, 13:24 last edited by qoja 6 Jan 2016, 13:24
    #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?

    R 1 Reply Last reply 1 Jun 2016, 13:47
    1
    • Q qoja
      1 Jun 2016, 13:24

      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?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 1 Jun 2016, 13:47 last edited by raven-worx 6 Jan 2016, 13:47
      #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

      Q 1 Reply Last reply 1 Jun 2016, 14:19
      2
      • R raven-worx
        1 Jun 2016, 13:47

        @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?
        Q Offline
        Q Offline
        qoja
        wrote on 1 Jun 2016, 14:19 last edited by
        #3

        @raven-worx , thx for your reply.

        1. Ok, I changed it.
        2. I made it without designer.
        R 1 Reply Last reply 2 Jun 2016, 06:34
        0
        • Q qoja
          1 Jun 2016, 14:19

          @raven-worx , thx for your reply.

          1. Ok, I changed it.
          2. I made it without designer.
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 2 Jun 2016, 06:34 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

          2/4

          1 Jun 2016, 13:47

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved