QListWidget display more items - python

Here is a picture of my GUI:
I want to display all 100 items in my list widget without an inner scroll bar (there is an outer scroll bar, so there is no issue that I cannot fit all the items).
I have tried disabling the scroll bar for the list widget, but that didn't increase the number of items the list widget was displaying.
Here is my code:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
dlg = QDialog()
listWidget = QListWidget()
for i in range(100):
listWidget.addItem(QListWidgetItem("Item " + str(i)))
layout1 = QVBoxLayout()
layout1.addWidget(QLabel("Label 1"))
groupBox1 = QGroupBox("Group 1")
groupBox1.setLayout(layout1)
layout2 = QVBoxLayout()
layout2.addWidget(listWidget)
groupBox2 = QGroupBox("Group 2")
groupBox2.setLayout(layout2)
nestedWidgetLayout = QVBoxLayout()
nestedWidgetLayout.addWidget(groupBox1)
nestedWidgetLayout.addWidget(groupBox2)
nestedWidget = QWidget()
nestedWidget.setLayout(nestedWidgetLayout)
scrollArea = QScrollArea()
scrollArea.setWidget(nestedWidget)
mainLayout = QVBoxLayout()
mainLayout.addWidget(scrollArea)
dlg.setLayout(mainLayout)
dlg.show()
app.exec()

The #a_manthey_67 solution gives us a starting point but has several limitations:
It is calculated for a specific number of items so if items are added/deleted it will fail.
Manually set the height of each item instead of obtaining the height set by the style.
Considering the above, I have implemented a similar logic using sizeHintForRow(), in addition to enabling the widgetResizable property of the QScrollArea and disabling the verticalScrollBar.
import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import (
QApplication,
QDialog,
QGroupBox,
QLabel,
QListWidget,
QListWidgetItem,
QScrollArea,
QVBoxLayout,
QWidget,
)
class ListWidget(QListWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.model().rowsInserted.connect(self._recalcultate_height)
self.model().rowsRemoved.connect(self._recalcultate_height)
#pyqtSlot()
def _recalcultate_height(self):
h = sum([self.sizeHintForRow(i) for i in range(self.count())])
self.setFixedHeight(h)
if __name__ == "__main__":
app = QApplication(sys.argv)
dlg = QDialog()
listWidget = ListWidget()
for i in range(100):
listWidget.addItem(QListWidgetItem("Item " + str(i)))
layout1 = QVBoxLayout()
layout1.addWidget(QLabel("Label 1"))
groupBox1 = QGroupBox("Group 1")
groupBox1.setLayout(layout1)
layout2 = QVBoxLayout()
layout2.addWidget(listWidget)
groupBox2 = QGroupBox("Group 2")
groupBox2.setLayout(layout2)
nestedWidget = QWidget()
nestedWidgetLayout = QVBoxLayout(nestedWidget)
nestedWidgetLayout.addWidget(groupBox1)
nestedWidgetLayout.addWidget(groupBox2)
scrollArea = QScrollArea(widgetResizable=True)
scrollArea.setWidget(nestedWidget)
mainLayout = QVBoxLayout(dlg)
mainLayout.addWidget(scrollArea)
dlg.show()
sys.exit(app.exec_())

if the height of listwidget is bigger than the height of all all items all items are shown in listWidget but no scrollbar (of listWidget). in this snippet height of items is set by item.sizeHint() and the needed height of listwidget calculated 10 px bigger than needed for all items. sizeHint() needs QSize as parameter.
listWidget = QListWidget()
lineHeight = 20
items = 100
for i in range(items):
item = QListWidgetItem("Item " + str(i)) # get every item to set sizeHint()
item.setSizeHint(QSize(-1, lineHeight)) # -1 = width undefined
listWidget.addItem(item)
listWidget.setFixedHeight(items*lineHeight + 10) # set fixed height of listwidget

Related

QGridlLayout with non-stretchable-height rows

I'm trying to build a QGridLayout with stretchable-width columns but non-stretchable-height rows. The grid in inside a QScrollArea, and with the exception of the height, it's almost working. You can see it in the following images:
As you can see, the rows are being vertically stretched. I would like all the rows to be equal and to not fit all the parent's height if there are too few rows (first image). Should I touch the grid or the actual widgets?
Edit: reproducible example
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QLabel, QRadioButton, QApplication, QScrollArea, QVBoxLayout)
class ScrollableGrid(QWidget):
def __init__(self, columnSpans, minimumColumnWidth):
super().__init__()
# Grid
self.grid = QWidget()
self.gridLayout = QGridLayout()
for i in range(len(columnSpans)):
self.gridLayout.setColumnStretch(i, columnSpans[i])
self.gridLayout.setColumnMinimumWidth(i, columnSpans[i] * minimumColumnWidth)
self.grid.setLayout(self.gridLayout)
# Scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidget(self.grid)
self.scrollArea.setWidgetResizable(True)
# Compute the correct minimum width
width = (self.grid.sizeHint().width() +
self.scrollArea.verticalScrollBar().sizeHint().width() +
self.scrollArea.frameWidth() * 2)
self.scrollArea.setMinimumWidth(width)
# Layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.scrollArea)
self.setLayout(self.layout)
def addRow(self, row, elements):
for column in range(len(elements)):
self.gridLayout.addWidget(elements[column], row, column)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# ScrollableGrid
self.grid = ScrollableGrid(columnSpans=[1,2,3], minimumColumnWidth=100)
# Add rows
for i in range(3):
self.grid.addRow(i, [QLabel('A'), QLabel('B'), QRadioButton()])
# Window layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.grid)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = MainWindow()
windowExample.show()
sys.exit(app.exec_())
void QGridLayout::setRowStretch(int row, int stretch)
Sets the stretch factor of row row to stretch. The first row is number 0.
The stretch factor is relative to the other rows in this grid. Rows with a higher stretch factor take more of the available space.
Yes, is it because it must me called after all rows have been added.
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QLabel, QRadioButton,
QApplication, QScrollArea, QVBoxLayout)
class ScrollableGrid(QWidget):
def __init__(self, columnSpans, minimumColumnWidth):
super().__init__()
# Grid
self.grid = QWidget()
self.gridLayout = QGridLayout()
for i in range(len(columnSpans)):
self.gridLayout.setColumnStretch(i, columnSpans[i])
self.gridLayout.setColumnMinimumWidth(i, columnSpans[i] * minimumColumnWidth)
self.grid.setLayout(self.gridLayout)
# Scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidget(self.grid)
self.scrollArea.setWidgetResizable(True)
# Compute the correct minimum width
width = (self.grid.sizeHint().width() +
self.scrollArea.verticalScrollBar().sizeHint().width() +
self.scrollArea.frameWidth() * 2)
self.scrollArea.setMinimumWidth(width)
# Layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.scrollArea)
self.setLayout(self.layout)
def addRow(self, row, elements):
for column in range(len(elements)):
self.gridLayout.addWidget(elements[column], row, column)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# ScrollableGrid
self.grid = ScrollableGrid(columnSpans=[1,2,3], minimumColumnWidth=100)
# Add rows
for i in range(3):
self.grid.addRow(i, [QLabel('A'), QLabel('B'), QRadioButton()])
self.grid.gridLayout.setRowStretch(111, 1) # !!!
# Window layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.grid)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = MainWindow()
windowExample.show()
sys.exit(app.exec_())

