PDA

View Full Version : How to add scroll bar in PyQt4 retrospectively



MrPickle
22nd June 2018, 08:16
Hello community!

I've made a simple GUI with Qt Designer and let it translate the .ui file to a python code.

You can find the code here: https://snip.dssr.ch/?efe8c61b137f0751#IGcw4iGEOTHYlXTseCc+Z7tPDDPVtgAe APx5A4P8pnc=

Now I would like to add a scroll bar that appears if the main window is being resized. In other words: if user resize the main window so that the size of content is bigger than the window itself, then the scrollbar should appear.

Any idea how to get this to work?

high_flyer
22nd June 2018, 10:33
checkout http://doc.qt.io/qt-5/qscrollarea.html

MrPickle
22nd June 2018, 11:09
Hey high_flyer,

I've read the doc and also followed buck54321's answer @Stackoverflow (https://stackoverflow.com/a/50967610/8937563) regarding my problem, but unfortunately I can't figure out how to achieve the desired behaviour. What I came out with is this code (https://snip.dssr.ch/?ffc40f610034b32d#Ef7B9l4XBKD4UcbuC6+E67j7MPthMqWW ovk8PulXMZQ=). The scrollarea shows up in the Dialog but unfortunately it has none of my content in it.
I simply can't figure out what I do wrong.

P.S. you can PM me also in German

high_flyer
22nd June 2018, 16:40
Ok, lets takes this one piece at a time.
Please reduce your code to only show your dialog, with a scroll area that contains your main widget which only has one button or label (just so there is something to see) in the middle.
This way we remove all non relevant noise and clutter from your code and can concentrate on the actual problem.

MrPickle
25th June 2018, 10:49
Hey high_flyer,

thanks again for the help.
I've reduced the code to have only two radio buttons inside the grid layout which self is inside a scroll area. Nevertheless here comes the code:


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(485, 785)
Dialog.setMinimumSize(QtCore.QSize(485, 400))
Dialog.setMaximumSize(QtCore.QSize(495, 785))
Dialog.setSizeIncrement(QtCore.QSize(1, 1))
Dialog.setMouseTracking(False)
Dialog.setAutoFillBackground(False)
Dialog.setModal(False)
self.layoutWidget = QtGui.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(10, 736, 471, 49))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.SaveCancelLayout = QtGui.QHBoxLayout(self.layoutWidget)
self.SaveCancelLayout.setContentsMargins(0, 8, 5, 0)
self.SaveCancelLayout.setSpacing(10)
self.SaveCancelLayout.setObjectName(_fromUtf8("SaveCancelLayout"))
self.cancelButton = QtGui.QPushButton(self.layoutWidget)
self.cancelButton.setMaximumSize(QtCore.QSize(200, 40))
self.cancelButton.setLayoutDirection(QtCore.Qt.Lef tToRight)
self.cancelButton.setObjectName(_fromUtf8("cancelButton"))
self.SaveCancelLayout.addWidget(self.cancelButton)
self.scrollArea = QtGui.QScrollArea(Dialog)
self.scrollArea.setGeometry(QtCore.QRect(10, 40, 471, 691))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.Q Rect(0, 0, 469, 689))
self.scrollAreaWidgetContents.setObjectName(_fromU tf8("scrollAreaWidgetContents"))
self.layoutWidget1 = QtGui.QWidget(self.scrollAreaWidgetContents)
self.layoutWidget1.setGeometry(QtCore.QRect(0, 20, 471, 631))
self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1"))
self.gridLayout = QtGui.QGridLayout(self.layoutWidget1)
self.gridLayout.setMargin(9)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.line_4 = QtGui.QFrame(self.layoutWidget1)
self.line_4.setMaximumSize(QtCore.QSize(465, 16777215))
self.line_4.setFrameShape(QtGui.QFrame.HLine)
self.line_4.setFrameShadow(QtGui.QFrame.Sunken)
self.line_4.setObjectName(_fromUtf8("line_4"))
self.gridLayout.addWidget(self.line_4, 0, 0, 1, 4)
self.logEnable = QtGui.QRadioButton(self.layoutWidget1)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.logEnable.setFont(font)
self.logEnable.setChecked(True)
self.logEnable.setObjectName(_fromUtf8("logEnable"))
self.buttonGroup_logging = QtGui.QButtonGroup(Dialog)
self.buttonGroup_logging.setObjectName(_fromUtf8("buttonGroup_logging"))
self.buttonGroup_logging.addButton(self.logEnable)
self.gridLayout.addWidget(self.logEnable, 1, 1, 1, 1)
self.logDisable = QtGui.QRadioButton(self.layoutWidget1)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.logDisable.setFont(font)
self.logDisable.setObjectName(_fromUtf8("logDisable"))
self.buttonGroup_logging.addButton(self.logDisable )
self.gridLayout.addWidget(self.logDisable, 1, 2, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetCon tents)

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Test Editor", None))
self.cancelButton.setText(_translate("Dialog", "Stop and exit", None))
self.logEnable.setText(_translate("Dialog", "enable logfile", None))
self.logDisable.setText(_translate("Dialog", "disable logfile", None))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())