scroll bar for long list of buttons - python

I have a problem desinging a window using PySide.
class MainForm(QWidget):
def __init__(self, parent=None):
super(MainForm, self).__init__()
# init window
self.hbox = QHBoxLayout(self)
self.scroll = QScrollArea(self)
self.table = QTableWidget(self)
self.leftspltr = QSplitter(QtCore.Qt.Vertical)
self.rightspltr = QSplitter(QtCore.Qt.Vertical)
self.scroll.setWidget(self.leftspltr)
self.rightspltr.addWidget(self.table)
self.hbox.addWidget(self.leftspltr)
self.hbox.addWidget(self.rightspltr)
self.setWindowTitle("...")
self.table.setColumnCount(3)
self.table.move(11,220)
self.table.verticalHeader().hide()
self.table.setHorizontalHeaderLabels(["Key","Full","HR"])
dic = dumpered_hash.dict_naming_values
os.popen("rm dumpered_hash dumpered_hash.py dumpered_hash.pyc")
xpos = 0
ypos = 0
for key, val1 in sorted(dic.iteritems()):
btn = QPushButton(key, self)
btn.clicked.connect(partial(self.fillTbl, val1, len(val1) + 1))
btn.setFixedSize(84,20)
self.leftspltr.addWidget(btn)
im trying to design a window that has some button (more than 30) placed virtically, so I want to have them in scroll bar that fits the window size, and a table at the right side that doesnt move while scrolling.
the code above gives me a tiny scroll bar that moves the button, bur they are out of the window size.
what wrong there ?

I think I understand what you are trying to do here. However, you are placing your buttons in a vertical splitter and then fixing their size. Instead, place the buttons in a vertical layout, and then place that and the table widget into a horizontal splitter.
class MainForm(QtGui.QWidget):
def __init__(self, parent=None):
super(MainForm, self).__init__()
# init window
self.hbox = QtGui.QHBoxLayout(self)
self.btnbox = QtGui.QVBoxLayout()
self.scroll = QtGui.QScrollArea(self)
self.table = QtGui.QTableWidget(self)
self.spltr = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.setWindowTitle("...")
self.table.setColumnCount(3)
self.table.move(11,220)
self.table.verticalHeader().hide()
self.table.setHorizontalHeaderLabels(["Key","Full","HR"])
dic = {}
for i in range(30):
dic['foo '+str(i)] = [i]
for key, val1 in sorted(dic.iteritems()):
btn = QtGui.QPushButton(key, self)
self.btnbox.addWidget(btn)
# must place layout into a widget before adding to QScrollArea
holder = QtGui.QWidget()
holder.setLayout(self.btnbox)
self.scroll.setWidget(holder)
self.spltr.addWidget(self.scroll)
self.spltr.addWidget(self.table)
self.hbox.addWidget(self.spltr)
I would also like to note, that is it better to include the simplest piece of code necessary to reproduce your problem. I don't know what you are trying to achieve with the lines containing dumpered_hash, but it seems to be irrelevant, as well as a dubious use of a deprecated function, and makes it impossible to run your code without modification. Make it easier for people to help you!

Related

Add dockable widgets in quadrants using PyQt

