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. Adding Widgets to widgets
Forum Update on Monday, May 27th 2025

Adding Widgets to widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 6.6k 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.
  • S Offline
    S Offline
    Swati777999
    wrote on 13 Dec 2021, 02:56 last edited by
    #1

    Is it possible to add widgets to a widget?

    Basically, widgets are added to the layout. I've done so in my coding but it does not give me the desired result.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    P J 2 Replies Last reply 13 Dec 2021, 04:07
    0
    • S Swati777999
      13 Dec 2021, 02:56

      Is it possible to add widgets to a widget?

      Basically, widgets are added to the layout. I've done so in my coding but it does not give me the desired result.

      P Online
      P Online
      Pl45m4
      wrote on 13 Dec 2021, 04:07 last edited by
      #2

      @Swati777999

      You cant add a widget to a widget directly.

      The widget's content is organized in layouts, where you can have multiple sub-widgets.

      You can set a widget as parent of another and show it (floating) within parent widget's boundings, but that's not really "adding"


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • S Swati777999
        13 Dec 2021, 02:56

        Is it possible to add widgets to a widget?

        Basically, widgets are added to the layout. I've done so in my coding but it does not give me the desired result.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 13 Dec 2021, 06:23 last edited by
        #3

        @Swati777999 said in Adding Widgets to widgets:

        but it does not give me the desired result.

        Then please show the code and tell us what your desired result is and what you get...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply 13 Dec 2021, 08:56
        0
        • J jsulm
          13 Dec 2021, 06:23

          @Swati777999 said in Adding Widgets to widgets:

          but it does not give me the desired result.

          Then please show the code and tell us what your desired result is and what you get...

          S Offline
          S Offline
          Swati777999
          wrote on 13 Dec 2021, 08:56 last edited by Swati777999
          #4

          @jsulm

               int n=20;
              QGridLayout *mSDLayout = new QGridLayout();
              QMap <int,QWidget*>dTWidgets;
              QMap <int, QLabel*>Names;
              QMap <int ,QTableWidget *> dTs;
          
              FlowLayout *flowLayout=new FlowLayout(); // Parent Layout
          
                for (int ii=0;ii<n;ii++)
                  {
          
                    dTWidgets[ii] =new QWidget();  
          
          //          mSDLayout->addWidget(dTWidgets[ii],0,ii);
                    mSDLayout->addWidget(dTWidgets[ii],0,ii,Qt::AlignCenter);
          
          
                    Names[ii] =new QLabel(QString("Name %1").arg(ii+1));
          
                    flowLayout->addWidget(Names[ii]);
                    flowLayout->setAlignment(Names[ii],Qt::AlignHCenter);
          
                    dTs[ii] = new QTableWidget(); 
                    flowLayout->addWidget(dTs[ii]);
                    dTWidgets[ii]->setLayout(flowLayout);
          

          This gives me following result [with inactive flowLayout functionality]
          SingleFLayout.PNG

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          J 1 Reply Last reply 13 Dec 2021, 08:59
          0
          • S Swati777999
            13 Dec 2021, 08:56

            @jsulm

                 int n=20;
                QGridLayout *mSDLayout = new QGridLayout();
                QMap <int,QWidget*>dTWidgets;
                QMap <int, QLabel*>Names;
                QMap <int ,QTableWidget *> dTs;
            
                FlowLayout *flowLayout=new FlowLayout(); // Parent Layout
            
                  for (int ii=0;ii<n;ii++)
                    {
            
                      dTWidgets[ii] =new QWidget();  
            
            //          mSDLayout->addWidget(dTWidgets[ii],0,ii);
                      mSDLayout->addWidget(dTWidgets[ii],0,ii,Qt::AlignCenter);
            
            
                      Names[ii] =new QLabel(QString("Name %1").arg(ii+1));
            
                      flowLayout->addWidget(Names[ii]);
                      flowLayout->setAlignment(Names[ii],Qt::AlignHCenter);
            
                      dTs[ii] = new QTableWidget(); 
                      flowLayout->addWidget(dTs[ii]);
                      dTWidgets[ii]->setLayout(flowLayout);
            

            This gives me following result [with inactive flowLayout functionality]
            SingleFLayout.PNG

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 13 Dec 2021, 08:59 last edited by
            #5

            @Swati777999 said in Adding Widgets to widgets:

            dTWidgets[ii]->setLayout(flowLayout);

            Why?!

            Your dTWidgets widgets should be set in flowLayout, not other way around.

            Also, why do you use QMap instead of QVector?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply 13 Dec 2021, 09:20
            3
            • J jsulm
              13 Dec 2021, 08:59

              @Swati777999 said in Adding Widgets to widgets:

              dTWidgets[ii]->setLayout(flowLayout);

              Why?!

              Your dTWidgets widgets should be set in flowLayout, not other way around.

              Also, why do you use QMap instead of QVector?

              S Offline
              S Offline
              Swati777999
              wrote on 13 Dec 2021, 09:20 last edited by Swati777999
              #6

              @jsulm

              Sorry to have put it here, this code was meant to be posted for FlowLayout Qs :
              For this Q

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              1 Reply Last reply
              0

              1/6

              13 Dec 2021, 02:56

              • Login

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