PyQt: Adding widgets to scrollarea during the runtime - qt

I'm trying to add new widgets (in the example below I use labels) during the runtime by pressing on a button. Here the example:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent= None):
super(Widget, self).__init__()
btn_new = QPushButton("Append new label")
self.connect(btn_new, SIGNAL('clicked()'), self.add_new_label)
#Container Widget
self.widget = QWidget()
#Layout of Container Widget
layout = QVBoxLayout(self)
for _ in range(20):
label = QLabel("test")
layout.addWidget(label)
self.widget.setLayout(layout)
#Scroll Area Properties
scroll = QScrollArea()
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(False)
scroll.setWidget(self.widget)
#Scroll Area Layer add
vLayout = QVBoxLayout(self)
vLayout.addWidget(btn_new)
vLayout.addWidget(scroll)
self.setLayout(vLayout)
def add_new_label(self):
label = QLabel("new")
self.widget.layout().addWidget(label)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Widget()
dialog.show()
app.exec_()
When I start the application everything looks ok, the list of labels is correctly shown and their size is also correct. But, when I press several times on the button to add new labels, the new ones are added to the list but their size change. All labels of the list go smaller.
How do I fix this error?

The problem is the line:
scroll.setWidgetResizable(False)
which obviously stops the widget resizing when you add more child widgets to it (and so they all get squashed together in the same space).
So reset it to True and add a stretchable space to the bottom of the widget's layout:
layout.addStretch()
self.widget.setLayout(layout)
...
scroll.setWidgetResizable(True)
scroll.setWidget(self.widget)
then insert the new labels before the spacer:
def add_new_label(self):
label = QLabel("new")
layout = self.widget.layout()
layout.insertWidget(layout.count() - 1, label)

Related

Custom title bar with dockable toolbar

I want to create a custom title bar for my PyQt application, and my application uses dockable toolbars. To be dockable, the toolbars should be added to the MainWindow. However, with my custom toolbar being a Widget added to a frameless window, the toolbars dock themselves around the title bar. They can be docked above the title bar, and whenever docked on the sides, they push the title bar, which is not the expected behavior. I understand that this is due to the fact the the toolbar areas are always around the central widget of the window, and my custom title bar is inside the central widget. However, I don't see how I can make this work the way I want. Here is a MWE (I'm using PyQt=5.12.3) :
import sys
from typing import Optional
from PyQt5.QtCore import Qt, QSize, QPoint
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QPushButton, QWidget, QToolBar, QHBoxLayout, QLabel, \
QToolButton
class CustomTitleBar(QWidget):
def __init__(self, title: str, parent: Optional[QWidget] = None):
super().__init__(parent=parent)
self.window_parent = parent
layout = QHBoxLayout()
self.setObjectName("CustomTitleBar")
self.setLayout(layout)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.setFixedHeight(40)
self.title = title
self.title_label = QLabel(self.title)
self.title_label.setObjectName("TitleBarLabel")
layout.addWidget(self.title_label)
layout.addStretch(1)
but_minimize = QToolButton()
but_minimize.setText("🗕")
but_minimize.setObjectName("MinimizeButton")
layout.addWidget(but_minimize)
but_minimize.clicked.connect(self.window().showMinimized)
self.but_resize = QToolButton()
if self.window().isMaximized():
self.but_resize.setText("🗗")
else:
self.but_resize.setText("🗖")
layout.addWidget(self.but_resize)
self.but_resize.clicked.connect(self.toggle_maximized)
self.but_resize.setObjectName("ResizeButton")
but_close = QToolButton()
but_close.setText("🗙")
layout.addWidget(but_close)
but_close.clicked.connect(self.window().close)
but_close.setObjectName("CloseButton")
self.m_pCursor = QPoint(0, 0)
self.moving = False
def toggle_maximized(self):
if self.window().isMaximized():
self.but_resize.setText("🗖")
self.window().showNormal()
else:
self.but_resize.setText("🗗")
self.window().showMaximized()
def mousePressEvent(self, event: QMouseEvent) -> None:
pass
def mouseDoubleClickEvent(self, event: QMouseEvent) -> None:
pass
def mouseMoveEvent(self, event: QMouseEvent) -> None:
pass
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
pass
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowSystemMenuHint)
self.resize(QSize(800, 600))
main_widget = QWidget(self)
self.setCentralWidget(main_widget)
layout = QVBoxLayout(self)
main_widget.setLayout(layout)
titlebar = CustomTitleBar("Custom TitleBar Test Window", self)
layout.addWidget(titlebar)
layout.addWidget(QPushButton("Hello world"))
layout.addStretch(1)
my_toolbar = QToolBar(self)
self.addToolBar(Qt.RightToolBarArea, my_toolbar)
my_toolbar.addWidget(QPushButton("A"))
my_toolbar.addWidget(QPushButton("B"))
my_toolbar.addWidget(QPushButton("C"))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
The resulting window is as follow:
How can I get the dockable toolbars to behave around my custom title bar the way they should behave around a standard title bar ?
Since the title bar should be put outside the standard contents, you cannot put it inside the central widget.
The solution is to setContentsMargins() using the height of the title bar for the top margin. Then, since the title bar is not managed by any layout, you need to resize it by overriding the resizeEvent():
class MainWindow(QMainWindow):
def __init__(self):
# ...
self.titlebar = CustomTitleBar("Custom TitleBar Test Window", self)
self.setContentsMargins(0, self.titlebar.sizeHint().height(), 0, 0)
def resizeEvent(self, event):
super().resizeEvent(event)
self.titlebar.resize(self.width(), self.titlebar.sizeHint().height())
As I guess, firstly you need to look QDockWidget Because you put your CustomTitleBar into centeralWidget in MainWindow and it states in Docking area, this is expected behaviour.
You need to create a Vertical Layout which would your , and put CustomTitleBar into it, you don't need MainWindow in your code. In main you can try something like that:
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
layout = QHBoxLayout()
titlebar = CustomTitleBar("Custom TitleBar Test Window", self)
layout.addWidget(titlebar)
window.setLayout(layout)
main_widget = QWidget(self)
layout.addWidget(main_widget)
main_widget_layout = QVBoxLayout()
main_widget.setLayout(main_widget_layout)
my_toolbar = QToolBar(self)
main_widget.addToolBar(Qt.RightToolBarArea, my_toolbar)
my_toolbar.addWidget(QPushButton("A"))
my_toolbar.addWidget(QPushButton("B"))
my_toolbar.addWidget(QPushButton("C"))
main_widget.addWidget(QPushButton("Hello world"))
main_widget_layout.addStretch(1)
layout.addStretch(1)
window.show()
app.exec_()

