『Qtの道を学ぶ2』ノート:Qtの各種ブラシ、ブラシ、描画
2433 ワード
ここではPaintEvent()イベントを直接呼び出します.
コードは次のとおりです.
コードは次のとおりです.
//paintedwidget.h
#ifndef PAINTEDWIDGET_H
#define PAINTEDWIDGET_H
#include <QtWidgets/QWidget>
#include "ui_paintedwidget.h"
#include <QPainter>
class PaintedWidget : public QWidget
{
Q_OBJECT
public:
PaintedWidget(QWidget *parent = 0);
~PaintedWidget();
protected:
void paintEvent(QPaintEvent *);
private:
Ui::PaintedWidgetClass ui;
};
#endif // PAINTEDWIDGET_H
//paintedwidget.cpp
#include "paintedwidget.h"
PaintedWidget::PaintedWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
resize(800, 600);
setWindowTitle(tr("Paint Demo"));
}
PaintedWidget::~PaintedWidget()
{
}
void PaintedWidget::paintEvent(QPaintEvent *)
{
/////////
/*QPainter painter(this);
painter.setPen(QPen(Qt::black, 5, Qt::DashDotLine, Qt::RoundCap));
painter.setBrush(Qt::yellow);
painter.drawEllipse(50, 150, 200, 150);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 5, Qt::DashDotLine, Qt::RoundCap));
painter.setBrush(Qt::yellow);
painter.drawEllipse(400,150, 200, 150);*/
////////
//QPainter painter(this);
//painter.setRenderHint(QPainter::Antialiasing, true);
//QLinearGradient linearGradient(60, 50, 200, 200);//
//linearGradient.setColorAt(0.2, Qt::white);
//linearGradient.setColorAt(0.6, Qt::green);
//linearGradient.setColorAt(1, Qt::black);
//painter.setBrush(QBrush(linearGradient));
//painter.drawEllipse(50, 50, 200, 150);
////////
//QPainter painter(this);
//painter.setRenderHint(QPainter::Antialiasing, true);
//const int r = 150;
//QConicalGradient conicalGradient(0, 0, 0); // ,
//conicalGradient.setColorAt(0.0, Qt::red);
//conicalGradient.setColorAt(60.0/360.0, Qt::yellow);
//conicalGradient.setColorAt(120.0/360.0, Qt::green);
//conicalGradient.setColorAt(180.0/360.0, Qt::cyan);
//conicalGradient.setColorAt(240.0/360.0, Qt::blue);
//conicalGradient.setColorAt(300.0/360.0, Qt::magenta);
//conicalGradient.setColorAt(1.0, Qt::red);
//painter.translate(r, r); // (X, Y)
//QBrush brush(conicalGradient);
//painter.setPen(Qt::NoPen);
//painter.setBrush(brush);
//painter.drawEllipse(QPoint(0, 0), r , r);
}
#include "paintedwidget.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PaintedWidget w;
w.show();
return a.exec();
}