I'm creating a GUI using PyQt, to display 4 images in a window, positioned in this way:
Top left
Top right
Bottom left
Bottom right
I'd like to be able to undock them, but also to redock them back to any of the 4 available space.
My final goal is to set it up so that moving an undocked image where another one is already placed, would move that second image out of the way (docking it in another free quadrant or undocking it), or that placing it in the center would make it occupy all the 4 quadrants (undocking all the others).
I've tried achieving this with QDockWidget, but so far I'm not achieving good results.
My current code:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window")
grid_layout = QGridLayout()
dock_window_1 = QMainWindow()
docked = QDockWidget("Dockable", self)
dock_window_1.addDockWidget(Qt.DockWidgetArea.TopDockWidgetArea, docked)
dockedWidget = QWidget(self)
docked.setWidget(dockedWidget)
dockedWidget.setLayout(QVBoxLayout())
dockedWidget.layout().addWidget(QPushButton("1"))
dock_window_2 = QMainWindow()
docked_2 = QDockWidget("Dockable_2", self)
dock_window_2.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, docked_2)
dockedWidget_2 = QWidget(self)
docked.setWidget(dockedWidget_2)
dockedWidget_2.setLayout(QVBoxLayout())
dockedWidget_2.layout().addWidget(QPushButton("2"))
dock_window_3 = QMainWindow()
docked_3 = QDockWidget("Dockable_3", self)
dock_window_3.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, docked_3)
dockedWidget_3 = QWidget(self)
docked.setWidget(dockedWidget_3)
dockedWidget_3.setLayout(QVBoxLayout())
dockedWidget_3.layout().addWidget(QPushButton("3"))
dock_window_4 = QMainWindow()
docked_4 = QDockWidget("Dockable_4", self)
dock_window_4.addDockWidget(Qt.DockWidgetArea.BottomDockWidgetArea, docked_4)
dockedWidget_4 = QWidget(self)
docked.setWidget(dockedWidget_4)
dockedWidget_4.setLayout(QVBoxLayout())
dockedWidget_4.layout().addWidget(QPushButton("4"))
grid_layout.addWidget(dock_window_1, 0, 0)
grid_layout.addWidget(dock_window_2, 1, 0)
grid_layout.addWidget(dock_window_3, 0, 1)
grid_layout.addWidget(dock_window_4, 1, 1)
widget = QWidget()
widget.setLayout(grid_layout)
self.setCentralWidget(widget)
This kind of works, but I'm only able to redock a widget in its original place.
Can anyone help me getting on the right road here?
Thanks in advance!
Most of the behavior you are describing is actually already implemented by Qt using the QMainWindow and the QDockWidget.
The issue with your code is you are creating a unique QMainWindow for each of the QDockWidgets when the program only needs one QMainWindow. Additionally you are adding each of the QDockWidgets to the layout after you have already added them using the standard dockAreas surrounding the central widget.
If you eliminate the unneeded QMainWindows and instead assign each of the QDockWidgets to the your MainWindow, your desired functionality should work automatically.
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window")
grid_layout = QGridLayout()
docked = QDockWidget("Dockable", self)
docked.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dockedWidget = QWidget(self)
docked.setWidget(dockedWidget)
dockedWidget.setLayout(QVBoxLayout())
dockedWidget.layout().addWidget(QPushButton("1"))
docked_2 = QDockWidget("Dockable_2", self)
docked_2.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dockedWidget_2 = QWidget(self)
docked_2.setWidget(dockedWidget_2)
dockedWidget_2.setLayout(QVBoxLayout())
dockedWidget_2.layout().addWidget(QPushButton("2"))
docked_3 = QDockWidget("Dockable_3", self)
docked_3.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dockedWidget_3 = QWidget(self)
docked_3.setWidget(dockedWidget_3)
dockedWidget_3.setLayout(QVBoxLayout())
dockedWidget_3.layout().addWidget(QPushButton("3"))
docked_4 = QDockWidget("Dockable_4", self)
docked_4.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dockedWidget_4 = QWidget(self)
docked_4.setWidget(dockedWidget_4)
dockedWidget_4.setLayout(QVBoxLayout())
dockedWidget_4.layout().addWidget(QPushButton("4"))
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, docked)
self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, docked_2)
self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, docked_3)
self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, docked_4)
widget = QWidget()
widget.setLayout(grid_layout)
self.setCentralWidget(widget)

PyQt6 deleting custom widget with nested class causes the program to crash

