[Solved] How to make the size of a widget in a QSplitter fixed when resizing the QSplitter?
Hello. After lots of struggling, I managed to make my complex nested layout (God saved me!), but I have a minor problem.
(At the simplest form) My environment is made up of a text editor, and a list widget at the top. I put them in a vertical QSplitter to let user resize the panes. But when you resize the main window, the QSplitter resizes both widgets proportionally, which is what I don’t want. I want the list editor’s height to remain the same when the main window is resizing. (Please note, I don’t want the list to be Fixed Size, indeed it should be resizable solely by the splitter’s handle, and I want it to hold its height unchanged when user resizes the main window)
I tried setting all the available size policies, overriding resizeEvent of the main window, and using splitter’s splitterMoved signal to somehow set the height with no luck. I don’t show what I tried here because they’re both long and may mislead future answers (I don’t think they were even near to answer though).
Here is a minimum situation showing the problem. I want widget1’s height unchanged when you resize the main window.
Thanks in advance.
Please!!!!!! You can set a fixed height on widget1, that will prevent the splitter from resizing it.
I don’t want the list to be Fixed Size, indeed it should be resizable solely by the splitter’s handle, and I want it to hold its height unchanged when user resizes the main window) …… (Please note,
What I want is not to prevent splitter from resizing it, I want it to keep its height fixed when user resizes the main window (i.e when the entire splitter area is resized).
I thought of setting the height fixed “while user is holding the mouse click and resizing the window”, but there is no way of determining when the resizing starts and when it stops so it was impossible (for me?).
For the widget to keep it’s size when the main window is resized, it will have to have some kind of fixed height, since the other size policies manage size relatively to the policies and sizes of other widgets in the same direction.
For the widget in the splitter to be resizable, the splitter needs to see a size policy that allows the widget to grow and shrink. I am quite certain it will not recognize this if, for example, a Preferred size policy is set only when the mouse button is pressed on the splitter, since the layout will process the update later after the mouse event is finished.
Maybe QSplitter is not exactly what you want. How about adding a simple handle widget to the corner of widget1 that resizes it when dragged with the mouse?
Then why don’t I subclass the very QSplitter and QSplitterHandle? Details of this are explained well in the documentation. Maybe QSplitter is not exactly what you want. How about adding a simple handle widget to the corner of widget1 that resizes it when dragged with the mouse?
No change. * set QSizePolicy::Minimum on the “fixed” widget
- set QSizePolicy::Expanding on the other widget(s)
There is really no way of telling QSplitter to resize its widgets non-proportionally? It is already done in Qt Creator: when you resize the main window, the size of left pane and bottom pane doesn’t change; only the text area grows or shrinks. I’m going to take a look at its source codes.
As for subclassing the QSplitter, I think I need small modifications to the handle’s code. I’d have no experience with version controls, can you tell me where can I see the QSplitter.cpp and QSplitterHangle.cpp files online? (Or whatever file that implements the code for QSplitter(.h) and QSplitterHandle(.h) ?)
Hi everyone, the solution seems to be simpler than I thought. That’s already implemented. All I needed to do was:
- QSplitter::setStretchFactor ( int index, int stretch )
I had to set the stretch factor of the part I wanted to remain fixed size 0 and set it to 1 on the other widget. That way when we resize the entire window, the widget with the stretch factor 0 won’t resize. :)