[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    How can I resize a QCanvas when it's QCanvasView is resized?
From:       Jeff McLean <9jrm4 () qlink ! queensu ! ca>
Date:       2003-06-02 6:48:21
[Download RAW message or body]

Hi,

Sorry to ask a strait-QT/KDE question I hope that is OK.

Again, I'm working on learning the required skills to make my drawing program, 
and I have a QCanvasView and it's associated QCanvas.  I have a few 
QCanvasItems that I can drag around, and it's working well.  But, when I 
resize the window, the canvas doesn't resize.  I know the QCanvasVIew does, 
because I added a printf line to its sizePolicy().   I can see that the 
canvas is not getting resized because I can't drag the items past where the 
original window border was.

I would guess that you don't even need to see the code, but it's at the end of 
this message anyways. 

Thanks a lot.

Jeff


earthview.h:

#ifndef EARTHVIEW_H
#define EARTHVIEW_H

#include <qwidget.h>
#include <qcanvas.h>
#include <qevent.h>

class EarthView : public QCanvasView {
	Q_OBJECT
public:
	EarthView(QCanvas *_canvas, QWidget *parent = 0, const char *name = 0);
	QSizePolicy sizePolicy() const;
public slots:
	void slotNewEarth();
protected:
	QCanvasItemList *items;
	void contentsMousePressEvent(QMouseEvent *);
	void contentsMouseMoveEvent(QMouseEvent *);
	void contentsMouseReleaseEvent(QMouseEvent *);

	QPoint previousMousePosition;

	private:
		QCanvas *canvas;
};

#endif






earthview.cpp:

#include "earthview.h"
#include "ball.h"

#include <qwidget.h>
#include <qlayout.h>
#include <qcanvas.h>
#include <qevent.h>

EarthView::EarthView(QCanvas *_canvas, QWidget *parent, const char *name) : 
QCanvasView(_canvas, parent, name) {
	items = new QCanvasItemList();
	canvas = _canvas;
}

void EarthView::slotNewEarth() {
	printf("Got another new earth request\n");
	Ball *newEarth = new Ball(new QCanvasPixmapArray("earth.png"), canvas);
  newEarth->move(75, 75);
  newEarth->show();
	canvas->update();
	items->append(newEarth);
}

QSizePolicy EarthView::sizePolicy() const {
	//printf("Reziese\n");
	printf("height=%d width=%d\n", height(), width());
	//canvas->resize(height(), width());
	//canvas->update();
	return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
}

void EarthView::contentsMousePressEvent(QMouseEvent *e) {
	previousMousePosition = e->pos();
}

void EarthView::contentsMouseMoveEvent(QMouseEvent *e) {
	QCanvasItemList collisions = canvas->collisions(e->pos());
	if (!collisions.isEmpty()) {
		Ball *first = (Ball*)collisions.first();
		first->moveBy(e->pos().x() - previousMousePosition.x(), e->pos().y() - 
previousMousePosition.y());
		canvas->update();
		previousMousePosition = e->pos();
	}
}

void EarthView::contentsMouseReleaseEvent(QMouseEvent *e) {

}



main.cpp:

#include <qwidget.h>
#include <qvbox.h>
#include <qpushbutton.h>
#include <qapplication.h>
#include <qlayout.h>
#include <qcanvas.h>

#include "earthview.h"
#include "ball.h"



class MyWidget : public QWidget {
public:
	MyWidget(QWidget *parent = 0, const char *name = 0);
	QCanvas *getCanvas() const;
protected:
	QVBoxLayout *partsLayout;
private:
	QCanvas *canvas;
};

MyWidget::MyWidget(QWidget *parent, const char *name) : QWidget(parent, name) 
{
	setPalette(QPalette(Qt::white));
	canvas = new QCanvas(this, "canvas");
	canvas->setBackgroundColor(Qt::black);
  canvas->resize(500, 500);

	partsLayout = new QVBoxLayout(this);
	EarthView *earthView = new EarthView(canvas, this, "earthView");
	QPushButton *newEarth = new QPushButton("Add New Earth", this, "newEarth");
	partsLayout->addWidget(earthView);
	partsLayout->addWidget(newEarth);
	connect(newEarth, SIGNAL(clicked()), earthView, SLOT(slotNewEarth()));
}

QCanvas *MyWidget::getCanvas() const { return canvas; }

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

_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic