By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,496 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,496 IT Pros & Developers. It's quick & easy.

PyQt layout question: QScrollView and QGridLayout?

P: n/a
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

--
Volker Lenhardt
E-Mail: vo*************@uni-duisburg-essen.de
Nov 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
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
Nov 22 '05 #2

P: n/a
On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
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
Nov 22 '05 #3

P: n/a
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
Nov 22 '05 #4

P: n/a
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
Nov 22 '05 #5

P: n/a
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.pal ette().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.M aximum,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.M inimum,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
Nov 22 '05 #6

P: n/a
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.pal ette().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.M aximum,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.M inimum,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
Nov 22 '05 #7

P: n/a
Volker Lenhardt wrote:
Phil Thompson schrieb:
On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
[Using a QGridLayout in a QScrollView]
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.


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

Nov 22 '05 #8

P: n/a
Volker Lenhardt wrote:
Phil Thompson schrieb:
On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
[Using a QGridLayout in a QScrollView]
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.


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

Nov 22 '05 #9

P: n/a
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
Nov 22 '05 #10

P: n/a
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
Nov 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.