Aligning popup widget in PyQt5

I've seen a number of replies on SO regarding this matter but not specifically to QMenu and QToolButton. Would appreciate some pointers on aligning the popup widget to the right side of the button. Here's a basic code I'm working off..
import sys
from PyQt5.QtWidgets import *
class test(QWidget):
def __init__(self):
super().__init__()
self.resize(200, 100)
layout = QHBoxLayout(self)
label = QLabel('Testing QToolButton Popup')
toolbutton = QToolButton()
toolbutton.setPopupMode(QToolButton.InstantPopup)
widget = QWidget()
widgetLayout = QHBoxLayout(widget)
widgetLabel = QLabel('Popup Text')
widgetSpinbox = QSpinBox()
widgetLayout.addWidget(widgetLabel)
widgetLayout.addWidget(widgetSpinbox)
widgetAction = QWidgetAction(toolbutton)
widgetAction.setDefaultWidget(widget)
widgetMenu = QMenu(toolbutton)
widgetMenu.addAction(widgetAction)
toolbutton.setMenu(widgetMenu)
layout.addWidget(label)
layout.addWidget(toolbutton)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = test()
win.show()
sys.exit(app.exec_())
The outcome looks like this:
The Qt developer thought the default position was correct, so if you want to modify the alignment you must move the QMenu as I show below:
import sys
from PyQt5.QtCore import QPoint
from PyQt5.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMenu,
QSpinBox,
QToolButton,
QWidgetAction,
QWidget,
)
class Menu(QMenu):
def showEvent(self, event):
if self.isVisible():
button = self.parentWidget()
if button is not None:
pos = button.mapToGlobal(button.rect().bottomRight())
self.move(pos - self.rect().topRight())
super().showEvent(event)
class Test(QWidget):
def __init__(self):
super().__init__()
self.resize(200, 100)
layout = QHBoxLayout(self)
label = QLabel("Testing QToolButton Popup")
toolbutton = QToolButton(popupMode=QToolButton.InstantPopup)
widgetLabel = QLabel("Popup Text")
widgetSpinbox = QSpinBox()
widget = QWidget()
widgetLayout = QHBoxLayout(widget)
widgetLayout.addWidget(widgetLabel)
widgetLayout.addWidget(widgetSpinbox)
widgetAction = QWidgetAction(toolbutton)
widgetAction.setDefaultWidget(widget)
widgetMenu = Menu(toolbutton)
widgetMenu.addAction(widgetAction)
toolbutton.setMenu(widgetMenu)
layout.addWidget(label)
layout.addWidget(toolbutton)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Test()
win.show()
sys.exit(app.exec_())

Get color from variable number of buttons PyQt

I'm trying to get the player's names and the color that they choose for a next step in the project. Getting the names is easy enough but the colors is a bit of a pain.
I don't know how to get the selected color and apply it the button using the QColorDialog. The ultimate goal is to get a structure with player name linked to the color they chose.
Here is what i have:
from PyQt5.QtWidgets import (
QLineEdit,
QWidget,
QApplication,
QLabel,
QMainWindow,
QGridLayout,
QColorDialog,
QPushButton,
QVBoxLayout,
QHBoxLayout,
)
import sys
class NamesPlayers(QMainWindow):
""" name screen, ask the names of the players """
def __init__(self, nb_players):
super().__init__()
self.layout_widget = QWidget()
self.player_names = []
self.player_colors = []
main_layout = QVBoxLayout()
names_layout = QGridLayout()
button_layout = QHBoxLayout()
button_list = []
for i in range(nb_players):
label = QLabel("Name :")
player_name = QLineEdit()
color_button = QPushButton("Color")
color_button.setStyleSheet("background-color: white")
names_layout.addWidget(label, i, 0)
names_layout.addWidget(player_name, i, 1)
names_layout.addWidget(color_button, i, 2)
button_list.append(color_button)
self.player_names.append(player_name)
self.player_colors.append(color_button.styleSheet())
self.confirm_button = QPushButton("Confirm")
button_layout.addWidget(self.confirm_button)
main_layout.addLayout(names_layout)
main_layout.addLayout(button_layout)
for button in button_list:
button.clicked.connect(self.open_colordialog)
self.layout_widget.setLayout(main_layout)
self.setCentralWidget(self.layout_widget)
def open_colordialog(self):
color_dialog = QColorDialog()
color_dialog.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = NamesPlayers(4)
window.show()
app.exec_()
My main issue i guess is the fact that the number of buttons for the color can vary and when I click on those buttons, the address for the QColorDialog is always the same which is not what I want.
Any help is appreciated
One possible solution is to use the sender() method to get the button pressed:
import sys
from PyQt5 import QtCore, QtWidgets
class NamesPlayers(QtWidgets.QMainWindow):
""" name screen, ask the names of the players """
def __init__(self, nb_players, parent=None):
super().__init__(parent)
names_layout = QtWidgets.QGridLayout()
for i in range(nb_players):
label = QtWidgets.QLabel("Name :")
player_name = QtWidgets.QLineEdit()
color_button = QtWidgets.QPushButton("Color")
color_button.setStyleSheet("background-color: white")
color_button.clicked.connect(self.open_colordialog)
names_layout.addWidget(label, i, 0)
names_layout.addWidget(player_name, i, 1)
names_layout.addWidget(color_button, i, 2)
self.confirm_button = QtWidgets.QPushButton("Confirm")
central_widget = QtWidgets.QWidget()
main_layout = QtWidgets.QVBoxLayout(central_widget)
button_layout = QtWidgets.QHBoxLayout()
button_layout.addWidget(self.confirm_button)
main_layout.addLayout(names_layout)
main_layout.addLayout(button_layout)
self.setCentralWidget(central_widget)
#QtCore.pyqtSlot()
def open_colordialog(self):
button = self.sender()
color_dialog = QtWidgets.QColorDialog()
if color_dialog.exec_() == QtWidgets.QColorDialog.Accepted:
button.setStyleSheet(
"background-color: {}".format(color_dialog.selectedColor().name())
)
button.clearFocus()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = NamesPlayers(4)
window.show()
sys.exit(app.exec_())
Another possible solution is to send the button pressed (with the methods indicated in this answer):
from functools import partial
# ...
for i in range(nb_players):
# ...
color_button = QtWidgets.QPushButton("Color")
color_button.setStyleSheet("background-color: white")
color_button.clicked.connect(partial(self.open_colordialog, color_button))
# or
# color_button.clicked.connect(lambda *args, btn=color_button: self.open_colordialog(btn))
def open_colordialog(self, button):
color_dialog = QtWidgets.QColorDialog()
if color_dialog.exec_() == QtWidgets.QColorDialog.Accepted:
button.setStyleSheet(
"background-color: {}".format(color_dialog.selectedColor().name())
)
button.clearFocus()

How to get the right table row index in pyQt5 contextMenuEvent

I need to get the table row index during a contextMenuEvent in pyQt5, but I keep getting odd row offsets. Since this is my first pyQt project, I'm quite lost. I have the following code.
def contextMenuEvent(self, event):
menu = QMenu(self)
openAction = menu.addAction('Open in browser')
action = menu.exec_(event.globalPos())
if action == openAction:
row = self.tableWidget.rowAt(event.y()) # Should this be event.globalY?
self.so_something(row)
Edit: adding code sample. You can see that when the execution gets to the event produced when right-clicking on a row, the printed number is not the correct row number. Usually it has an offset, and last rows produce -1.
import sys
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QLabel, QLineEdit, QWidget, \
QPushButton, QVBoxLayout, QHBoxLayout, QComboBox, \
QTableWidget, QHeaderView, QTableWidgetItem, \
QMenu, QApplication
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Window size and title
self.setWindowTitle('Test')
self.resize(700, 700)
self.setMinimumWidth(700)
# Label
addressLabel = QLabel('Testing')
addressLabel.setAlignment(Qt.AlignCenter)
# Dropdown
self.addressDropdown = QComboBox(self)
self.addressDropdown.addItem('A')
self.addressDropdown.addItem('B')
# Refresh button
refreshButton = QPushButton()
refreshButton.setMaximumSize(40, 40)
# Address layout
addressayout = QHBoxLayout()
addressayout.addWidget(addressLabel, 1)
addressayout.addWidget(self.addressDropdown, 7)
addressayout.addWidget(refreshButton)
# Balance label
balaceLabel = QLabel()
balaceLabel.setText('Testing')
balaceLabel.setAlignment(Qt.AlignCenter)
# Balance amount label
self.balaceAmountLabel = QLabel()
self.balaceAmountLabel.setText('Testing')
self.balaceAmountLabel.setAlignment(Qt.AlignCenter)
# Balance layout
balanceLayout = QVBoxLayout()
balanceLayout.addWidget(balaceLabel)
balanceLayout.addWidget(self.balaceAmountLabel)
# Transactions label
transactionsLabel = QLabel('Testing')
transactionsLabel.setAlignment(Qt.AlignCenter)
# Transactions table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(1)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setVisible(False)
self.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
item = QTableWidgetItem('Testing')
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignHCenter)
self.tableWidget.setItem(0, 0, item)
# Transactions layout
transactionsLayout = QVBoxLayout()
transactionsLayout.addWidget(transactionsLabel)
transactionsLayout.addWidget(self.tableWidget)
# Send label A
sendLabelA = QLabel('Send')
sendLabelA.setAlignment(Qt.AlignCenter)
# Send amount
self.sendAmount = QLineEdit()
self.sendAmount.setAlignment(Qt.AlignCenter)
# Send label B
sendLabelB = QLabel('to')
sendLabelB.setAlignment(Qt.AlignCenter)
# Send address
self.sendAddress = QLineEdit()
self.sendAddress.setAlignment(Qt.AlignCenter)
# Send button
sendButton = QPushButton()
sendButton.setMaximumSize(40, 40)
sendIcon = QIcon.fromTheme("mail-send")
sendButton.setIcon(sendIcon)
sendButton.setIconSize(QSize(24,24))
# Send layout
sendLayout = QHBoxLayout()
sendLayout.addWidget(sendLabelA)
sendLayout.addWidget(self.sendAmount, 2)
sendLayout.addWidget(sendLabelB)
sendLayout.addWidget(self.sendAddress, 4)
sendLayout.addWidget(sendButton)
# Window layout
layout = QVBoxLayout()
layout.addLayout(addressayout)
layout.addLayout(balanceLayout)
layout.addLayout(transactionsLayout)
layout.addLayout(sendLayout)
self.setLayout(layout)
def contextMenuEvent(self, event):
menu = QMenu(self)
openAction = menu.addAction('Open in browser')
action = menu.exec_(event.globalPos())
row = self.tableWidget.rowAt(event.y())
if action == openAction:
print(row)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
The coordinate that rowAt() method requires must be with respect to the viewport of the QTableWidget so you must convert the global position to local using mapFromGlobal() method:
def contextMenuEvent(self, event):
gp = event.globalPos()
menu = QMenu(self)
openAction = menu.addAction("Open in browser")
action = menu.exec_(gp)
vp_pos = self.tableWidget.viewport().mapFromGlobal(gp)
row = self.tableWidget.rowAt(vp_pos.y())
if action == openAction:
print(row)

How set two scroll bars (vertical and horizontal) to the same widget in PyQt environment?

I'm trying to make two scroll bars for a QGroupBox but I only succeed having one (the vertical one)
I'm not sure what I need to do.
here is a short example of my code:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralTabs= QTabWidget()
self.setCentralWidget(self.centralTabs)
self.setFixedWidth(200)
self.setFixedHeight(200)
#tab Model selection
self.tab_ModelSelect = QWidget()
self.centralTabs.addTab(self.tab_ModelSelect,"Label")
self.groupscrolllayouttest = QHBoxLayout()
self.groupscrollbartest = QGroupBox()
self.mainHBOX_param_scene = QVBoxLayout()
for i in range(10):
Label = QLabel('BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla')
Label.setFixedWidth(200)
self.mainHBOX_param_scene.addWidget(Label)
#
scroll = QScrollArea()
scroll.setWidget(self.groupscrollbartest)
scroll.setWidgetResizable(True)
scroll.setFixedWidth(20)
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.groupscrollbartest.setLayout(self.mainHBOX_param_scene)
self.groupscrolllayouttest.addWidget(self.groupscrollbartest)
self.groupscrolllayouttest.addWidget(scroll)
self.tab_ModelSelect.setLayout(self.groupscrolllayouttest)
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.setWindowTitle('window')
# ex.showMaximized()
ex.show()
sys.exit(app.exec_( ))
if __name__ == '__main__':
main()
and here is the result:
In my more complex code I used QTabWidget, that why I included it in this example. What I would like to do is having a horizontal scrollbar to the bottom which allows me to shift the text left and right. Obviously I want keep the other one to shift the text up and down.
I also try to add a second scroll bar to the first one (groupscrolllayouttest)
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralTabs= QTabWidget()
self.setCentralWidget(self.centralTabs)
self.setFixedWidth(200)
self.setFixedHeight(200)
#tab Model selection
self.tab_ModelSelect = QWidget()
self.centralTabs.addTab(self.tab_ModelSelect,"Label")
self.groupscrolllayouttest2 = QVBoxLayout() ####
self.groupscrollbartest2 = QGroupBox() ####
self.groupscrolllayouttest = QHBoxLayout() ####
self.groupscrollbartest = QGroupBox() ####
self.mainHBOX_param_scene = QVBoxLayout()
for i in range(10):
Label = QLabel('BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla')
Label.setFixedWidth(200)
self.mainHBOX_param_scene.addWidget(Label)
#
scroll = QScrollArea()
scroll.setWidget(self.groupscrollbartest)
scroll.setWidgetResizable(True)
scroll.setFixedWidth(20)
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
# self.mainHBOX_param_scene.addWidget(scroll)
self.groupscrollbartest.setLayout(self.mainHBOX_param_scene)
self.groupscrolllayouttest.addWidget(self.groupscrollbartest)
self.groupscrolllayouttest.addWidget(scroll)
scroll2 = QScrollArea()
scroll2.setWidget(self.groupscrollbartest2)
scroll2.setWidgetResizable(True)
scroll2.setFixedWidth(20)
scroll2.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll2.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.groupscrollbartest2.setLayout(self.groupscrolllayouttest)
self.groupscrolllayouttest2.addWidget(self.groupscrollbartest2)
self.groupscrolllayouttest2.addWidget(scroll2)
self.tab_ModelSelect.setLayout(self.groupscrolllayouttest2)
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.setWindowTitle('window')
# ex.showMaximized()
ex.show()
sys.exit(app.exec_( ))
if __name__ == '__main__':
main()
But I end up with a strange scroll bar:
So now I'm stuck. Any idea?
What you have to do is create a widget, and in that widget place the QGroupBox:
[...]
scroll = QScrollArea()
widget = QWidget(self)
widget.setLayout(QVBoxLayout())
widget.layout().addWidget(self.groupscrollbartest)
scroll.setWidget(widget)
scroll.setWidgetResizable(True)
self.groupscrollbartest.setLayout(self.mainHBOX_param_scene)
self.groupscrolllayouttest.addWidget(scroll)
self.tab_ModelSelect.setLayout(self.groupscrolllayouttest)
[...]
Output:

Categories

Resources