PyQt layout question: QScrollView and QGridLayout?

V

Volker Lenhardt

For a QApplication (PyQt) on the small screen of my Zaurus 5500 PDA I
try to layout my data output in a QScrollView as the central widget. I'd
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.

Best wishes
Volker
 
P

Phil Thompson

For a QApplication (PyQt) on the small screen of my Zaurus 5500 PDA I
try to layout my data output in a QScrollView as the central widget. I'd
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
 
V

Volker Lenhardt

Phil said:
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
 
D

dwelch

Volker said:
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
 
D

David Boddie

[Using a QGridLayout in a QScrollView]
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.

You can still use QGridLayout, but you need to put a widget into the
QScrollView in what the documentation describes as "Using One Big
Widget" (http://doc.trolltech.com/3.3/qscrollview.html).

Something along these lines should do basically what you want:

sv = QScrollView()
w = QWidget(sv.viewport())
sv.addChild(w)

grid = QGridLayout(w, 3, 3)

for i in range(0,3):
for j in range(0,3):
grid.addWidget(QLabel("(%i,%i)" % (i,j), w), i, j)

Of course, the widget that contains the layout is only as large as it
needs to be, so you may need to call its setMinimumSize() method if
you want it to be larger. However, it's often a better idea to ensure
that the child widgets have the sizes they need - their parent will
then be as large as they require.

Hope this helps,

David
 
V

Volker Lenhardt

Thank you Don, thank you David,

I was convinced that there must be a simple solution at hand. A dummy
widget!

It does work to my needs. Don's ScrollToolView is very interesting
though not yet the right tool for my actual application, but I've got
some more ideas for long winter nights ...

All the best
Volker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top