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. How do I activate a layout on a widget that is not itself part of a larger layout?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 906 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.
  • C Offline
    C Offline
    cscooper
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      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."

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

        Hi,

        Maybe a call to adjustSize ?

        Hope it helps

        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

        • Login

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