[PyQt] PyQt newbie needs some help with a small report designer program

Wayne Roberts vweenucf at yahoo.com
Tue Jun 9 23:56:21 BST 2009


Hi all,

I'm new to pyqt and am having a bit of trouble.  I'm trying to make a (_very_) simple report designer.  Right now I'm just trying to get a feel for what I can do in pyqt and so I've laid out a simple program that represents a document with several bands (if you are familiar with Crystal, you've seen the band style designer I'm trying to emulate).  I feel I'm getting closer, but right now i'm just hacking at it so I feel I should ask for some guidance at this point.  A few of the issues I'm having are:

1) Resizer.paintEvent is getting called... a lot.  I'm not really sure why.
2) When I resize the inner window, i don't wan't contents to resize, but if I don't have the inner widget set to resizable, things are worse.  How can I have it both ways?  I guess what I am trying to say is I want the only way to resize a band be via the Resizer (or eventually some scale factor).  Not by dragging the window.  
3) General resizing issues.  Top band doesn't get smaller when I drag the Resizer up.
    When i drag top Resizer down it makes the top one bigger the middle one gets smaller instead of just being pushed down, etc...
    What can I try in order to get the layouts to function like I envision?
    
Any guidance on what I can try to straighten some of these issues out would be appreciated.  Or, if I'm just going about this the wrong way, please let me know (though I've swiped the ideas from several similar projects...)

Thanks,
Lee

Below is the code (hopefully not too long to post here):

#!/usr/bin/env python
 
import sys
from PyQt4 import QtCore, QtGui
 
class CustomWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
           
    def paintEvent(self, event):
        print "paintEvent"
        paint = QtGui.QPainter()
        paint.begin(self)
        size = self.size()
        w = size.width()
        h = size.height()
        pen = QtGui.QPen(QtGui.QColor(20, 20, 20), 1, QtCore.Qt.SolidLine)
        paint.setPen(pen)
        paint.setBrush(QtCore.Qt.NoBrush)
        paint.drawRect(0, 0, w-1, h-1)
        paint.end()
       
class Resizer(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self,parent)
       
        self.setMinimumHeight(5)
        self.setMaximumHeight(5)
        self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
        self.resize(5,50)
        print "Resizer.__init__"
 
    def mouseMoveEvent(self, event):
        event.accept()
        print "here"
        y = event.y()
        self.emit(QtCore.SIGNAL('MovedResizer'), y)
       
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        print "here"
        styles = QtGui.QStyleOption(QtGui.QStyleOption.SO_SizeGrip)
        styles.initFrom(self)
        self.style().drawControl(QtGui.QStyle.CE_Splitter, styles, painter, self)
        self.setBackgroundRole(QtGui.QPalette.Dark)
 
       
class Band(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Band, self).__init__(parent)
        self.rb = Resizer()
        # eventually replace with custom edit control
        self.lb = CustomWidget()
       
        layout = QtGui.QVBoxLayout(self)
        layout.setMargin(0)
        layout.setSpacing(0)
        layout.addWidget(self.lb)
        layout.addWidget(self.rb)       
        #self.setLayout(layout)
        self.connect(self.rb, QtCore.SIGNAL('MovedResizer'), self.changeSize )
       
    def changeSize(self, y):
        print "In changeSize %d" % y
        h = self.lb.height() + y
        if h < 1:
            h = 1
        print "H = %d" % h
        self.lb.setMinimumHeight(h)
        #self.lb.setMaximumHeight(h)
        #self.lb.resize(self.lb.width(), h)
        #self.lb.repaint()
     
       
class Document(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Document, self).__init__(parent)
       
        self.outerLayout = QtGui.QVBoxLayout()
       
        self.myBands = QtGui.QWidget()
        self.myBands.setBackgroundRole(QtGui.QPalette.Base)
        self.layoutBands = QtGui.QVBoxLayout(self.myBands)
        self.layoutBands.setMargin(0)
        self.layoutBands.setSpacing(0)
       
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setBackgroundRole(QtGui.QPalette.Dark)
        self.scrollArea.setWidget(self.myBands)
        self.scrollArea.setWidgetResizable(True)
 
        self.outerLayout.addWidget(self.myBands)
               
    def addBand(self):
        print "adding band"
        self.layoutBands.addWidget(Band())       
   
    def getScrollArea(self):
        return self.scrollArea
       
class MyWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
 
        self.ws = QtGui.QWorkspace(self)
        self.ws.setScrollBarsEnabled(True)
        self.setCentralWidget(self.ws)       
       
        self.doc = Document()
        self.doc.addBand()
        self.doc.addBand()
        self.doc.addBand()
       
        self.ws.addWindow(self.doc.getScrollArea())
       
        self.setWindowTitle(self.tr("Doesn't Quite Work"))
        self.resize(640, 480)
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    sys.exit(app.exec_())


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090609/df5b86a6/attachment-0001.html


More information about the PyQt mailing list