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. [SOLVED] QGridLayout has no effect

[SOLVED] QGridLayout has no effect

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    alaink
    wrote on last edited by
    #1

    Hello,

    I have made some custom widgets and I try to put them in a QGridLayout. If I put only one of them in it, everything works fine. But if I add a second one, the QGridLayout doesn't do any positioning. They are both on top of each other. If I output the pos() of both widgets, they are both (0, 0). How can that be? Is there something I have to implement in my widgets so that they can be properly arranged by a layout? I also tried other layouts such as QVBoxLayout, it's the same behavior.

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please show the code where you use QGridLayout

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Sounds like you put the widgets in layout but didn't assign that layout to any container. Layouts don't have an effect if they are not set on a widget.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alaink
          wrote on last edited by
          #4

          I assigned the layout to a widget, otherwise nothing at all would be displayed.

          That's the code where I use QGridLayout:

          @void MyWindow::arrangePlugins(void)
          {
          qint32 x, y, n;

          x = 0;
          y = 0;
          
          setCentralWidget(new QWidget(this));
          QGridLayout grid;
          
          n = 0;
          
          foreach(RCCPlugin *plugin, plugins)
          {
              QWidget *w = plugin->w();
              grid.addWidget(w, y, x);
              x++;
              n += w->height();
          
              if(n > (qint32)(0.8 * QApplication::desktop()->screenGeometry().height()))
              {
                  n = 0;
                  x++;
                  y = 0;
              }
          }
          
          centralWidget()->setLayout(&grid);
          

          }@

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ QGridLayout grid; @
            This is a local variable.
            Goes out of scope and gets destroyed here:
            @ } @
            Use dynamic allocation (new).

            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