[PyQt] dynamically resize custom widget fill remaining space - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] dynamically resize custom widget fill remaining space (/thread-23410.html) |
dynamically resize custom widget fill remaining space - ironcthulhu - Dec-29-2019 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? 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_()) RE: dynamically resize custom widget fill remaining space - Denni - Dec-31-2019 Okay first sys.exit(app.exec_()) this is PyQt4 and this is app.exec() is PyQt5 This app = QApplication(sys.argv) is basically wrong for 2 reasons1) You are not using Command Line arguments in this program 2) If you were using Command Line arguments those are better handled by argparser library So the above should be MainEventThread = QApplication([]) also I changed it from app to MainEventThread because that is what it represents and it is always best to be reminded of that and to tell others that is what it is.Passing the parent in this QWidget.__init__(self, parent) especially since your parent = None but regardless understand what you are doing and why or do not do it. The fact that you do not seem to be using it at all implies to me that you do not know why you are even doing beyond the fact that the other guy jumped out of the plane without a parachute so you did tooNext you need to understand what the QScrollArea functionality does -- which I cannot tell you as I have not used it yet. However I can tell you this it shuts off the dynamic resizing of QWidget which makes sense if a Box can resize you do not need scroll bars. So the question is do you want a dynamic box for static box with scroll bars? As you cannot have both. This is not to say you cannot code a static box to seem semi-dynamic but that really opens a whole can of worms that I personally would not want to mess with. So here is your cleaned up program and I want to applaud you for not using super( ) that was one bullet you dodge ;-)Oh and I added a few label and a lineedit so I could see what was occurring a bit better USE_PYSIDE2 = False if USE_PYSIDE2: from PySide2.QtCore import Qt, QSize, Slot from PySide2.QtGui import QColor, QPainter, QBrush from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout from PySide2.QtWidgets import QLineEdit, QLabel else: from PyQt5.QtCore import pyqtSlot as Slot from PyQt5.QtCore import Qt, QSize from PyQt5.QtGui import QColor, QPainter, QBrush from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout from PyQt5.QtWidgets import QLineEdit, QLabel, QFrame, QSplitter class MyWidget(QWidget): def __init__(self, parent): QWidget.__init__(self) self.color = QColor(255, 255, 255) self.Parent = parent self.lneHere = QLineEdit('Over Here Is Better') layout = QHBoxLayout() layout.addWidget(self.lneHere) self.setLayout(layout) def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.fillRect(event.rect(), QBrush(self.color)) painter.end() def sizeHint(self): return QSize(250, 300) class Window(QWidget): def __init__(self): QWidget.__init__(self) self.lblHere = QLabel('Here We Are') FrmBox = QHBoxLayout() FrmBox.addWidget(self.lblHere) self.mywidget = MyWidget(self) # custom widget for left pane # scroll = QScrollArea() # scroll.setWidget(self.mywidget) self.frmRite = QFrame() # dummy QFrame for right pane self.frmRite.setFrameShape(QFrame.StyledPanel) self.frmRite.setLayout(FrmBox) splitter = QSplitter(Qt.Horizontal) splitter.addWidget(self.mywidget) splitter.addWidget(self.frmRite) layout = QHBoxLayout() layout.addWidget(splitter) self.setLayout(layout) if __name__ == '__main__': MainEventThread = QApplication([]) MainApp = Window() MainApp.show() MainEventThread.exec() |