[SOLVED] QGridLayout has no effect



  • 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


  • Lifetime Qt Champion

    Hi,

    Please show the code where you use QGridLayout


  • Moderators

    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.



  • 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);
    

    }@


  • Moderators

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


Log in to reply
 

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