Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Basic Implementation of Scroll Area in PyQt4
Forum Updated to NodeBB v4.3 + New Features

Basic Implementation of Scroll Area in PyQt4

Scheduled Pinned Locked Moved Language Bindings
3 Posts 2 Posters 12.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alabaster
    wrote on 6 Nov 2012, 16:07 last edited by
    #1

    PyQt4 / Python 3 / Windows 7

    Hello -- I am fairly new to PyQt and I'm having a great deal of trouble figuring how out to implement a basic scroll area. Attached is some simple sample code - how can I set this window to scroll rather than extend the window below the screen edge? Thank you

    @import sys
    from PyQt4 import QtGui
    from random import choice

    class ThisWindow(QtGui.QWidget):
    def init(self):
    super(ThisWindow, self).init()
    self.initUI()

    def initUI(self):    
        vbox = QtGui.QVBoxLayout() 
              
        itemlist = GenerateList()        
        
        for item in itemlist:
            newlabel = QtGui.QLabel(item)
            newcheckbox = QtGui.QCheckBox()
            newcombobox = QtGui.QComboBox()
            
            new_row_box = QtGui.QHBoxLayout()
            
            new_row_box.addWidget(newlabel)
            new_row_box.addWidget(newcheckbox)
            new_row_box.addWidget(newcombobox)
            new_row_box.addStretch(1)
            
            vbox.addLayout(new_row_box)
            
        self.setGeometry(100,100,800,600)
        
        self.setLayout(vbox)    
        self.setWindowTitle('How do I make this window scroll if it gets beyond a certain height (e.g. the edge of the screen?)')    
        self.show()        
    

    def GenerateList():
    itemlist = []
    top = choice(range(50,150))
    for x in range(0,top):
    item = "Label # {0}".format(x)
    itemlist.append(item)
    return itemlist

    def main():
    app = QtGui.QApplication(sys.argv)
    win = ThisWindow()
    sys.exit(app.exec_())

    if name == 'main':
    main()@

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jazzycamel
      wrote on 6 Nov 2012, 18:54 last edited by
      #2

      The best way to do this is using QScrollArea as demonstrated below:

      @
      from PyQt4.QtGui import *
      from random import choice

      class ThisWidget(QWidget):
      def init(self, parent=None):
      QWidget.init(self, parent)

          l=QVBoxLayout(self)
          l.setContentsMargins(0,0,0,0)
          l.setSpacing(0)
      
          s=QScrollArea()
          l.addWidget(s)
      
          w=QWidget(self)        
          vbox=QVBoxLayout(w)
      
          for x in range(0, choice(range(50,150))):
              _l=QHBoxLayout()
              _l.addWidget(QLabel("Label # %d" % x, self))
              _l.addWidget(QCheckBox(self))
              _l.addWidget(QComboBox(self))
              _l.addStretch(1)
              vbox.addLayout(_l)
      
          s.setWidget(w)
      
          self.setGeometry(100, 100, 800, 600)
          self.setWindowTitle("This is how you make the window scroll if it gets beyond a certain height")
      

      if name=="main":
      from sys import argv, exit
      a=QApplication(argv)
      win=ThisWidget()
      win.show()
      win.raise_()
      exit(a.exec_())
      @

      Hope this helps.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alabaster
        wrote on 13 Nov 2012, 19:16 last edited by
        #3

        Sorry I didn't respond earlier -- yes, this was very helpful. Much appreciated!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved