[PyKDE] QCanvasView C++/PyQt differences?

Lawrence Chai Lawrence.Chai at disney.com
Thu Jun 28 01:25:43 BST 2001


Hello:

I was working with PyQt and was having trouble getting the QCanvasView
widget to resize properly.  I thought it was a problem with the widget,
so I wrote a small C++ program to demonstrate it to Trolltech.  However,
when I ran the C++ version, everything seemed to work fine.  I was
wondering what possible differences there might be and if there was
anything I could do to get the same behavior in the PyQt version as in
the C++ version.  I've attached a small python script as well as the
equivalent C++ source code.  When running, you'll notice that in the C++
version, the QCanvasWidget is the same height as the QLabel and the
QLineEdit, whereas in the python version, the QCanvas widget is much
taller.  Any insight into this would be much appreciated.  Thanks! 
Lawrence.
-------------- next part --------------
import sys

from qt import *


class ColorSlider(QHBox):
    def __init__(self, parent=None):
	QHBox.__init__(self, parent)

	self.label = QLabel(QString("blah"), self)
	self.label.setMinimumWidth(16)

	self.slider = ColorSwatch(self)
	self.setStretchFactor(self.slider, 10)

	self.valueLE = QLineEdit(self)
	self.valueLE.setMaximumWidth(96)

    def resizeEvent(self, event):
	self.slider.resize(self.slider.size())


class ColorSwatch(QWidget):
    def __init__(self, parent):
	QWidget.__init__(self, parent)
	self.colorSwatchCanvasView = ColorSwatchCanvasView(parent)
	self.hSize = self.width() - 4
	self.vSize = self.height() - 4
	self.canvas = QCanvas(self.hSize, self.vSize)
	self.colorSwatchCanvasView.setCanvas(self.canvas)
	self.pixmap = QPixmap(self.hSize, self.vSize)

    def resizeEvent(self, event):
	self.colorSwatchCanvasView.resize(event.size())
	self.hSize = event.size().width() - 4
	self.vSize = event.size().height() - 4
	self.pixmap.resize(self.hSize, self.vSize)
	self.canvas.resize(self.hSize, self.vSize)
	self.canvas.update()

class ColorSwatchCanvasView(QCanvasView):
    def __init__(self, parent):
	QCanvasView.__init__(self, None, parent)
	self.setHScrollBarMode(QScrollView.AlwaysOff)
	self.setVScrollBarMode(QScrollView.AlwaysOff)


if __name__ == '__main__':
    a = QApplication(sys.argv)
    QObject.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))
    w = ColorSlider()
    a.setMainWidget(w)
    w.show()
    a.exec_loop()
-------------- next part --------------
#include <qapplication.h>
#include <qcanvas.h>
#include <qhbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpixmap.h>
#include <qscrollview.h>
#include <qwidget.h>

class ColorSlider;
class ColorSwatch;
class ColorSwatchCanvasView;


class ColorSlider : public QHBox
{
public:
    ColorSlider(QWidget *parent = 0);
    void resizeEvent(QResizeEvent *);

private:
    QLabel *label;
    ColorSwatch *slider;
    QLineEdit *valueLE;
};


class ColorSwatch : public QWidget
{
public:
    ColorSwatch(QWidget *parent);
    void resizeEvent(QResizeEvent *);

private:
    QCanvas *canvas;
    QPixmap *pixmap;
    ColorSwatchCanvasView *colorSwatchCanvasView;
    int hSize;
    int vSize;
};


class ColorSwatchCanvasView : public QCanvasView
{
public:
    ColorSwatchCanvasView(QWidget *parent);
};


int main( int argc, char **argv )
{
     QApplication a(argc,argv);			
    
     ColorSlider cs;
     a.setMainWidget( &cs );
     cs.show();
     return a.exec();
}


ColorSlider::ColorSlider(QWidget * parent) : QHBox(parent)
{
    label = new QLabel(QString("blah"), this);
    label->setMinimumWidth(16);

    slider = new ColorSwatch(this);
    setStretchFactor(slider, 10);

    valueLE = new QLineEdit(this);
    valueLE->setMaximumWidth(96);
}


void
ColorSlider::resizeEvent(QResizeEvent * event){
    slider->resize(slider->size());
}


ColorSwatch::ColorSwatch(QWidget * parent) : QWidget(parent)
{
    colorSwatchCanvasView = new ColorSwatchCanvasView(this);
    hSize = width() - 4;
    vSize = height() - 4;
    canvas = new QCanvas(hSize, vSize);
    colorSwatchCanvasView->setCanvas(canvas);
    pixmap = new QPixmap(hSize, vSize);
}

	
void
ColorSwatch::resizeEvent(QResizeEvent * event)
{
    colorSwatchCanvasView->resize(event->size());
    hSize = event->size().width() - 4;
    vSize = event->size().height() - 4;
    pixmap->resize(hSize, vSize);
    canvas->resize(hSize, vSize);
    canvas->update();
}


ColorSwatchCanvasView::ColorSwatchCanvasView(QWidget * parent) :
    QCanvasView(0, parent)
{
    setHScrollBarMode(QScrollView::AlwaysOff);
    setVScrollBarMode(QScrollView::AlwaysOff);
}


More information about the PyQt mailing list