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. Visibility and Position

Visibility and Position

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 3.7k Views 1 Watching
  • 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.
  • W Offline
    W Offline
    Wurstinator
    wrote on last edited by
    #1

    Hello,
    I have three QPushButtons next to each other in a QHBoxLayout. The second button in the center is sometimes visible and sometimes now, but no matter which of those is currently true I want to keep the position of the other two buttons the same so they don't "hop around". How can I achieve this?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bomb3rman
      wrote on last edited by
      #2

      A QSpacerItem could help here. You have to set a size for your buttons and the QSpacerItem fills up the remaining space.
      http://doc.qt.nokia.com/4.7-snapshot/qboxlayout.html#insertStretch

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Wurstinator
        wrote on last edited by
        #3

        Could you maybe provide a small example code? I am having trouble especially with finding the correct size of the space.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          You could also set a minimum size for the buttons. The middle button will not disappear anymore. You might want to check out how the window handles this, because you get horizontal scroll bars automatically otherwise. You are also able to set maximum sizes and as bomb3rman says use the spacers to keep the buttons in the correct position on the screen.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Wurstinator
            wrote on last edited by
            #5

            I don't see how setting the minimum size would help me.

            And as I already answered to Bomb3rman, I need example code for the QSpacerItem because I have trouble finding the correct size. QPushButton::size() does not return the right value.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              raaghuu
              wrote on last edited by
              #6

              [quote author="Wurstinator" date="1344250755"]Could you maybe provide a small example code? I am having trouble especially with finding the correct size of the space.[/quote]

              First, use "setFixedSize":http://qt-project.org/doc/qt-5.0/qwidget.html#setFixedSize-2 on all your buttons using width and height as required then add them to a "Horizontal Box Layout":http://qt-project.org/doc/qt-4.8/QHBoxLayout.html along with a QSpacer via "addStretch":http://qt-project.org/doc/qt-4.8/qboxlayout.html#addStretch or "insertStretch":http://qt-project.org/doc/qt-4.8/qboxlayout.html#insertStretch

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raaghuu
                wrote on last edited by
                #7

                [quote author="Wurstinator" date="1344256193"] QPushButton::size() does not return the right value.[/quote]

                use "sizeHint":http://qt-project.org/doc/qt-5.0/qwidget.html#sizeHint-prop instead

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  Wurstinator
                  wrote on last edited by
                  #8

                  setFixedSize cannot be used because the Window can be resized and therefore the buttons need to adapt their size to it.

                  sizeHint doesn't work correctly either. I am trying to get the size exactly like it is displayed in the window.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raaghuu
                    wrote on last edited by
                    #9

                    You can just omit the setFixedSize then from my previous post and do the rest... But the problem with that is that if the window is too small, the button will dissappear as they get no space to stay on screen... To fix this, you should call "setMinimumSize":http://qt-project.org/doc/qt-4.8/qwidget.html#minimumSize-prop on the MainWindow such that it can accommodate the buttons properly... Then the user cannot resize beyond a certain point and so the buttons stay on screen and the QLayout takes care of their proper positioning and size

                    Also, you should put the stretches between the buttons, so that when the middle button goes invisible, the stretch covers it instead of the other button jumping back into its place(This is what you originally wanted, right?)

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Wurstinator
                      wrote on last edited by
                      #10

                      Yes, that is what I am trying to accomplish.
                      But I never worked with stretches, that's why I asked for example code. If I simply use insertStretch the two remaining buttons get reduced to their MinimumSize.

                      edit:
                      Well, it seems that I found a solution but I feel like it is inconvenient.
                      First, I have to use setStretch(1) on the three buttons when I create them. This is probably the major thing I have been missing before.
                      The reason why I don't like my current code is because I have to use setSpacing whenever the visibility changes.
                      If the button is visible, the Layout creates a space of 5 between each of the three buttons which gives a total space of 10.
                      If it is invisible, the Layout only creates a space of 5 between the first and the third button. So, to make it look the same, I have to manually duplicate the spacing attribute.

                      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