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. auto-resizing window based on content

auto-resizing window based on content

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 671 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        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.

        jsulmJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          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.

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          mzimmersM 1 Reply Last reply
          2
          • jsulmJ jsulm

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

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by mzimmers
              #6

              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

              jsulmJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                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

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

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

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

                    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.

                    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
                    3
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      1

                      • Login

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