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.

    @#include <QSplitter>
    #include <QWidget>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    QWidget widget1 = new QWidget();
    widget1->setStyleSheet("
    { background: blue }");

    QSplitter *splitter = new QSplitter(Qt::Vertical);
    splitter->addWidget(widget1);
    splitter->addWidget(new QWidget());
    
    setCentralWidget(splitter);
    

    }@

    Thanks in advance.



  • You can set a fixed height on widget1, that will prevent the splitter from resizing it.



  • [quote author="miroslav" date="1328707547"]You can set a fixed height on widget1, that will prevent the splitter from resizing it. [/quote]Please!!!!!!

    [quote author="Chromatix" date="1328701973"]... (Please note, [color=red]I don't want the list to be Fixed Size[/color], 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) ...[/quote]

    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?



  • I didn't test it, but I would give this a try:

    • set QSizePolicy::Minimum on the "fixed" widget
    • set QSizePolicy::Expanding on the other widget(s)


  • [quote author="miroslav" date="1328775495"]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? [/quote]Then why don't I subclass the very QSplitter and QSplitterHandle? Details of this are explained well in the documentation.

    [quote author="Volker" date="1328787032"]* set QSizePolicy::Minimum on the "fixed" widget

    • set QSizePolicy::Expanding on the other widget(s)[/quote]No change.

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



  • Hi @Chromatix ,
    Thanks for your answer & you guided me in the right direction.
    This does seem to work but i have a huge application in which there are nested splitters inside main splitter (a horizontal inside a vertical splitter etc).
    After setting stretch factors of fixed size widgets to "0" & others to "1", i find all widgets inside a splitter are flickering when i move a handle to change size of some widgets. I also find it sticky to move the handle which used to work normal before this change.

    Does this ring any bell to anyone.?

    Thanks in advance.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.