⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form.cpp

📁 本文件是<精通QT4编程>的配套源代码
💻 CPP
字号:
#include <QMouseEvent>#include <QPainter>#include <QtDebug>#include "form.h"Form::Form(QWidget * parent)	: QWidget(parent){    setAttribute(Qt::WA_NoBackground);	bDrawing = false;	curShape = Ellipse;	resize(800,600);    bufferImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);    bufferImage.fill(qRgb(255, 255, 255));    tempImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);}void Form::setShape(ShapeType shape){	curShape = shape;}void Form::mousePressEvent( QMouseEvent * event ){	bDrawing = true;	x = event->x();	y = event->y();}void Form::mouseMoveEvent(QMouseEvent *event){	w = event->x() - x;	h = event->y() - y;    tempImage = bufferImage;	paint(tempImage);}void Form::mouseReleaseEvent(QMouseEvent *event){	bDrawing = false;	paint(bufferImage);}void Form::paintEvent(QPaintEvent *event){	QPainter painter(this);	if(bDrawing)    	painter.drawImage(QPoint(0, 0), tempImage);	else    	painter.drawImage(QPoint(0, 0), bufferImage);}void Form::fontChanged(const QFont& font){	textFont = font;}void Form::paint(QImage& image){	QPainter painter(&image);	painter.setPen(curPen);	painter.setBrush(curBrush);	switch(curShape) {	case Rectangle:		painter.drawRect(x, y, w, h);		break;	case Ellipse:		painter.drawEllipse(x, y, w, h);		break;	case Text:		QFontMetrics metrics(textFont);		QRect rect = metrics.boundingRect(textFont.family());		painter.setFont(textFont);		painter.translate(x, y);		painter.scale(w/rect.width(), h/rect.height());		painter.drawText(0, rect.height(), textFont.family());		break;	}			update();}void Form::penChanged(QPen& pen){	curPen = pen;}void Form::brushChanged(QBrush& brush){	curBrush = brush;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -