This picture illustrate the problem. It is a dialog with a QScrollArea
in it. The area has a QWidget
, using a QGridLayout
with 3 columns containing mutliple QRadioButtons
. But you see only 2 columns not 3.
But I want to have it look like this. The dialog and its child do expand to its "full width" defined by its content (the three columns of radio buttons). No need to horizontal scroll anymore. The vertical scrolling is OK and I want to keep it that way.
Here is an MWE reproducing the first picture.
#!/usr/bin/env python3
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class LanguageDialog(QDialog):
def __init__(self):
super().__init__()
# Language widget
wdg_lang = self._language_widget()
# Scroll area
scroll = QScrollArea(self)
# scroll.setWidgetResizable(True)
# scroll.setFrameStyle(QFrame.NoFrame)
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# Here I'm not sure what to do.
# scroll.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
scroll.setWidget(wdg_lang)
# Button "Apply"
button = QDialogButtonBox(QDialogButtonBox.Apply)
button.clicked.connect(self.accept)
# Dialog layout
layout = QVBoxLayout(self)
layout.addWidget(scroll)
layout.addWidget(button)
def _language_widget(self):
"""
nativ | ownlocal -> tooltip: english (code)
"""
grid = QGridLayout()
wdg = QWidget(self)
wdg.setLayout(grid)
for col in range(1, 4):
for row in range(1, 24):
r = QRadioButton(f'{col} x {row} | Lorem ipsum dolor sit amet', self)
r.toggled.connect(self.slot_radio)
r.mydata = (col, row)
grid.addWidget(r, row, col)
# ???
# wdg.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
return wdg
def slot_radio(self, val):
btn = self.sender()
if btn.isChecked():
print(f'{btn.mydata=}')
class Window(QMainWindow):
def __init__(self, parent=None):
"""Initializer."""
super().__init__(parent)
def showEvent(self, e):
dlg = LanguageDialog()
dlg.exec()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())