《学习Qt之路2》笔记:Qt之各种画笔、画刷和绘制

这里直接调用了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();
}


你可能感兴趣的:(《学习Qt之路2》笔记:Qt之各种画笔、画刷和绘制)