3

Is there a way to set the width of a QScrollArea so that a horizontal scrollbar is not needed? I know I can hide horizontal scrollbar by setting it's policy to always off. However, I want to make the QScrollArea large enough to not need it.

I'm currently somewhat doing this by using scrollbar.setFixedWidth(). Is there a better way, with something like scrollbar.setSizetoContents() or something similar?

As a side note, what is the best way to get the maximum width of all the widgets in a layout anyway?

NorthCat
  • 9,643
  • 16
  • 47
  • 50
durden2.0
  • 9,222
  • 9
  • 44
  • 57

2 Answers2

3

It looks like QScrollArea.setWidgetResizable could do what you want. But that will probably depend on what sort of widgets the scroll-area contains.

Also see the section on Size Hints and Layouts.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • 1
    setWidgetResizable doesn't seem to change anything in my scenario. I'm guessing it's because of the underlying widgets in the scroll area. – durden2.0 Jan 30 '12 at 18:51
  • 1
    @durden2.0. What widgets are in your scroll-area? It would help if you added some example code to your question which illustrates what you are trying to achieve. – ekhumoro Jan 30 '12 at 18:56
  • 1
    Sorry, it's a complicated layout. I'll try to boil it down to the essentials, I have a bunch of Qlabels organized in a QVBoxLayout in the scroll area. The scroll area is then part of a QHboxLayout with another HBoxLayout. So the width of the scroll area defaults to half the size of the main HBoxLayout. I would like to set a fixed width for the scroll area that defaults to the width of the maximum QLabel. Thus, no horizontal scrollbar would be needed. – durden2.0 Jan 30 '12 at 19:06
  • 2
    @durden2.0. Did you read the section I linked to on size-hints and layouts? Given the complexity of your scroll-area's contents, it seems inevitable that you will have to do the resize calculations yourself. What's wrong with your current solution? Does it work at all? – ekhumoro Jan 30 '12 at 20:30
  • 1
    I'm actually reading your link and some relevant sections in the Rapid GUI Programming with Python and Qt. My current implementation works, but sometimes it doesn't show the full width of the scroll area. I would like to find a good way to see the widths of all the widgets in a layout. I guess I need to do this on the resize event? – durden2.0 Jan 30 '12 at 21:39
2

I finally figured it out. Here is the hackish code I was using to find out the width of one of my sub-widgets in the scrollarea:

def _restrictWidthToFit(self):
    layout = self._scroll.widget().layout()
    if layout.count() > 0:
        fw = layout.itemAt(0).widget().sizeHint().width()
        sw = self._scroll.verticalScrollBar().sizeHint().width()
        w = fw + sw
        return w

The trick was I was previously setting this width when first adding the widgets into the scrollarea. However, the sizes aren't completely filled out by Qt at this point apparently. Thus, I used the above method to set the width by overriding the showEvent():

def showEvent(self, ev):
    super(UIFilterCollection, self).showEvent(ev)
    self._scroll.setFixedWidth(self._restrictWidthToFit())

At this point the width is filled out so it seems to work.

It's also worth noting that all of my sub-widgets are the same width already since they are in a

Chnossos
  • 9,971
  • 4
  • 28
  • 40
durden2.0
  • 9,222
  • 9
  • 44
  • 57
  • Great. Adapted your solution for another [anwser](https://stackoverflow.com/a/76738806/4865723). – buhtz Jul 21 '23 at 14:20
  • I have to add something. Using `sowEvent()` seems to be an exception just in your case (without knowing the full code). In case it is no problem to calculate the width from the scrollareas child widgets and its scrollbar while `__init__()` is running. – buhtz Jul 22 '23 at 18:07