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. Finding the "snap point" for a splitter ?
Forum Updated to NodeBB v4.3 + New Features

Finding the "snap point" for a splitter ?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 2.2k Views 2 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.
  • S Offline
    S Offline
    SamiV123
    wrote on last edited by
    #1

    I have a layout where I have 3 widgets left, center and right laid out in a horizontal splitter (so they're from left to right side-by-side).

    The problem is with the initial size of the splitter. The left side is initially way too large.

    The splitter has a "snap" point that seems to be some kind of minimum acceptable size if you drag it smaller it'll completely close.

    How to find this snap point and adjust the splitter so that both the
    left and right sides are at the snap point and the center widget has maximum space?

    Thanks!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #12

      According to my experience, the "snap point" is indeed the minimum size, but not the value minimumSize() returned (unless you have set it), but minimumSizeHint() if you have layout set on the widget.
      Are you sure setStretchFactor() is not working? I think it should if you set the center widget's strech to 1 and leave the other two untouched (0). (Or you can just set the center widget's Horizontal Stretch to 1 if you are using designer.)

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

        Hi,

        Isn't it the minimal size of the widget ?

        That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Isn't it the minimal size of the widget ?

          That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.

          S Offline
          S Offline
          SamiV123
          wrote on last edited by
          #3

          @SGaist said in Finding the "snap point" for a splitter ?:

          Hi,

          Isn't it the minimal size of the widget ?

          That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.

          The minimum size is (width) is 0. So this can't be right.

          strech factor seems super obscure, doubt that will do anything other than make the situation worse.

          SGaistS 1 Reply Last reply
          0
          • S SamiV123

            @SGaist said in Finding the "snap point" for a splitter ?:

            Hi,

            Isn't it the minimal size of the widget ?

            That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.

            The minimum size is (width) is 0. So this can't be right.

            strech factor seems super obscure, doubt that will do anything other than make the situation worse.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @SamiV123 yes it can be. You can also set the minimum size to something suitable for you.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              @SamiV123 yes it can be. You can also set the minimum size to something suitable for you.

              S Offline
              S Offline
              SamiV123
              wrote on last edited by
              #5

              @SGaist said in Finding the "snap point" for a splitter ?:

              @SamiV123 yes it can be. You can also set the minimum size to something suitable for you.

              Well yes the minimum size of a widget can be 0 but in this case it doesn't make sense since the when the splitter is at the snap point the size (width) of the widget is non-zero. So the snap point can't be at a point where the widget's minimum size is zero.. Makes sense?

              Secondarily, using fixed minimum size rarely (if ever) works correctly across platforms and style engines. Using X units of width and it looks ok with Fusion or whatever? Yeah, great then go and change the style engine to kvAntum or Adwaita and see how things are all kaput because the layout and widget sizes are completely different.

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zbigniew-Sch
                wrote on last edited by
                #6

                Use the following function:
                pcoSplitter->setSizes({ iLeftViewWidth , iMiddleViewWidth, iRightViewWidth });

                and to your question:
                pcoSplitter->setSizes({ 0 , iMiddleViewWidth, 0 });

                S 1 Reply Last reply
                0
                • Z Zbigniew-Sch

                  Use the following function:
                  pcoSplitter->setSizes({ iLeftViewWidth , iMiddleViewWidth, iRightViewWidth });

                  and to your question:
                  pcoSplitter->setSizes({ 0 , iMiddleViewWidth, 0 });

                  S Offline
                  S Offline
                  SamiV123
                  wrote on last edited by
                  #7

                  @Zbigniew-Sch

                  And what are the sizes? Yeah.. that's basically the meat of the question.

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    Zbigniew-Sch
                    wrote on last edited by
                    #8

                    Each splitter part is connected to a widget (QLabel, QListWidget, QScrollArea ....)

                    pcoSplitter->addWidget(pcoLeftWidget);
                    int iLeftViewWidth = pcoLeftWidget->width();

                    pcoSplitter->addWidget(pcoMiddleWidget);
                    int iMiddleViewWidth = pcoMiddleWidget->width();

                    pcoSplitter->addWidget(pcoRightWidget);
                    int iRightViewWidth = pcoRightWidget->widht();

                    S 1 Reply Last reply
                    0
                    • Z Zbigniew-Sch

                      Each splitter part is connected to a widget (QLabel, QListWidget, QScrollArea ....)

                      pcoSplitter->addWidget(pcoLeftWidget);
                      int iLeftViewWidth = pcoLeftWidget->width();

                      pcoSplitter->addWidget(pcoMiddleWidget);
                      int iMiddleViewWidth = pcoMiddleWidget->width();

                      pcoSplitter->addWidget(pcoRightWidget);
                      int iRightViewWidth = pcoRightWidget->widht();

                      S Offline
                      S Offline
                      SamiV123
                      wrote on last edited by
                      #9

                      @Zbigniew-Sch said in Finding the "snap point" for a splitter ?:

                      Each splitter part is connected to a widget (QLabel, QListWidget, QScrollArea ....)

                      pcoSplitter->addWidget(pcoLeftWidget);
                      int iLeftViewWidth = pcoLeftWidget->width();

                      pcoSplitter->addWidget(pcoMiddleWidget);
                      int iMiddleViewWidth = pcoMiddleWidget->width();

                      pcoSplitter->addWidget(pcoRightWidget);
                      int iRightViewWidth = pcoRightWidget->widht();

                      If you think about this for a second you should realize this is completely illogical.

                      Let's say the parent widget that contains the splitter is 1000 units wide and has a splitter that has left pane 250, center 500 and right pane 250 units in width.

                      What would be the result of setting the splitter "sizes" (QSplitter::setSizes) to 0, 500, 0?

                      The space to be allotted between the three widgets is 1000. So the sizes would logically need to sum up to 1000. 0 + 500 + 0 sums to 500. Makes no sense.

                      1 Reply Last reply
                      0
                      • Z Offline
                        Z Offline
                        Zbigniew-Sch
                        wrote on last edited by
                        #10

                        I think you didn't understand the logic. You first have to determine the three widths of the widgets (when closing splitter).
                        If you reduce the left and right widgets to 0 (via GUI splitter widgets changes), the middle widget will give the total width of splitter (1000), and the next time you start the program you can use splitter values.
                        If you want to set the wide splitter widgets arbitrarily when initializing, then the following logic applies, example:

                        splitter->setSizes({20, 200, 300}) - total width of the splitter
                              corresponds to 520 units
                            - the left widget: (20/520) * spliter->width() - wide
                            - the middle widget : (200/520) * spliter->width() - wide
                            - the right widget: (300/520) * spliter->width() - wide
                        

                        and if I set splitter->setSizes({0, 500, 0}) it doesn't matter what I pass 200, 500 or 1000, the left and right
                        widgets have a width of 0 and the middle widget has the width of the entire splitter.

                        I hope the logic is understandable now

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SamiV123
                          wrote on last edited by
                          #11

                          So, no solution? :|

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            Bonnie
                            wrote on last edited by Bonnie
                            #12

                            According to my experience, the "snap point" is indeed the minimum size, but not the value minimumSize() returned (unless you have set it), but minimumSizeHint() if you have layout set on the widget.
                            Are you sure setStretchFactor() is not working? I think it should if you set the center widget's strech to 1 and leave the other two untouched (0). (Or you can just set the center widget's Horizontal Stretch to 1 if you are using designer.)

                            S 1 Reply Last reply
                            0
                            • Z Offline
                              Z Offline
                              Zbigniew-Sch
                              wrote on last edited by
                              #13

                              Use the QSplitterHandle class.
                              Integrate the object into the splitter and use "QSplitterHandle::mousePressEvent(QMouseEvent*
                              eventPress)" to realize the “snap point”.

                              1. Create your own class (QYourSplitterHandle) derived from "QSplitterHandle".
                              2. Implement:
                                void QYourSplitterHandle::mousePressEvent(QMouseEvent* eventPress)
                              3. Create spliiter handle in your splitter class (virtual):
                                virtual QSplitterHandle *createHandle();

                              Example:
                              QYourSplitterHandle* QYourSplitter::createHandle()
                              {
                              return new QYourSplitterHandle(Qt::Orientation::Horizontal, this);
                              }

                              Calculate the widths of splitter widgets attached to the current splitter and for the "snap-point"
                              do not call function from base class:
                              QSplitterHandle::mousePressEvent(eventPress);

                              Determine splitter handle (using splitter object):
                              QYourSplitterHandle* pcoSplitterHandle =
                              qobject_cast<QYourSplitterHandle*>(pcoSplitter->handle(0));

                              1 Reply Last reply
                              0
                              • Z Offline
                                Z Offline
                                Zbigniew-Sch
                                wrote on last edited by
                                #14

                                I forgot, sorry, you have to do the same with the following function:
                                void QYourSplitterHandle::mouseMoveEvent(QMouseEvent* event)

                                1 Reply Last reply
                                0
                                • B Bonnie

                                  According to my experience, the "snap point" is indeed the minimum size, but not the value minimumSize() returned (unless you have set it), but minimumSizeHint() if you have layout set on the widget.
                                  Are you sure setStretchFactor() is not working? I think it should if you set the center widget's strech to 1 and leave the other two untouched (0). (Or you can just set the center widget's Horizontal Stretch to 1 if you are using designer.)

                                  S Offline
                                  S Offline
                                  SamiV123
                                  wrote on last edited by
                                  #15

                                  @Bonnie

                                  Thanks, the horizontal stretch factor seems to get me closest to what I want.

                                  1 Reply Last reply
                                  0
                                  • S SamiV123 has marked this topic as solved on

                                  • Login

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