目录
目录
Posts List
  1. QPainter
  2. 绘图设备
  3. QFile
  4. QFileinfo

Qt学习 P5:绘图,文件QFile

QPainter

通过重载绘图事件paintEvent()绘制图形。该事件会自动触发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Widget::paintEvent(QPaintEvent *event) {
QPainter painter(this); // 指定绘图的位置

QPen pen(QColor(255, 0, 0)); // 红色画笔
pen.setWidth(2); // 设置画笔宽度
pen.setStyle(Qt::DotLine); // 设置画笔样式
painter.setPen(pen); // 使用画笔

QBrush brush(Qt::green); // 绿色画刷
brush.setStyle(Qt::Dense7Pattern); // 设置画刷样式
painter.setBrush(brush); // 使用画刷,用于填充封闭区域

painter.drawLine(QPoint(0, 0), QPoint(100, 100)); // 线
painter.drawEllipse(QPoint(100, 100), 50, 50); // 圆
painter.drawEllipse(QPoint(100, 100), 100, 50); // 椭圆
painter.drawRect(QRect(20, 20, 50, 50)); // 矩形
painter.drawText(QRect(10, 200, 150, 50), "好好学习,天天向上"); // 文字
painter.drawPixmap(0, 0, QPixmap(":/img.png")); // 图片
}

QPainter的一些高级设置:

1
2
3
4
5
6
7
8
9
10
11
QPainter painter(this);
painter.drawEllipse(QPoint(100, 50), 50, 50);
painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿,会降低效率
painter.drawEllipse(QPoint(200, 50), 50, 50);

painter.drawRect(QRect(20, 20, 50, 50));
painter.save(); // 暂存状态
painter.translate(100, 0); // 平移painter的参考系原点
painter.drawRect(QRect(20, 20, 50, 50));
painter.restore(); // 恢复状态
painter.drawRect(QRect(0, 0, 50, 50));

手动调用绘图事件:

1
2
3
4
5
6
7
8
9
10
11
12
connect(ui->pushButton, &QPushButton::clicked, [=]() {
posX += 20; // 每次点击按钮平移图形
if (posX > this->width()) posX = 0;
update(); // 重新绘图
});

-----

void Widget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.drawRect(QRect(posX, 0, 100, 100));
}

绘图设备

绘图设备是指继承于QPainterDevice类的子类。

Qt一共提供了四个这样的类,分别是QPixmap、QBitmap、QImage和 QPicture。

  • QPixmap专门为图像在屏幕上的显示做了优化

  • QBitmap是QPixmap的一个子类,它的色深限定为1,可以使用 QPixmap的isQBitmap()函数来确定这个QPixmap是不是一个QBitmap。

  • QImage专门为图像的像素级访问做了优化。

  • QPicture则可以记录和重现QPainter的各条命令。

注:QWidget多重继承于QObject类和QPainterDevice类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
QPixmap pix(300, 300); // 宽、高

QPainter painter(&pix); // 指定绘图设备

pix.fill(Qt::white); // 填充为白色
painter.drawEllipse(QPoint(150, 150), 100, 100);

pix.save("pix.png"); // 保存图片

-----

QImage img(300, 300, QImage::Format_RGB32); // 宽、高、格式
QPainter painter(&img);
img.fill(Qt::white);
painter.drawEllipse(QPoint(150, 150), 100, 100);
img.save("pix.png");

-----

void Widget::paintEvent(QPaintEvent *event) {
QImage img;
img.load("C:\\Users\\zqh-wz\\Desktop\\LearningQT\\build-14_QtPainterDevice-Desktop_Qt_5_9_0_MinGW_32bit-Debug\\pix.png"); // 加载图片

for (int i = 50; i < 100; i++)
for (int j = 50; j < 100; j++) {
QRgb value = qRgb(255, 0, 0);
img.setPixel(QPoint(i, j), value); // 修改像素点
}

QPainter painter(this);
painter.drawImage(0, 0, img);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
QPicture pic;
QPainter painter;

painter.begin(&pic); // 指定绘图设备
painter.setPen(QPen(Qt::cyan));
painter.drawEllipse(QPoint(150, 150), 100, 100);
painter.end();

pic.save("pic.zt"); // 保存绘图指令,后缀名实际上是任意的

---

void Widget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
QPicture pic;
pic.load("C:\\Users\\zqh-wz\\Desktop\\LearningQT\\build-14_QtPainterDevice-Desktop_Qt_5_9_0_MinGW_32bit-Debug\\pic.zt"); // 重现绘图指令
painter.drawPicture(0, 0, pic);
}

QFile

文件操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
connect(ui->pushButton, &QPushButton::clicked, [=]() {
QString filepath = QFileDialog::getOpenFileName(this, "打开文件", "C:\\Users\\zqh-wz\\Desktop");
ui->lineEdit->setText(filepath);

QTextCodec *codec = QTextCodec::codecForName("gbk"); // 编码格式

QFile file(filepath);
file.open(QIODevice::ReadOnly); // 打开方式
QByteArray array = file.readAll(); // 全部读取
ui->textEdit->setText(array); // 默认为utf-8
ui->textEdit->setText(codec->toUnicode(array)); // 使用gbk编码格式打开
file.close();
});

-----

QByteArray array;
while (!file.atEnd()) array += file.readLine(); // 按行读
ui->textEdit->setText(array);

-----

file.open(QIODevice::Append); // 追加写入
file.write("abc");
file.close();

QFileinfo

文件信息读取。

1
2
3
4
QFileInfo fileinfo(filepath);
qDebug() << "size: " << fileinfo.size() << " suffix: " << fileinfo.suffix() << " filename: " << fileinfo.fileName() << " filepath: " << fileinfo.filePath();
qDebug() << "create: " << fileinfo.created().toString("yyyy/MM/dd HH:mm:ss");
qDebug() << "lastmodified: " << fileinfo.lastModified().toString("yyyy-MM-dd HH:mm:ss");