PDA

View Full Version : Drawing on QWidget - strech & resize



kemp
22nd January 2007, 07:33
In my application i am drawing directly on QWidget. i have that widget in QMainWindow as
central widget. If i resize my MainWindow i want that the drawing on the widget is
resizing properly. Is there i way to do that with layers, like if i add a QDial to QHBoxLayout
is the widget resizing properly but not my widget with my drawing?

Thank you for your answer.

jpn
22nd January 2007, 07:45
You have to take the geometry into account. Don't use fixed coordinates but calculate them according to the current width and height. QPainter helps you to achieve that transparently. See Window-Viewport Conversion (http://doc.trolltech.com/4.2/coordsys.html#window-viewport-conversion).

kemp
22nd January 2007, 12:44
Thank you for the answer. I applied the analog clock example to my code, but there are still
things that i don't understand.
the ratio of my drawing is correct but I don't know why my drawing is not centered properly.

I can reproduce this on an example:




#include <QLabel>

class Test_Label : public QLabel
{
Q_OBJECT

public:
Test_Label(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *e);
};



#include <QtGui>
#include "test_label.h"

Test_Label::Test_Label(QWidget *parent)
: QLabel(parent)
{

}

void Test_Label::paintEvent(QPaintEvent *e)
{

int side = qMin(width(), height());

QPainter painter(this);

painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);

painter.drawRect(0,0,50,70);

}






The rectangle is always positioned in the lower right corner. I don't understand why.
Could you please help me?

Thanks.

jpn
22nd January 2007, 13:57
I'm not sure how do you want it to look like, but here's an example at simplest:


void SomeWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setWindow(QRect(-50,-50,100,100));
painter.drawRect(-25, -25, 50, 50);
}

PS. I just took a quick look at the Analog Clock example and I find it a bit surprising that it doesn't even use QPainter::setWindow() nor QPainter::setViewport() still if it's linked to the aforementioned "Window-Viewport Conversion" documentation.. :)

kemp
22nd January 2007, 14:36
Thanks for your help. Now the rectangle is in the middle, but when i resize the
window the rectangle also gets deformed, but i want to keep the ratio between
the width() and height() always the same and centered. If i use the set window
my drawing is deformed and if i use:


int side = qMin(width(), height());
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);

in my code, the drawing dissapear and without setWindow the ratio is correct but
the drawing is always in lower right corner.

jpn
22nd January 2007, 14:39
the drawing is always in lower right corner.
Yes, because the painter is translated so that (0,0) is in the center of the widget.