February 8, 2012

Chromatix Chromatix
Lab Rat
10 posts

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

  1. #include <QSplitter>
  2. #include <QWidget>
  3.  
  4. MainWindow::MainWindow(QWidget *parent)
  5.     : QMainWindow(parent)
  6. {
  7.     QWidget *widget1 = new QWidget();
  8.     widget1->setStyleSheet("* { background: blue }");
  9.  
  10.     QSplitter *splitter = new QSplitter(Qt::Vertical);
  11.     splitter->addWidget(widget1);
  12.     splitter->addWidget(new QWidget());
  13.  
  14.     setCentralWidget(splitter);
  15. }

Thanks in advance.

6 replies

February 8, 2012

miroslav miroslav
Ant Farmer
228 posts

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

 Signature 

Mirko Boehm | .(JavaScript must be enabled to view this email address) | KDE e.V.
FSFE Fellow
Qt Certified Specialist

February 8, 2012

Chromatix Chromatix
Lab Rat
10 posts

miroslav wrote:
You can set a fixed height on widget1, that will prevent the splitter from resizing it.
Please!!!!!!

Chromatix wrote:
… (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) …

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

February 9, 2012

miroslav miroslav
Ant Farmer
228 posts

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?

 Signature 

Mirko Boehm | .(JavaScript must be enabled to view this email address) | KDE e.V.
FSFE Fellow
Qt Certified Specialist

February 9, 2012

Volker Volker
Robot Herder
5428 posts

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)

February 10, 2012

Chromatix Chromatix
Lab Rat
10 posts

miroslav wrote:
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.

Volker wrote:
* set QSizePolicy::Minimum on the “fixed” widget
  • set QSizePolicy::Expanding on the other widget(s)
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) ?)

May 13, 2012

Chromatix Chromatix
Lab Rat
10 posts

Hi everyone, the solution seems to be simpler than I thought. That’s already implemented. All I needed to do was:

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

 
  ‹‹ Embed editable resources      [solved] QLocale - with atof - why doesn’t it convert "3.3"? ››

You must log in to post a reply. Not a member yet? Register here!