Results 1 to 5 of 5

Thread: How to add scroll bar in PyQt4 retrospectively

  1. #1
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    1
    Qt products
    Platforms
    Windows

    Question How to add scroll bar in PyQt4 retrospectively

    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/?efe8c61b137f07...AeAPx5A4P8pnc=

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to add scroll bar in PyQt4 retrospectively

    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    MrPickle (22nd June 2018)

  4. #3
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: How to add scroll bar in PyQt4 retrospectively

    Hey high_flyer,

    I've read the doc and also followed buck54321's answer @Stackoverflow regarding my problem, but unfortunately I can't figure out how to achieve the desired behaviour. What I came out with is this code. 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

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to add scroll bar in PyQt4 retrospectively

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: How to add scroll bar in PyQt4 retrospectively

    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:

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'test.ui'
    4. #
    5. # Created by: PyQt4 UI code generator 4.11.4
    6. #
    7. # WARNING! All changes made in this file will be lost!
    8.  
    9. from PyQt4 import QtCore, QtGui
    10.  
    11. try:
    12. _fromUtf8 = QtCore.QString.fromUtf8
    13. except AttributeError:
    14. def _fromUtf8(s):
    15. return s
    16.  
    17. try:
    18. _encoding = QtGui.QApplication.UnicodeUTF8
    19. def _translate(context, text, disambig):
    20. return QtGui.QApplication.translate(context, text, disambig, _encoding)
    21. except AttributeError:
    22. def _translate(context, text, disambig):
    23. return QtGui.QApplication.translate(context, text, disambig)
    24.  
    25. class Ui_Dialog(object):
    26. def setupUi(self, Dialog):
    27. Dialog.setObjectName(_fromUtf8("Dialog"))
    28. Dialog.resize(485, 785)
    29. Dialog.setMinimumSize(QtCore.QSize(485, 400))
    30. Dialog.setMaximumSize(QtCore.QSize(495, 785))
    31. Dialog.setSizeIncrement(QtCore.QSize(1, 1))
    32. Dialog.setMouseTracking(False)
    33. Dialog.setAutoFillBackground(False)
    34. Dialog.setModal(False)
    35. self.layoutWidget = QtGui.QWidget(Dialog)
    36. self.layoutWidget.setGeometry(QtCore.QRect(10, 736, 471, 49))
    37. self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
    38. self.SaveCancelLayout = QtGui.QHBoxLayout(self.layoutWidget)
    39. self.SaveCancelLayout.setContentsMargins(0, 8, 5, 0)
    40. self.SaveCancelLayout.setSpacing(10)
    41. self.SaveCancelLayout.setObjectName(_fromUtf8("SaveCancelLayout"))
    42. self.cancelButton = QtGui.QPushButton(self.layoutWidget)
    43. self.cancelButton.setMaximumSize(QtCore.QSize(200, 40))
    44. self.cancelButton.setLayoutDirection(QtCore.Qt.LeftToRight)
    45. self.cancelButton.setObjectName(_fromUtf8("cancelButton"))
    46. self.SaveCancelLayout.addWidget(self.cancelButton)
    47. self.scrollArea = QtGui.QScrollArea(Dialog)
    48. self.scrollArea.setGeometry(QtCore.QRect(10, 40, 471, 691))
    49. self.scrollArea.setWidgetResizable(True)
    50. self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
    51. self.scrollAreaWidgetContents = QtGui.QWidget()
    52. self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 469, 689))
    53. self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
    54. self.layoutWidget1 = QtGui.QWidget(self.scrollAreaWidgetContents)
    55. self.layoutWidget1.setGeometry(QtCore.QRect(0, 20, 471, 631))
    56. self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1"))
    57. self.gridLayout = QtGui.QGridLayout(self.layoutWidget1)
    58. self.gridLayout.setMargin(9)
    59. self.gridLayout.setSpacing(6)
    60. self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
    61. self.line_4 = QtGui.QFrame(self.layoutWidget1)
    62. self.line_4.setMaximumSize(QtCore.QSize(465, 16777215))
    63. self.line_4.setFrameShape(QtGui.QFrame.HLine)
    64. self.line_4.setFrameShadow(QtGui.QFrame.Sunken)
    65. self.line_4.setObjectName(_fromUtf8("line_4"))
    66. self.gridLayout.addWidget(self.line_4, 0, 0, 1, 4)
    67. self.logEnable = QtGui.QRadioButton(self.layoutWidget1)
    68. font = QtGui.QFont()
    69. font.setBold(True)
    70. font.setWeight(75)
    71. self.logEnable.setFont(font)
    72. self.logEnable.setChecked(True)
    73. self.logEnable.setObjectName(_fromUtf8("logEnable"))
    74. self.buttonGroup_logging = QtGui.QButtonGroup(Dialog)
    75. self.buttonGroup_logging.setObjectName(_fromUtf8("buttonGroup_logging"))
    76. self.buttonGroup_logging.addButton(self.logEnable)
    77. self.gridLayout.addWidget(self.logEnable, 1, 1, 1, 1)
    78. self.logDisable = QtGui.QRadioButton(self.layoutWidget1)
    79. font = QtGui.QFont()
    80. font.setBold(True)
    81. font.setWeight(75)
    82. self.logDisable.setFont(font)
    83. self.logDisable.setObjectName(_fromUtf8("logDisable"))
    84. self.buttonGroup_logging.addButton(self.logDisable)
    85. self.gridLayout.addWidget(self.logDisable, 1, 2, 1, 1)
    86. self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    87.  
    88. self.retranslateUi(Dialog)
    89. QtCore.QMetaObject.connectSlotsByName(Dialog)
    90.  
    91. def retranslateUi(self, Dialog):
    92. Dialog.setWindowTitle(_translate("Dialog", "Test Editor", None))
    93. self.cancelButton.setText(_translate("Dialog", "Stop and exit", None))
    94. self.logEnable.setText(_translate("Dialog", "enable logfile", None))
    95. self.logDisable.setText(_translate("Dialog", "disable logfile", None))
    96.  
    97.  
    98. if __name__ == "__main__":
    99. import sys
    100. app = QtGui.QApplication(sys.argv)
    101. Dialog = QtGui.QDialog()
    102. ui = Ui_Dialog()
    103. ui.setupUi(Dialog)
    104. Dialog.show()
    105. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 30th January 2017, 12:42
  2. Replies: 1
    Last Post: 20th April 2013, 19:53
  3. [PyQt4] PyQt4 + gcin issue
    By fieliapm in forum Installation and Deployment
    Replies: 0
    Last Post: 28th September 2010, 09:04
  4. QDial disabling key scroll and mouse wheel scroll
    By ldg20 in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2010, 00:05
  5. Replies: 6
    Last Post: 14th April 2006, 06:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.