头文件mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include
#include
class Mywidget : public QWidget
{
Q_OBJECT
public:
explicit Mywidget(QWidget *parent = 0);
void paintEvent(QPaintEvent *e);
signals:
public slots:
};
#endif // MYWIDGET_H
#include "mywidget.h"
#include
#include
Mywidget::Mywidget(QWidget *parent) : QWidget(parent)
{
}
void Mywidget::paintEvent(QPaintEvent *e)
{
QPixmap pixmap(size()); //相当于小汽车,将所画的东西打包塞进小汽车中
//paintEvent需要画笔工具QPainter,在头文件QPainter中
QPainter p(&pixmap); //画在小汽车里边,能有效提高效率
QTransform transform;
//transform可以代替p的translate,因为他会进行一系列复杂的运算,平移、旋转、缩放等等
//虽然p也有scale,rotate,translate等函数
transform.rotate(10);
transform.scale(0.8, 0.8); //缩放功能
//将以下所有的图像旋转10度,改语句后面的所画的图像都要旋转
p.setTransform(transform);
//将下方画的所有图形移动相对应的位置,整体图像平移,只影响这句话之后的语句,知道遇到下一个translate语句
p.translate(100, 100);
//消锯齿,让画出的图形更加美观
p.setRenderHint(QPainter::SmoothPixmapTransform);
//设置画笔属性
p.setPen(QPen(Qt::red, 2, Qt::DashDotLine));
//设置封闭图像的填充颜色,从BrushStyle文件中找,要学会查询函数的使用准则
p.setBrush(Qt::CrossPattern);
//设置字体格式,宋体, 40个像素, 加粗值为100, 斜体
p.setFont(QFont("宋体", 40, 100, true));
QTransform transform2;
transform.scale(0.5, 0.5);
//这里的false代表是和前面的transform相互独立,效果不叠加
p.setTransform(transform2, false);
//画一条直线
p.drawLine(QPoint(0, 0), QPoint(100, 100));
//画一个椭圆,第一个参数为中心点,第二个第三个分别为x和y的轴长,当二者相等时未圆
p.drawEllipse(QPoint(100, 100), 50, 50);
//画字
p.drawText(QPoint(200, 100), "Hello, China!");
//画图
p.drawPixmap(QPoint(230, 300), QPixmap("pic.jpg"));
//画出直角矩形
p.drawRect(QRect(150, 150, 100,60));
//画出有弧的矩形
p.drawRoundedRect(QRect(230, 230, 100,60), 20, 20);
//结束画笔
p.end();
//开始在窗口上画,画什么呢?
p.begin(this);
//前方开始装车的过程,最后一股气打包全部带走,送到窗口
//北京颜色发生了改变,怎么变回来呢?
p.drawPixmap(QPoint(0, 0), pixmap);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Mywidget w;
w.show();
app.exec();
}
注意:提高画图的效率!
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include
#include
#include
class Mywidget : public QWidget
{
Q_OBJECT
public:
explicit Mywidget(QWidget *parent = 0);
void paintEvent(QPaintEvent *ev);
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
QVector> _lines; //画多条线
signals:
public slots:
};
#endif // MYWIDGET_H
#include "mywidget.h"
#include
Mywidget::Mywidget(QWidget *parent) : QWidget(parent)
{
}
void Mywidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
for(int i=0; i<_lines.size(); i++)
{
const QVector &line = _lines.at(i);
for(int j=0; j line;
_lines.append(line);
}
QVector &lastLine = _lines.last();
//记录该条线当前的位置
lastLine.append(ev->pos());
//强制重绘
update();
}
void Mywidget::mousePressEvent(QMouseEvent *ev)
{
//每次点击鼠标都会为之绘画一条新的线,并将该线的起点位置添加到_lines中
QVector line;
_lines.append(line);
//记录下该条线当前的位置
QVector &lastLine = _lines.last();
lastLine.append(ev->pos());
}
void Mywidget::mouseReleaseEvent(QMouseEvent *ev)
{
QVector &lastLine = _lines.last();
lastLine.append(ev->pos());
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Mywidget w;
w.show();
return app.exec();
}
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include
class MyButton : public QWidget
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = 0);
MyButton(const QString& text, QWidget* parent = 0);
QRect _rect;
QString _text;
bool _pressed;
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void paintEvent(QPaintEvent *);
signals:
void clicked();
public slots:
};
#endif // MYBUTTON_H
#include "MyButton.h"
#include
#include
MyButton::MyButton(QWidget *parent) :
QWidget(parent), _rect(0, 0, 100, 30), _text(QString())
{
_pressed = false;
this->setGeometry(_rect);
}
MyButton::MyButton(const QString &text, QWidget *parent):
QWidget(parent), _text(text), _rect(0, 0, 100, 30)
{
_pressed = false;
this->setGeometry(_rect);
}
void MyButton::mousePressEvent(QMouseEvent *)
{
_pressed = true;
update();
}
void MyButton::mouseReleaseEvent(QMouseEvent *ev)
{
_pressed = false;
update();
if(_rect.contains(ev->pos()))
emit clicked();
// callback handler
}
void MyButton::paintEvent(QPaintEvent *)
{
QPainter p(this);
if(_pressed)
p.setBrush(Qt::yellow);
else
p.setBrush(Qt::darkGray);
p.drawRect(_rect);
p.drawText(_rect, _text, QTextOption(Qt::AlignCenter));
}
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include
#include "MyButton.h"
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
MyButton* button;
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
QVector< QVector > _lines;
signals:
public slots:
void slotButtonClicked();
};
#endif // MYWIDGET_H
#include "MyWidget.h"
#include
#include
#include
#include
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
button = new MyButton("MyButton", this);
connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
// button->setGeometry(30, 30, 100, 30);
}
void MyWidget::slotButtonClicked()
{
qDebug() << "button is clicked";
}
void MyWidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
for(int i=0; i<_lines.size(); ++i)
{
const QVector& line = _lines.at(i);
for(int j=0; j line;
_lines.append(line);
}
QVector& lastLine = _lines.last();
lastLine.append(ev->pos());
update();
}
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
QVector line;
_lines.append(line);
QVector& lastLine = _lines.last();
lastLine.append(ev->pos());
}
void MyWidget::mouseReleaseEvent(QMouseEvent *ev)
{
QVector& lastLine = _lines.last();
lastLine.append(ev->pos());
}
#include
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}