How to update QScrollArea size when contents change size?

My QScrollArea does not update its size dinamically when I add a new QPushButton inside it.
I want to add/remove some QPushButton inside a QScrollArea dinamically, but my QScrollArea does not update
its size.
I want my QScrollArea has always a minimum possible size.
With this code:
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow,
QPushButton, QScrollArea, QVBoxLayout)
class MyWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
scroll = QScrollArea(self)
scroll.setWidgetResizable(True)
# Contents
w = QWidget()
lay = QVBoxLayout(w)
lay.addWidget(QPushButton('Button'))
scroll.setWidget(w)
# Controls
button_add = QPushButton('Add')
button_add.clicked.connect(lambda: lay.addWidget(QPushButton('Button')))
button_del = QPushButton('Del')
button_del.clicked.connect(lambda: lay.takeAt(0).widget().deleteLater() if lay.count()>0 else None)
# Main Layout
vlay = QVBoxLayout()
vlay.addWidget(scroll)
vlay.addStretch()
vlay.addWidget(button_add)
vlay.addWidget(button_del)
w = QWidget(self)
w.setLayout(vlay)
self.setCentralWidget(w)
self.resize(200, 300)
if __name__ == '__main__':
app = QApplication([])
mainWin = MyWindow()
mainWin.show()
sys.exit(app.exec_())
I got this view (left) when started and (right) when I add some QPushButtons:
So I have two questions:
How I start my application with QScrollArea with a minimum size?
How QScrollAre can update its size dinamically?
My desirable view is:
And of course, when I add a lot of QPushButtons, a Scrollbar appears.
You can set maximum height for scrollarea equals to widget contents (layout size + margins). This should be done after layout completes it's calculations (for example asyncronously with zero-timer).
import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QScrollArea, QVBoxLayout
class MyWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
scroll = QScrollArea(self)
scroll.setWidgetResizable(True)
# Contents
w = QWidget()
lay = QVBoxLayout(w)
#lay.addWidget(QPushButton('Button'))
scroll.setWidget(w)
def updateSize():
left, top, right, bottom = lay.getContentsMargins()
hint = lay.sizeHint()
scroll.setMaximumHeight(hint.height() + top + bottom + 1)
def addButton():
lay.addWidget(QPushButton('Button'))
QTimer.singleShot(0, updateSize)
def removeButton():
if lay.count() > 0:
lay.takeAt(0).widget().deleteLater()
QTimer.singleShot(0, updateSize)
addButton()
# Controls
button_add = QPushButton('Add')
button_add.clicked.connect(addButton)
button_del = QPushButton('Del')
button_del.clicked.connect(removeButton)
# Main Layout
vlay = QVBoxLayout()
vlay.addWidget(scroll)
vlay.addStretch(1)
vlay.addWidget(button_add)
vlay.addWidget(button_del)
vlay.setStretch(0,1000)
w = QWidget(self)
w.setLayout(vlay)
self.setCentralWidget(w)
self.resize(200, 300)
if __name__ == '__main__':
app = QApplication([])
mainWin = MyWindow()
mainWin.show()
app.exec()

python qt : automatically resizing main window to fit content

