PyQt layout question: QScrollView and QGridLayout?

dwelch donald.welch at hp.com
Fri Nov 18 00:30:48 CET 2005


Volker Lenhardt wrote:
> Phil Thompson schrieb:
> 
>> On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
>>
>>> prefer to use QGridLayout, but cannot add it to the scroll view.
>>>
>>> sc=QScrollView(self)
>>> layout=QGridLayout(..., sc.viewport())
>>> sc.addChild(layout)
>>>
>>> results in a TypeError.
>>>
>>> Is there a way to get it to work? Filling a box viewport with lots of
>>> padding boxes and white space labels to establish grids is very
>>> cumbersome. And I need 4 different layouts to change places.
>>
>>
>>
>> QGridLayout is not a sub-class of QWidget, which is what addChild() is 
>> expecting. You probably want QGrid.
>>
>> Phil
> 
> 
> I hoped to find a more assuring answer. There's no MultiCellWidget, no 
> Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one 
> VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the 
> white space QLabels to fill not used grid cells.
> 
> And I have to delete all of them to change to another data layout.
> 
> Are you sure that there's no way to fill a QScrollView with the help of 
> some QLayout?
> 
> Still hopefully
> Volker

I _think_ I have code that does waht you want. This creates multiple 
layouts that contain an icon, a button, and some text on a scrollview. 
The code is awkward and ugly, but it works.


class ScrollToolView(QScrollView):
     def __init__(self,parent = None,name = None,fl = 0):
         QScrollView.__init__(self,parent,name,fl)
         self.items = {}
         self.setStaticBackground(True)
         self.enableClipper(True)
 
self.viewport().setPaletteBackgroundColor(qApp.palette().color(QPalette.Active, 
QColorGroup.Background))
         self.row_height = 120

     def viewportResizeEvent(self, e):
         for x in self.items:
             self.items[x].resize(e.size().width(), self.row_height)

     def addItem(self, name, title, pix, text, button_text, button_func):
         num_items = len(self.items)
         LayoutWidget = QWidget(self.viewport(),"layoutwidget")
         LayoutWidget.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, 
QSizePolicy.Minimum))
         LayoutWidget.setGeometry(QRect(0, 0, self.width(), 
self.row_height))
         self.addChild(LayoutWidget)

         if num_items:
             self.moveChild(LayoutWidget, 0, self.row_height*num_items)

         layout = QGridLayout(LayoutWidget,1,1,10,10,"layout")

         pushButton = QPushButton(LayoutWidget,"pushButton")
 
pushButton.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Fixed,0,0,
 
pushButton.sizePolicy().hasHeightForWidth()))
         self.connect(pushButton,SIGNAL("clicked()"), button_func)

         layout.addWidget(pushButton,2,2)

         textLabel = QLabel(LayoutWidget,"textLabel")

         layout.addWidget(textLabel,1,1)

         pixmap = QLabel(LayoutWidget,"pixmapLabel2")
 
pixmap.setSizePolicy(QSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed,0,0,
                              pixmap.sizePolicy().hasHeightForWidth()))
         pixmap.setMinimumSize(QSize(32,32))
         pixmap.setMaximumSize(QSize(32,32))
         pixmap.setPixmap(pix)
         pixmap.setScaledContents(1)

         layout.addWidget(pixmap,1,0)

         textLabel2 = QLabel(LayoutWidget,"textLabel2")
         textLabel2.setAlignment(QLabel.WordBreak | QLabel.AlignTop)
 
textLabel2.setSizePolicy(QSizePolicy(QSizePolicy.Minimum,QSizePolicy.Expanding))

         layout.addWidget(textLabel2,2,1)

         if num_items:
             line = QFrame(LayoutWidget,"line")
             line.setFrameShadow(QFrame.Sunken)
             line.setFrameShape(QFrame.HLine)
             layout.addMultiCellWidget(line,0,0,0,2)

         textLabel.setText(title)
         textLabel2.setText(text)
         pushButton.setText(button_text)
         self.resizeContents(self.width(), num_items*self.row_height*2)

         LayoutWidget.show()

         try:
             self.items[name]
         except KeyError:
             self.items[name] = LayoutWidget
         else:
             print "ERROR: Duplicate button name:", name

     def clear(self):
         if len(self.items):
             for x in self.items:
                 self.removeChild(self.items[x])
                 self.items[x].hide()

             self.items.clear()
             self.resizeContents(self.width(), 0)



-Don



More information about the Python-list mailing list