PDA

View Full Version : Advice on how to implement a rotating and zooming drawing canvas



ruakuu
2nd October 2009, 08:11
Hi, I been doing a lot of study on Qt but haven't programmed a line yet, because Im researching for the best approach for my first Qt program.

I want a build a drawing software and want some advice for the best aproach. I already studied the scribble and tablet examples so I know how to build the drawing Widget, the thing is that I need it to zoom and rotate with the mouse.

The approach I was thinking of is to set up a scene, and put the canvas widget in it, then set up a view window with the proper events to rotate and zoom the scene.

The other aproach I thought is to do the canvas Widget alone, program a white drawing area, and set up drawing restraints so the brush strokes won't go out of the canvas.

What do you guys think?

wysota
2nd October 2009, 09:11
It's fine. Just to confirm something - implement it using Graphics View.

ruakuu
2nd October 2009, 17:22
It's fine. Just to confirm something - implement it using Graphics View.

Thanks, yeah, Graphics View is what I had in mind for the first approach:
- Create a canvas widget derived from QWidget
- Define a scene (QGraphicsScene)
- Put the canvas widget in the scene (QGraphicsScene::addWidget())
- Define a View (QGraphicsView) and rotate scene with it

wysota
2nd October 2009, 18:30
- Create a canvas widget derived from QWidget
Hold on! What do you want to do that for? Scene is your canvas, you don't need any extra widgets.

ruakuu
2nd October 2009, 19:12
Oh, you mean I can paint(draw) directly in the scene with the mouse or tablet? As far as I know, you could only do that with classes derived from QPaintDevice, you can add items to the scene but not draw directly in it, that is what I readed in the documentation, or am I missing something here?

Unless Im not explaining myself correctly, I want to actually use the mouse or tablet to scketch there.

I was going to make a class derived from QWidget (also using QImage for loading images), to make me a drawing canvas Widget, then put that in the scene and rotate it with a view. I saw this canvas approach in the Scribble and Tablet examples that comes with Qt. Both make a canvas that derives from QWidget.

I want to make something like the little japanese program called Opencanvas.

wysota
2nd October 2009, 19:59
As far as I know, you could only do that with classes derived from QPaintDevice, you can add items to the scene but not draw directly in it, that is what I readed in the documentation, or am I missing something here?
You can add your scribbling as items to the scene. You'd have to do it when subclassing QWidget too, just without any dedicated infrastructure.


I was going to make a class derived from QWidget (also using QImage for loading images), to make me a drawing canvas Widget, then put that in the scene and rotate it with a view. I saw this canvas approach in the Scribble and Tablet examples that comes with Qt.

And how would your paintEvent() for the widget look like?

ruakuu
3rd October 2009, 19:47
And how would your paintEvent() for the widget look like?

For now it will look like this:


void MyCanvas::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), image);
}

ruakuu
8th October 2009, 08:46
After looking at the Pencil animation software I will implement a better aproach, since this is going to be an animation software too.

Rather than use graphic views Im just us a QMatrix on the widget to do the transforms and a combination QPixmap and QImage for the temp image storing. All this because of performance.

wysota
8th October 2009, 17:59
void MyCanvas::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), image);
}

And how do you create the image? So far your widget is equivalent to QGraphicsPixmapItem. What about providing undo/redo functionality?