Reputation: 541
I'm somewhat experienced in python but it's been a looooong time so bear with me on this.
Okay, so I found this really great PyQt code sample where a guy pushes buttons onto a scroll area, which automatically triggers a scrollbar when the content (the buttons) overflow.
So I went and tried to do the very same thing but pushing widgets to the scroll area, this is my code:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui
class myDialog(QtGui.QWidget):
_buttons = 0
def __init__(self, parent=None):
super(myDialog, self).__init__(parent)
self.setGeometry(50, 50, 500, 500)
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setText(QtGui.QApplication.translate("self", "Add Button!", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.scrollArea = QtGui.QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.scrollArea)
self.verticalLayoutScroll = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self._buttons += 1
pustButtonName = u"Button {0}".format(self._buttons)
#pushButton = QtGui.QPushButton(self.scrollAreaWidgetContents)
#pushButton.setText(pustButtonName)
muhAlarma = AlarmaWidget(self.scrollAreaWidgetContents)
self.verticalLayoutScroll.addWidget(muhAlarma)
class AlarmaWidget(QtGui.QWidget):
def __init__(self, parent):
super(AlarmaWidget, self).__init__(parent)
#self.setGeometry(0,0, 30, 30)
theButton = QtGui.QPushButton(self)
theButton.setText("It's Friday my dudes.")
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
main = myDialog()
#main.setWindowFlags(QtCore.Qt.FramelessWindowHint)
#main.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
#main.showFullScreen()
main.show()
sys.exit(app.exec_())
Upon running this code and pressing the button, buttons will start to appear. However, the big problem is that instead of filling up the scrollbar area and causing the scrollbar to be enabled, the buttons just end up piling on top of each other, why is that?
PS: uncommenting the commented pushButton lines and commenting the muhAlarma line will make this sample work again. However I need to push custom widgets, not buttons.
Thanks in advance
Upvotes: 0
Views: 405
Reputation: 561
Adding QWidgets (QPushButton) to scrollArea. This is the example code for insert QWidgets (QPushButton) to scrollArea. Please check this code. Are you expecting this result, If your are not expecting this answer, sorry. Thanks, Subin
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class myDialog (QtGui.QWidget):
def __init__(self, parent=None):
super(myDialog, self).__init__(parent)
self.button = QtGui.QPushButton (self)
self.button.setObjectName ('button')
self.button.setText ('Add Button')
self.scrollArea = QtGui.QScrollArea (self)
self.scrollArea.setWidgetResizable (True)
self.scrollArea.setObjectName('scrollArea')
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setObjectName('scrollAreaWidgetContents')
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout = QtGui.QVBoxLayout (self)
self.verticalLayout.setObjectName ('verticalLayout')
self.verticalLayout.addWidget(self.button)
self.verticalLayout.addWidget (self.scrollArea)
self.verticalLayout_scroll = QtGui.QVBoxLayout (self.scrollAreaWidgetContents)
self.verticalLayout_scroll.setObjectName ('verticalLayout_scroll')
self.button.clicked.connect (self.createButtons)
self.resize(250, 330)
def createButtons (self) :
existsButtonList = []
for loop in range (self.verticalLayout_scroll.count ()) :
if type(self.verticalLayout_scroll.itemAt(loop).widget())==QtGui.QPushButton :
existsButtonList.append (self.verticalLayout_scroll.itemAt(loop).widget())
button_custom = QtGui.QPushButton (self)
button_custom.setObjectName ('button%s'% str(len(existsButtonList)+1))
button_custom.setText ('Add Button - %s'% str(len(existsButtonList)+1))
self.verticalLayout_scroll.addWidget(button_custom)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
widget = myDialog ()
widget.show()
app.exec_()
Upvotes: 1
Reputation: 541
Okay I have found a solution after almost going crazy for quite a while. If anyone is having a similar problem, I've got your back!
in
AlarmaWidget.__init__
set a minimunHeight like so:
self.setMinimumHeight(50)
Upvotes: 0