Python Forum
[PyQt] dynamically resize custom widget fill remaining space
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] dynamically resize custom widget fill remaining space
#1
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_())
Reply
#2
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 reasons
1) 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 too

Next 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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,396 Aug-02-2021, 08:47 PM
Last Post: JayCee
  how to resize image in canvas tkinter samuelmv30 2 17,559 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,747 May-06-2020, 06:11 PM
Last Post: karolp
  [Tkinter] Dynamically Deleting Widget smabubakkar 1 3,354 Feb-15-2020, 11:11 PM
Last Post: Larz60+
  [PySimpleGUI] error trying to resize Text element skratt 3 8,000 Dec-10-2019, 06:05 PM
Last Post: FullOfHelp
  [Tkinter] Display Selected Image from Directory and Resize it EchoLi 0 4,198 Oct-02-2019, 06:54 PM
Last Post: EchoLi
  [Tkinter] not resize a frame Scorpio 2 3,488 Mar-13-2019, 11:24 PM
Last Post: Scorpio
  [PyQt] Resize button with window resize FesterJester 2 9,690 Dec-03-2018, 12:02 AM
Last Post: FesterJester
  [WxPython] Dynamically Sized Widget CanadaGuy 3 3,866 Nov-12-2018, 02:55 AM
Last Post: Larz60+
  How to Integrate PyQt4 Custom Widget with Qt Designer Zukias 1 3,884 Aug-29-2018, 05:33 PM
Last Post: Zukias

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020