widget.cpp
641 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <QtGui>
#include <QApplication>
#include <QWidget>
class MyWidget:public QWidget
{
int x,y; //end of line position
public:
MyWidget (QWidget * parent = 0):QWidget (parent),x(200),y(100)
{
};
protected:
void paintEvent (QPaintEvent *)
{
QPainter painter (this);
QPen pen;
pen.setWidth(3);
pen.setBrush(Qt::magenta);
painter.setPen(pen);
painter.drawLine(0,0,x,y);
}
void mousePressEvent ( QMouseEvent * event )
{
x=event->x();
y=event->y();
update();
}
};
int
main (int argc, char *argv[])
{
QApplication app (argc, argv);
MyWidget w;
w.resize(200,200);
w.show ();
return app.exec ();
}