widget.cpp
726 Bytes
#include <QApplication>
#include <QWidget>
#include <QtGui>
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();
}