I am using Python 3.9.5.
I have encountered some serious problem in my project and here is a minimum reproducible example code, along with some descriptions.
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
class Editor(QTextEdit):
doubleClicked = pyqtSignal(QTextEdit)
def __init__(self):
super().__init__()
self.setReadOnly(True)
def mouseDoubleClickEvent(self, e: QMouseEvent) -> None:
self.doubleClicked.emit(self)
class textcell(QGroupBox):
def __init__(self, text):
super().__init__()
self.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)
self.label = QLabel(text)
self.apply = makebutton('Apply')
self.apply.hide()
self.editor = Editor()
self.editor.doubleClicked.connect(lambda: self.editor.setReadOnly(False))
self.editor.doubleClicked.connect(self.apply.show)
self.hbox = QHBoxLayout()
self.hbox.addSpacerItem(spacer)
self.hbox.addWidget(self.apply)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.label)
self.vbox.addWidget(self.editor)
self.vbox.addLayout(self.hbox)
self.setLayout(self.vbox)
self.apply.clicked.connect(self.on_ApplyClick)
def on_ApplyClick(self):
self.editor.setReadOnly(True)
self.apply.hide()
def makebutton(text):
button = QPushButton()
button.setFixedSize(60, 20)
button.setText(text)
return button
class songpage(QGroupBox):
def __init__(self, texts):
super().__init__()
self.init(texts)
self.setCheckable(True)
self.setChecked(False)
def init(self, texts):
self.vbox = QVBoxLayout()
artist = textcell('Artist')
artist.editor.setText(texts[0])
album = textcell('Album')
album.editor.setText(texts[1])
title = textcell('Title')
title.editor.setText(texts[2])
self.vbox.addWidget(artist)
self.vbox.addWidget(album)
self.vbox.addWidget(title)
self.setLayout(self.vbox)
spacer = QSpacerItem(0, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(405, 720)
self.setWindowTitle('example')
frame = self.frameGeometry()
center = self.screen().availableGeometry().center()
frame.moveCenter(center)
self.move(frame.topLeft())
self.centralwidget = QWidget(self)
vbox = QVBoxLayout(self.centralwidget)
hbox = QHBoxLayout()
add = makebutton('Add')
delete = makebutton('Delete')
hbox.addWidget(add)
hbox.addSpacerItem(spacer)
hbox.addWidget(delete)
vbox.addLayout(hbox)
self.scrollArea = QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget()
self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)
vbox.addWidget(self.scrollArea)
self.setCentralWidget(self.centralwidget)
add.clicked.connect(self.addObj)
delete.clicked.connect(self.deleteObj)
def addObj(self):
Obj = songpage(('AAA', 'BBB', 'CCC'))
self.verticalLayout.addWidget(Obj)
def deleteObj(self):
item = self.verticalLayout.itemAt(0)
widget = item.widget()
self.verticalLayout.removeItem(item)
self.verticalLayout.removeWidget(widget)
app = QApplication([])
window = Ui_MainWindow()
window.show()
app.exec()
The problem is very simple, if I click add button, the widget will be added and everything works fine, if I double click on a QTextEdit, its apply button will show and it will change from read only to editable.
After I click an apply button, the button will hide and the corresponding QTextEdit will be read only again.
I have finally managed to add a doubleClicked signal to QTextEdit.
And, the problem, is if I click delete button, instead of deleting the thing as expected, it crashes the whole application.
I am sorry the minimum reproducible example is so long, but I have only managed to reproduce the issue with all the code and I really don't know what went wrong.
So how to fix it?
Well, I have figured it out, it was caused by the spacer item that I add to every one of horizontal layouts where fixed-sized buttons are used.
All such layouts are using the same spacer item, exactly the same spacer item, not just identical.
From what I have observed, the spacer items are all references to the same object, they are all echos of the same object which lives at a fixed memory address.
I honestly don't understand how such thing works, that the same object can not only be added to multiple layouts and present in all the layouts concurrently, but also be added to the same layout multiple times, yet it always remains the original object, not duplicates of itself.
I thought when I added the same spacer item to multiple layouts, I didn't add the original spacer item, instead duplicates of the original item which are identical but are at different memory addresses, and clearly that isn't how Python works.
So when I remove a widget, everything inside it, everything inside its layout is deleted, and the spacer item is deleted.
Because all the spacer items in all the layouts are references to the original spacer item, when I delete one of the layouts, the original spacer item is deleted as well, and the spacer item is deleted from all other layouts, yet its shadows remain, and the item isn't properly removed from all the other layouts, the layouts contain references to an object that no longer exists, thus the application crashed.
By removing the definition of the spacer item and replacing adding the spacer item with .addStretch(), the bug is fixed.

How to save selected element from combobox as variable in PyQt5?

I'm interested in how to save a selected value from my combobox as variable, so when I press e.g. B then I want it to be saved as SelectedValueCBox = selected value, which would be B in this case.
Thank you for your help
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = "PyQt5 - StockWindow"
self.left = 0
self.top = 0
self.width = 200
self.height = 300
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.tab_widget = MyTabWidget(self)
self.setCentralWidget(self.tab_widget)
self.show()
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
#self.layout = QGridLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tabs.resize(300, 200)
self.tabs.addTab(self.tab1, "Stock-Picker")
self.tab1.layout = QGridLayout(self)
button = QToolButton()
self.tab1.layout.addWidget(button, 1,1,1,1)
d = {'AEX':['A','B','C'], 'ATX':['D','E','F'], 'BEL20':['G','H','I'], 'BIST100':['J','K','L']}
def callback_factory(k, v):
return lambda: button.setText('{0}_{1}'.format(k, v))
menu = QMenu()
self.tab1.layout.addWidget(menu, 1,1,1,1)
for k, vals in d.items():
sub_menu = menu.addMenu(k)
for v in vals:
action = sub_menu.addAction(str(v))
action.triggered.connect(callback_factory(k, v))
button.setMenu(menu)
self.tab1.setLayout(self.tab1.layout)
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Since you're already returning a lambda for the connection, the solution is to use a function instead.
class MyTabWidget(QWidget):
def __init__(self, parent):
# ...
def callback_factory(k, v):
def func():
self.selectedValueCBox = v
button.setText('{0}_{1}'.format(k, v))
return func
# ...
self.selectedValueCBox = None
Note that your code also has many issues.
First of all, you should not add the menu to the layout: not only it doesn't make any sense (the menu should pop up, while adding it to a layout makes it "embed" into the widget, and that's not good), but it also creates graphical issues especially because you added the menu to the same grid "slot" (1, 1, 1, 1) which is already occupied by the button.
Creating a layout with a widget as argument automatically sets the layout to that widget. While in your case it doesn't create a big issue (since you've already set the layout) you should not create self.tab1.layout with self. Also, since you've already set the QVBoxLayout (due to the parent argument), there's no need to call setLayout() again.
A widget container makes sense if you're actually going to add more than one widget. You're only adding a QTabWidget to its layout, so it's almost useless, and you should just subclass from QTabWidget instead.
Calling resize on a widget that is going to be added on a layout is useless, as the layout will take care of the resizing and the previous resize call will be completely ignored. resize() makes only sense for top level widgets (windows) or the rare case of widgets not managed by a layout.
self.layout() is an existing property of all QWidgets, you should not overwrite it. The same with self.width() and self.height() you used in the App class.
App should refer to an application class, but you're using it for a QMainWindow. They are radically different types of classes.
Finally, you have no combobox in your code. A combobox is widget that is completely different from a drop down menu like the one you're using. I suggest you to be more careful with the terminology in the future, otherwise your question would result very confusing, preventing people to actually being able to help you.

Stop widgets from overlapping each other and add space between them

I am using QHBoxLayout for layout. The breakdown is: pic.1 is what happens, pic.2 is what I want - widgets don't overlap and there is a gap between them. For pic.2, I created a Gap widget to stick between the existing widgets. But this is a cumbersome solution which implies extra maintenance (especially when I have more than two widgets to care for). Moreover, since B overlaps A, I think the newly added gap widget overlaps A as well, maybe according the fraction of its size. I am not so sure about this.
I tried using self.layout.addSpacing(10), but that doesn't work. The red widget shifts by 10 pixels from where it was before, not from the border of the widget on the left.
Note, too, that the contained widgets will all have some minimum width specification.
So, how can I add the space between two widgets like in pic.2?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Gap(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setMinimumWidth(10)
self.setMinimumHeight(1)
class Line(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
# Set layout
self.layout = QHBoxLayout()
self.setLayout(self.layout)
# Set palette
self.palette1 = QPalette()
self.palette1.setColor(QPalette.Window, Qt.red)
self.palette2 = QPalette()
self.palette2.setColor(QPalette.Window, Qt.blue)
self.palettebg = QPalette()
self.palettebg.setColor(QPalette.Window, Qt.green)
self.setAutoFillBackground(True)
self.setPalette(self.palettebg)
# Set labels
self.label1 = QLabel(self)
self.label1.setText("A")
self.label1.setStyleSheet('font-size: 36pt;')
self.label1.adjustSize()
self.label1.setAutoFillBackground(True)
self.label1.setPalette(self.palette1)
self.label1.setMinimumSize(36, 36)
self.label2 = QLabel(self)
self.label2.setText("B")
self.label2.move(30, 0)
self.label2.setStyleSheet('font-size: 36pt;')
self.label2.adjustSize()
self.label2.setAutoFillBackground(True)
self.label2.setPalette(self.palette2)
self.label2.setMinimumSize(36, 36)
self.gap = Gap()
self.layout.addWidget(self.label1)
# self.layout.addWidget(self.gap)
# self.layout.addSpacing(10)
self.layout.addWidget(self.label2)
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt5'
self.left = 10
self.top = 10
self.width = 200
self.height = 54
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.line = Line(self)
self.line.resize(74, 54)
self.line.move(50, 50)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
# CUSTOM
app.setFont(QFontDatabase().font("Monospace", "Regular", 14))
ex = App()
sys.exit(app.exec_())
EDIT clarification, as requested: (1) suppose the size of the parent widget is too small (and cannot be resized), (2) the parent widget has QHBoxLayout with widgets A and B added to it, (3) the parent widget being too small, QHBoxLayout aranges the child widgets A and B such that they overlap each other like in the first picture. (4) Such overlap is undesirable, instead the widgets just need to be placed one after another, with no overlap, and with gap between them, like in picture 2. I don't know how to do this with QHBoxLayout.
EDIT 2 Here is a visual explanation.
The green here is the parent widget - not resizable by assumption. Widget A is added:
Add widget B:
Now, widget B is on top of A. I don't want it. I want this instead:
The minimumSize of the child widgets does not influence the minimumSize of the parent widget, and the use of the layout does not influence the minimumSize of the widget either. The layouts set the minimumSizeHint and sizeHint using as information the minimumSize of the widgets that handle plus other features such as the size and sizing policy. So in the first instance you must set the minimumSize of the parent widget to be the minimumSizeHint of it.
On the other hand the layout has a spacing by default so it is advisable to set it to 0.
class Line(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent, autoFillBackground=True)
# Set palette
palette1 = QPalette()
palette1.setColor(QPalette.Window, Qt.red)
palette2 = QPalette()
palette2.setColor(QPalette.Window, Qt.blue)
palettebg = QPalette()
palettebg.setColor(QPalette.Window, Qt.green)
self.setPalette(palettebg)
# Set labels
self.label1 = QLabel(text="A", autoFillBackground=True)
self.label1.setStyleSheet('font-size: 36pt;')
self.label1.setPalette(palette1)
self.label1.setMinimumSize(36, 36)
self.label1.adjustSize()
self.label2 = QLabel(text="B", autoFillBackground=True)
self.label2.setStyleSheet('font-size: 36pt;')
self.label2.setPalette(palette2)
self.label2.setMinimumSize(36, 36)
self.label2.adjustSize()
# Set layout
layout = QHBoxLayout(self, spacing=0)
layout.addWidget(self.label1)
layout.addSpacing(10)
layout.addWidget(self.label2)
self.setMinimumSize(self.minimumSizeHint())
# or
# layout = QHBoxLayout(self, spacing=10)
# layout.addWidget(self.label1)
# layout.addWidget(self.label2)
# self.setMinimumSize(self.minimumSizeHint())
Update:
The maximum size of the layout that can be handled is the size of the parent widget, so in its case it will compress not respecting the spaces, a solution is to set a widget that is the content, and in that to establish the layout, so the layout will stretch to the content widget with freedom.
class Line(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent, autoFillBackground=True)
# Set palette
palette1 = QPalette()
palette1.setColor(QPalette.Window, Qt.red)
palette2 = QPalette()
palette2.setColor(QPalette.Window, Qt.blue)
palettebg = QPalette()
palettebg.setColor(QPalette.Window, Qt.green)
self.setPalette(palettebg)
# Set labels
self.label1 = QLabel(text="A", autoFillBackground=True)
self.label1.setStyleSheet('font-size: 36pt;')
self.label1.setPalette(palette1)
self.label1.setMinimumSize(36, 36)
self.label1.adjustSize()
self.label2 = QLabel(text="B", autoFillBackground=True)
self.label2.setStyleSheet('font-size: 36pt;')
self.label2.setPalette(palette2)
self.label2.setMinimumSize(36, 36)
self.label2.adjustSize()
content_widget = QWidget(self)
layout = QHBoxLayout(content_widget, spacing=10)
layout.addWidget(self.label1)
layout.addWidget(self.label2)
content_widget.setFixedSize(content_widget.minimumSizeHint())

Qt Widget with slider

I'm writing GUI application using PyQt4 and I need to create widgets and dialog windows which can be slided vertically. After clicking "Add new widgets" button (and moving it down) I create dynamically new small widgets (containing 2 textEdit objects). I want to avoid situation when window's height will be too small to properly show all widgets so the obvious solution seems to be a slider. I tried to use QSlider but I'm not sure if it's the best way and how to configure such slider.
class MyDialog(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.smallWidgets = list()
self.resize(450,500)
self.buttonPosition = 60
firstWidget = SmallWidget(self)
firstWidget.setGeometry(QRect(5,5, 450, 80))
self.addWidgetButton = QPushButton(self)
self.addWidgetButton.setText("Add new widget")
self.addWidgetButton.setGeometry(QRect(165,self.buttonPosition,120,40))
self.connect(self.addWidgetButton, QtCore.SIGNAL("clicked()"), self.addNewWidget)
def addNewWidget(self):
newWidget = SmallWidget(self)
newWidget.setGeometry(QRect(5,self.buttonPosition, 450, 80))
self.buttonPosition += 70
newWidget.setVisible(True)
self.smallWidgets.append(newWidget)
self.addWidgetButton.setGeometry(QRect(165,self.buttonPosition,120,40))
So I took #ekhumoro advice and implemented this using layout and QScrollArea (thanks! That's what I was looking for!). Scrolling works as I wanted, but I suppose my implementation is not the ideal solution. ;)
class MyDialog(QDialog):
def __init__(self, strt, parent=None):
QDialog.__init__(self, parent)
self.smallWidgets = list()
self.setMinimumWidth(450)
self.setMinimumHeight(600)
self.setupLayout()
self.setupScrollArea()
self.addWidgetButton = QPushButton(self.containerWidget)
self.addWidgetButton.setText("Add new widget")
self.layout.addWidget(self.addWidgetButton)
self.connect(self.addWidgetButton, QtCore.SIGNAL("clicked()"), self.addNewWidget)
def setupLayout(self):
self.containerWidget = QWidget(self)
self.widgetHeight = 120
self.containerWidget.setGeometry(QRect(0,0,450,self.widgetHeight))
self.layout = QVBoxLayout()
self.containerWidget.setLayout(self.layout)
def setupScrollArea(self):
self.scrollArea = QScrollArea(self)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setMaximumWidth(440)
self.scrollArea.setMinimumHeight(600)
self.scrollArea.setWidgetResizable(False)
self.scrollArea.setWidget(self.containerWidget)
def addNewWidget(self):
newWidget = SmallWidget(self.containerWidget)
widgetPosition = len(self.smallWidgets)
self.layout.insertWidget(widgetPosition, newWidget)
self.smallWidgets.append(newWidget)
self.widgetHeight += 80
self.containerWidget.resize(450,self.widgetHeight)

Categories

Resources