0
votes

I have a QMainWindow with a horizontal layout as central widget. The horizontal layout contains three widgets. Here is the code:

this.navigationTree=new NavigationTree();
Browser browser = new Browser();
this.clusteringPlot=new ClusteringPlot();
QWidget centralWidget=new QWidget();
QHBoxLayout layout=new QHBoxLayout();
layout.addWidget(navigationTree);
layout.addWidget(clusteringPlot);
layout.addWidget(browser);
centralWidget.setLayout(layout);
this.setCentralWidget(centralWidget);`

where Browser is a QWebView.

Whenever I resize the main window Browser does not get resized while clusteringPlot(extends QWidget) and navigationTree(extends QTreeWidget) do.

How can I change my code so that also Browser get resized? I took a look at similar questions but I could not find a solution to my problem.

Thanks a lot in advance Rossella

1
The right way to do this depends on how you want the extra space you're making available to be assigned. Do you want the extra space divided between your widgets in equal proportions? Would you like the space assigned to just one of your widgets? Maybe you want clusteringPlot to get a third of the extra space, while browser gets the remaining two-thirds? If you could provide a bit more information, I'm happy to provide a more detailed answer :)sam-w
Ideally I would like NavigationTree to have 1/4 of the space, ClusteringPlot to have 1/4 and browser to have the remaining 1/2. The problem I have is that if I make browser smaller than a certain size (1024x648) instead of having the web page resized I get a scrollable area which I would like to avoidRossella

1 Answers

0
votes

A layout queries the minimum size of its children when deciding how to assign space. Your first task therefore is to ensure that all of the widgets occupying your QHBoxLayout know what their minimum useful size is:

  • Since NavigationTree inherits QTreeWidget, I'm assuming that it is already supplying a sensible minimum size (via the minimumSizeHint() inherited from QAbstractScrollArea)). If this needs altering, reimplement QWidget::minimumSizeHint() as below.
  • If ClusteringPlot inherits directly from QWidget, it's probably not providing the layout with a sensible minimum size, so you need to provide one. You can either reimplement QWidget::minimumSizeHint() as a virtual function in ClusteringPlot (especially useful if you're able to calculate the size based on the state of the object) or call clusteringPlot->setMinimumSize(...);.
  • Since you are dictating the minimum size for your QWebView, call browser->setMinimumSize(1024, 768);.

Once enough space in has been assigned to satisfy each of its child widget's minimumSizeHint()s, a QBoxLayout starts assigning remaining space according to the stretchFactor of each widget. The easiest way to set stretch factors is when adding the widgets to the layout. Try changing your code to:

layout.addWidget(navigationTree, 1);
layout.addWidget(clusteringPlot, 1);
layout.addWidget(browser, 2);

This tells the layout that any spare space should be assigned in the ratio 1:1:2, i.e. if 40 extra pixels become available, 10 go to navigationTree, 10 to clusteringPlot and 20 to browser.