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

auto-resizing window based on content



  • Hi all -

    I'm implementing a QWidget that will contain a column of radio buttons. The column will be populated from an enum:

    enum PatternID
    {
        PATTERN_UNKNOWN,
        PATTERN_SOLID_OFF,
        PATTERN_SOLID_ON,
        PATTERN_CYCLE_SLOW,
        PATTERN_CYCLE_RAPID,
        PATTERN_BLIP_SLOW,
        PATTERN_BLOOM,
        NBR_PATTERNS
    };
    

    My .ui layout looks like this:
    0_1564695826193_buttons.PNG

    As currently implemented, my UI looks like this at runtime:
    0_1564695894647_buttons2.PNG

    Which is a bit crowded. Obviously, I could just increase the height of the widget in Designer, but I'm wondering whether there's a way to do this that will automatically alter the size if I add or delete buttons.

    Thanks...



  • you might want to put that panel in a scrollbox instead of resizing the top level window. Keep in mind that you will need to track the window size relative to the available desktop geometry if you go your chosen route. Certinaly doable you way, but is it worth the hassle when a scrolled region of radio buttons works too?



  • It depends on how much hassle it is; that's what I'm hoping to find out.

    When I said "a column" that was a fib; there will actually be two columns. The display might look a little busy if both have scrollbars. But...it could work, and if my idea is truly a big deal to do, then this might be preferred.


  • Lifetime Qt Champion

    @mzimmers From where does this "LED pattern" come? I don't see it in your UI file.
    To me it looks like it is not in the layout.
    If you add it at runtime you could simply put an empty widget into your layout, which will be small without content and then use this widget as parent for "LED pattern".



  • @jsulm you're correct; I add LED pattern at runtime:

    DeviceTest::DeviceTest(ModelDevice *md, QModelIndex *qmi, Worker *pWorker, QWidget *parent) 
    {
        ui->setupUi(this);
    
        // initialize objects for the radio buttons.
        m_buttonGroupLed = new QButtonGroup;
        m_boxLayoutLed = new QVBoxLayout();
        m_groupBoxLed = new QGroupBox("LED pattern", this);
    

    So, following your suggestion: what kind of widget should I add to my layout? I tried a list widget, and it works, though I'm not sure it's the ideal widget for this. And I assume that I give it an expanding vertical policy, right?

    Thanks...



  • I'm seeing some unexpected behavior. Here's my code for setting up the widgets:

        m_groupBoxLed = new QGroupBox("LED pattern", ui->listWidget);
        m_groupBoxBuzzer = new QGroupBox("buzzer pattern", ui->listWidget);
    
        // position the group boxes.
        qRect = new QRect(20, 20, 150, 200);
        m_groupBoxLed->setGeometry(*qRect);
        int width = this->width();
        qRect->setLeft(width / 2);
    qRect->setLeft(150);
        m_groupBoxBuzzer->setGeometry(*qRect)
    

    (The second setLeft() call is to demonstrate one of the problems.)

    And here's what I'm seeing:
    0_1564764578501_widgets.PNG

    It appears that my second group box isn't being permitted to render outside the area of the first group box.

    Other issues: I'm not getting enough height to show all the buttons. I'd also prefer a non-white background, but this might just be a matter of selecting a different widget.

    Thanks...

    EDIT: I discovered the cause of my first problem: I misunderstood the second coordinates in a QRect. I thought they were an offset, but they're an actual position (as I'm sure most of you know already). So, I had to add the first coordinates to them to get what I wanted:

        // position the group boxes.
        qRect = new QRect(20, 20, 150, 200);
        m_groupBoxLed->setGeometry(*qRect);
    
        // compute the x positions for the second box.
        width = this->width() / 2;
        left = qRect->left() + width;
        right = qRect->right() + width;
        qRect->setLeft(left);
        qRect->setRight(right);
        m_groupBoxBuzzer->setGeometry(*qRect);
    

    (I know I should get rid of the magic numbers in the code above.)

    0_1564780368797_widgets.PNG


  • Lifetime Qt Champion

    @mzimmers Why do you use setGeometry() instead of putting everything into a layout?



  • @jsulm I could do that. What do you recommend I do for correctly sizing the button boxes? If I create an empty layout in Designer, and do this (and this only):

        this->ui->layoutPatterns->addWidget(m_groupBoxLed);
        this->ui->layoutPatterns->addWidget(m_groupBoxBuzzer);
    

    Then my window looks like this:
    0_1565020355432_testbuttons.PNG

    It would be very nice if the button boxes and their corresponding sliders had their widths synchronized, but that's not necessary.

    Thanks...


  • Lifetime Qt Champion

    Hi,

    It looks like each paire of slider/list view should be in their own vertical layout and then these two layouts should be store inside am horizontal layout.



  • Hi SG - that did the trick. It looks a little weird in Designer, since the list layouts are almost invisible:

    0_1565037317102_designer.PNG
    but when the program run, it looks like I wanted it to:
    0_1565037334082_running.PNG

    Thanks for the help, all.


Log in to reply