Question

I have the following code:


#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore

class SimfilePanel(QtGui.QWidget):
  '''This class provides the simfile panel shown on the right side of the main window.'''
  def __init__(self, parent=None):
    '''Load song info here.'''
    QtGui.QWidget.__init__(self, parent)

    ## Make widgets.
    # Pane with simfile information.
    simfileInfoPane = QtGui.QWidget()
    simfileInfoPane.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
    simfileInfoGrid = QtGui.QGridLayout()
    simfileInfoPane.setLayout(simfileInfoGrid)

    simfileInfoScrollArea = QtGui.QScrollArea()
    simfileInfoScrollArea.setWidget(simfileInfoPane)
    #if DEBUG: simfileInfoScrollArea.setBackgroundRole(QtGui.QPalette.Dark);

    # This will change
    labels = []
    textfields = []
    for i in range(0,20):
      labels.append( QtGui.QLabel("Label "+str(i)) )
      textfields.append( QtGui.QLineEdit() )
      labels[i].setBuddy(textfields[i])
      simfileInfoGrid.addWidget(labels[i], i, 0)
      simfileInfoGrid.addWidget(textfields[i], i, 1)

    ## Put widgets in a grid layout.
    mainvbox = QtGui.QVBoxLayout()
    mainvbox.addWidget(simfileInfoScrollArea)
    self.setLayout(mainvbox)


# Standalone testing
if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  panel = SimfilePanel()
  panel.show()
  sys.exit(app.exec_())

I can't get anything that I'm putting into the simfileInfoGrid to display! They'll display if I leave out the scroll area, but I need the scroll area as I will have a lot of fields to edit in the final version and I don't want to stretch the entire window over the screen.

As you see I've tried to add a size policy to simfileInfoPane, but it doesn't seem to affect anything. The area that's supposed to contain my pane stays empty!

Was it helpful?

Solution

Add the pane to the scroll area after you've added all the grid's contents. In particular you need to call QScrollArea.setWidget after you have finished creating the widget you add.

I don't know exactly why this is the problem, but I do know that I tend to initialize widgets "bottom-up": I finish adding all the contents of a sub-layout before I ever add it to a parent layout. I believe this is Qt optimizing order of rendering but I could be wrong about that.

The code below is a patch, mostly so you can see where the one-line change is.

    diff -u 1848547.py  tmp2.py
--- 1848547.py  2009-12-04 11:19:09.000000000 -0800
+++ tmp2.py 2009-12-04 11:34:58.000000000 -0800
@@ -19,7 +19,6 @@
     simfileInfoPane.setLayout(simfileInfoGrid)

     simfileInfoScrollArea = QtGui.QScrollArea()
-    simfileInfoScrollArea.setWidget(simfileInfoPane)
     #if DEBUG: 
     simfileInfoScrollArea.setBackgroundRole(QtGui.QPalette.Dark)

@@ -33,6 +32,8 @@
       simfileInfoGrid.addWidget(labels[i], i, 0)
       simfileInfoGrid.addWidget(textfields[i], i, 1)

+    simfileInfoScrollArea.setWidget(simfileInfoPane)
+
     ## Put widgets in a grid layout.
     mainvbox = QtGui.QVBoxLayout()
     mainvbox.addWidget(simfileInfoScrollArea)

OTHER TIPS

I have recently been struggling with the same thing, and I believe I have found the solution you are looking for.

The problem seems to be that when you add an empty widget to the scroll area, it has dimensions of zero by zero (since there is nothing inside of it).

The reason it doesn't get bigger is because there is a flag called widgetResizable that is False by default.

By simply calling setWidgetResizable(True) on the scroll area, the widget gets bigger as new items are added.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top