I have a main window which contains a main widget, to which a vertical layout is set. To the layout is added a QTableWidget only (for the moment).
When I start the application and call show on the main_window, only part of the QTableWidget is shown. I can extend the window manually to see it all, but I would like the window to have its size nicely adapted to the size of the QTableWidget.
Googling the question found a lot of posts on how to use resize to an arbitrary size, and call to resize(int) works fine, but this is not quite what I am asking
Lots of other posts are not explicit enough, e.g "use sizePolicy" or "use frameGeometry" or "use geometry" or "use sizeHint". I am sure all of them may be right, but an example on how to would be awesome.
You can do something like this, from within your MainWindow after placing all the elements you need in the layout:
self.setFixedSize(self.layout.sizeHint())
This will set the size of the MainWindow to the size of the layout, which is calculated using the size of widgets that are arranged in the layout.
I think overriding sizeHint() on the QTableWidget is the key:
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget
class Table(QTableWidget):
def sizeHint(self):
horizontal = self.horizontalHeader()
vertical = self.verticalHeader()
frame = self.frameWidth() * 2
return QSize(horizontal.length() + vertical.width() + frame,
vertical.length() + horizontal.height() + frame)
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
top = Table(3, 5, self)
self.setCentralWidget(top)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
You can use sizeHint() but not as stated in the other answers. sizeHint() returns a QSize object with a width and height. Let's say you have a main window mainWindow and a widget inside it called content. If your resizing involves content height to get bigger, you can fit the mainWindow to it like this:
mainWindow.resize(mainWindow.sizeHint().width,
mainWindow.size().height() + content.sizeHint().height());
Old but i experienced this a while back and seeing how the answers here didn't exactly work for me.
Here's what i did:
Please make sure you have the central widget for the 'mainwindow' set properly and the parent of the layout is the central widget,
Then set a sizepolicy for the mainwindow/widget as you wish.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class RandomWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(RandomWidget, self).__init__(parent)
self.layout = QtWidgets.QVBoxLayout()
self.setLayout(self.layout)
self.ui()
self.layout.addWidget(self.table)
self.layout.addWidget(self.table2)
def ui(self):
self.table = QtWidgets.QTableWidget()
self.table.setMinimumSize(800,200)
self.table2 = QtWidgets.QTableWidget()
class Mainwindow(QtWidgets.QMainWindow):
def __init__(self):
self.widget = None
super(Mainwindow, self).__init__()
self.setWindowTitle('test')
def ui(self):
self.setCentralWidget(self.widget)
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Window = Mainwindow()
Window.widget = RandomWidget(Window)
Window.ui()
sys.exit(app.exec_())

Semi-resizable widgets in PyQt

I try to create a gui with two main widgets. The window should be resizable. When resized horizontally only one of them widgets should expand. When resized vertically both should expand. Furthermore it should be possible readjust the resize this split horizontally. I illustrated this to make it more clear:
With tkinter this was easily achievable with the properties expand and fill. In Qt I could use the resize event but I hope that I don't have to do this manually, since this should after all be a common task. I tried toying around with QHBoxLayout but without success unfortunately.
Any help is greatly appreciated. Thanks.
You need to use the setStretchFactor method on your QSplitter.
An example (modified from the QSplitter example here):
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
left = QtGui.QFrame(self)
left.setFrameShape(QtGui.QFrame.StyledPanel)
right = QtGui.QFrame(self)
right.setFrameShape(QtGui.QFrame.StyledPanel)
splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter.addWidget(left)
splitter.addWidget(right)
splitter.setStretchFactor(1, 1)
splitter.setSizes([125, 150])
hbox.addWidget(splitter)
self.setLayout(hbox)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This produces an initial UI that looks like this:
When the image is expanded horizontally, you can see that the left widget stays the same size:
When expanded vertically, both widgets expand:
Finally, the splitter is resizeable:
If you adjust the window size after adjusting the splitter, the left widget will retain it's size and the right will expand/collapse to fill the remainder of the window.

QBoxLayout stretches its widgets when resizing it's containing ScrollArea, rather than scrolling

I have a ScrollArea, containing a VBoxLayout containing several Labels:
realmScroll = QScrollArea(self.container.widget())
realmScroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
realmScroll.setWidgetResizable(True) # setting this to False has no effect
self.realmLayout = QVBoxLayout(realmScroll)
realmScroll.setWidget(self.realmLayout.widget())
self.container.addWidget(realmScroll)
for i in range(1, 20):
label = QLabel("test #" + str(i))
label.setMinimumHeight(20)
self.realmLayout.addWidget(label)
However when viewed the layout doesn't scroll, it de-stretches (shrinks?) the items together vertically:
I've tried using minimum heights but this doesn't seem to work - what can I do this to make each label appear separately and cause the scrollview to scroll?
Create a separate widget as a container of the labels (below as "labelsContainer") and set it as the scroll area's widget. Make a vertical box layout for the container widget (below as "labelsLayout") and add the labels to that layout.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Test(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.realmScroll = QScrollArea(self)
self.setCentralWidget(self.realmScroll)
self.realmScroll.setWidgetResizable(True)
labelsContainer = QWidget()
self.realmScroll.setWidget(labelsContainer)
labelsLayout = QVBoxLayout(labelsContainer)
for i in range(1, 20):
label = QLabel("test #" + str(i))
labelsLayout.addWidget(label)
app = QApplication(sys.argv)
test = Test()
test.show()
app.exec_()

Resources