Dec-29-2019, 01:35 AM
I have a two-pane horizontal QSplitter that takes up the entire window. The right pane is unimportant for now. I just gave it a dummy QFrame. But the left pane contains a QScrollArea which contains a custom widget I'm trying to create. Right now the widget is just a white rectangle for simplicity sake.
Scrollbars will appear if the QSplitter is dragged smaller than the dimensions of the widget, but I also want the widget to expand to fill any excess space if the splitter is dragged larger than those dimensions.
How do I make my widget expand to fill any excess space?
Scrollbars will appear if the QSplitter is dragged smaller than the dimensions of the widget, but I also want the widget to expand to fill any excess space if the splitter is dragged larger than those dimensions.
How do I make my widget expand to fill any excess space?
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
#custom widget
class MyWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.color = QColor(255, 255, 255)
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.fillRect(event.rect(), QBrush(self.color))
painter.end()
def sizeHint(self):
return QSize(250, 600)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QWidget()
layout = QHBoxLayout()
splitter = QSplitter(Qt.Horizontal)
scroll = QScrollArea()
mywidget = MyWidget() #custom widget for left pane
right = QFrame() #dummy QFrame for right pane
right.setFrameShape(QFrame.StyledPanel)
scroll.setWidget(mywidget)
splitter.addWidget(scroll)
splitter.addWidget(right)
layout.addWidget(splitter)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
