Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I activate a layout on a widget that is not itself part of a larger layout?



  • I am creating a QGroupBox which has a QGridLayout which contains two child widgets. I am then setting the parent of the QGroupBox to be a QFrame which (for historical reasons) does not have a layout. The problem is that the QGroupBox doesn't fit itself at all well to its contents - it is way too small, and the child widgets are themselves shrunk down (the QLabel, which is initially 100x30, shrinks to 68x0 when added to the layout). I suspect this is happening because the QGroupBox isn't part of a larger layout. How do I tell the QGroupBox (or its QGridLayout) to fit itself nicely to its child widgets?

    Thanks,
    Chris

    @QGroupBox* pBox = new QGroupBox(szName, this);

    QPixmap targetPmp("://ButtonRight-24x24-NoCircle.png");
    QIcon targetIcon(targetPmp);

    QLabel* pVolumeLabel = new QLabel("Volume: 38 mm3", pBox);

    QPushButton* pMenuButton = new QPushButton(targetIcon, "", pBox);
    pMenuButton->setIconSize(targetPmp.size());
    pMenuButton->setFixedSize(30, 30);
    rMenuButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    QGridLayout* pLayout = new QGridLayout(pBox);
    pLayout->setContentsMargins(0, 0, 0, 0);
    pLayout->setSpacing(1);
    pLayout->addWidget(pVolumeLabel, 0, 0);
    pLayout->addWidget(pMenuButton, 0, 1);
    pBox->setLayout(pLayout);

    pBox->setParent(pFrame);
    @



  • I guess you need to handle the size of QGroupBox by yourself.
    As "Layout Management":http://qt-project.org/doc/qt-5/layout.html doc states
    "The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space."


  • Lifetime Qt Champion

    Hi,

    Maybe a call to adjustSize ?

    Hope it helps


Log in to reply