0

I have a PySide6 Qt screen that looks like this:
enter image description here

The problem is that I want the buttons "0 test1.rtf," "1 test2.rtf," "2 test3.rtf," etc... to populate at the top left of the scroll area without dynamic spacing in between them which incorrectly makes the buttons fill up the entire area.

See how the spacing between the buttons dynamically changes when I add buttons:
enter image description here

I want it to look like this:
enter image description here

I have the following code for my PySide6 Qt screen:

wid = QtWidgets.QWidget()
grid = QtWidgets.QVBoxLayout(wid)

# setting the inner widget and layout
grid_inner = QtWidgets.QVBoxLayout(wid)
wid_inner = QtWidgets.QWidget(wid)
wid_inner.setLayout(grid_inner)

# add the inner widget to the outer layout
grid.addWidget(wid_inner)

# add tab frame to widget
wid_inner.tab = QtWidgets.QTabWidget(wid_inner)
grid_inner.addWidget(wid_inner.tab)

# create tab
new_tab = QtWidgets.QScrollArea(wid_inner.tab)
grid_tab_1 = QtWidgets.QVBoxLayout(new_tab)
new_tab.tab_name_private = "test1"
wid_inner.tab.addTab(new_tab, "test1")
for idx, tx in enumerate(self.log):
    fname_array = (tx.file_name).split('/')
    btn = QtWidgets.QPushButton(str(idx) + ' ' + fname_array[(len(fname_array)) - 1])
    btn.clicked.connect(lambda checked=False, x=idx: self.display_transaction(x))
    grid_tab_1.addWidget(btn)

# create tab 2
new_tab2 = QtWidgets.QScrollArea(wid_inner.tab)
grid_tab_2 = QtWidgets.QVBoxLayout(new_tab2)
wid_inner.tab.addTab(new_tab2, "test2")
for idx, tx in enumerate(self.log):
    fname_array = (tx.file_name).split('/')
    btn = QtWidgets.QPushButton(str(idx) + ' ' + fname_array[(len(fname_array)) - 1])
    btn.clicked.connect(lambda checked=False, x=idx: self.display_transaction(x))
    grid_tab_2.addWidget(btn)

continue_btn = QtWidgets.QPushButton("Ok")

self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(wid)
self.layout.addWidget(continue_btn)
self.setLayout(self.layout)

continue_btn.clicked.connect(self.continue_to_main)

How do I alter my code to get the screen to look the way I want?

ChristianOConnor
  • 820
  • 7
  • 29
  • 1
    Use `layout.setAlignment(Qt.AlignLeft|Qt.AlignTop)`. But note that you're using the scroll area in the wrong way. You do **not** set a layout for it, you create a QWidget container and set the layout for *that* widget, then call [`setWidget()`](https://doc.qt.io/qt-5/qscrollarea.html#setWidget). – musicamante Mar 13 '23 at 22:44
  • Oh awesome! Can you alter my code to set the layout for the correct widget in an answer? I keep getting breaking errors when I alter the setLayout code. – ChristianOConnor Mar 13 '23 at 22:55
  • @musicamante I did this: https://gist.github.com/ChristianOConnor/24626d28b4a12c45012279718eab3626. Note lines 17-19 and lines 29-31. This seems to work a lot better, but now the buttons are a little too high up and overflowing over the top of the scroll area. Is there a way to fix this? https://imgur.com/a/v6bc5c7 – ChristianOConnor Mar 13 '23 at 23:35
  • 1
    You're still *not* using `setWidget()` as written above (please, *do* read the documentation). Also, don't create widgets and layouts with parents as argument if those arguments are not the final ones (see [this post](https://stackoverflow.com/q/30354166)), in fact you usually don't need to manually specify a parent of a widget if it's going to be a child that will be managed by a layout. Only use the parent argument for a layout **if** that layout will be the main layout for that widget. – musicamante Mar 13 '23 at 23:53
  • Does this answer your question? [Scroll Area - set widget / layout?](https://stackoverflow.com/questions/55620637/scroll-area-set-widget-layout) – relent95 Mar 14 '23 at 05:13
  • Actually yes, I was able to solve the `setWidget` problem through a similar means. Thank you @relent95 ! As for the original question of putting the buttons on the top right of the scroll area, I fixed that with `tab.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignTop)` if @musicamante wants to make that into an answer I'll accept it – ChristianOConnor Mar 14 '23 at 05:50
  • Why not answer your own question? – relent95 Mar 14 '23 at 06:01

0 Answers0