#include <QtGui>#include <QPainter>int main(int argc, char **argv) { QApplication app(argc,argv); QWidget wgt; wgt.resize(500,500); QPainter painter; painter.begin(&wgt); painter.setPen(QPen(Qt::black,3)); painter.drawLine(QPointF(5,5),QPointF(100,100)); painter.end(); wgt.show(); return app.exec();}
class PaintWidget : public QWidget{ Q_OBJECT public: PaintWidget(QWidget *parent = 0); void draw( QPainter *painter); protected: void paintEvent(QPaintEvent *event);};PaintWidget::PaintWidget(QWidget *parent) : QWidget(parent){ setPalette(QPalette(QColor(250, 250, 200))); setAutoFillBackground(true);}void PaintWidget::paintEvent(QPaintEvent * /* event */){ QPainter painter(this); draw(&painter);}void PaintWidget::draw( QPainter *painter){ painter->setRenderHint( QPainter ::Antialiasing, true); painter->drawLine(0, 0, 20, 30);}
: QWidget(parent)
#include <QtGui>class myWidget : public QWidget{ public: myWidget (QWidget *parent=0) : QWidget(parent) { } void paintEvent(QPaintEvent *pe) { QPainter p(this); // тут и только тут должны быть все Ваши "рисования" p.drawLine(0,0,100,100); }};int main(int argc, char *argv[]){ QApplication a(argc, argv); myWidget w; w.show(); return a.exec();}
#include <QWidget>class PaintWidget : public QWidget{ Q_OBJECT public: PaintWidget(QWidget *parent = 0); void draw( QPainter *painter); protected: void paintEvent(QPaintEvent *event);};
#include <QPainter>#include "paintwidget.h"PaintWidget::PaintWidget(QWidget *parent) : QWidget(parent){ setPalette(QPalette(QColor(250, 250, 200))); setAutoFillBackground(true);}void PaintWidget::paintEvent(QPaintEvent * /* event */){ QPainter painter(this); draw(&painter);}void PaintWidget::draw( QPainter *painter){ painter->setRenderHint( QPainter ::Antialiasing, true); painter->drawLine(0, 0, 20, 30);}
#include <QtGui>#include "paintwidget.h"int main(int argc, char **argv) { QApplication app(argc,argv); PaintWidget wgt; wgt.resize(500,500); wgt.show(); return app.exec();}
void MyWidget::paintEvent(QPaintEvent *e){ Q_UNUSED(e); QPainter painter(this); if (mPaintRect) { // рисуем прямоугольник painter.drawRect(0, 0, 10, 10); }}void MyWidget::doPaintRect() // слот{ mPaintRect = !mPaintRect; update(); // вызывает paintEvent}