Drawing on QWidget - strech & resize
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.
Re: Drawing on QWidget - strech & resize
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.
Re: Drawing on QWidget - strech & resize
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:
Code:
#include <QLabel>
class Test_Label
: public QLabel{
Q_OBJECT
public:
protected:
};
#include <QtGui>
#include "test_label.h"
Test_Label
::Test_Label(QWidget *parent
){
}
{
int side = qMin(width(), height());
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.
Re: Drawing on QWidget - strech & resize
I'm not sure how do you want it to look like, but here's an example at simplest:
Code:
{
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.. :)
Re: Drawing on QWidget - strech & resize
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:
Code:
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.
Re: Drawing on QWidget - strech & resize
Quote:
Originally Posted by
kemp
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.