QImage img;
QPixmap savedPixmap(sourceRect.size());
QPainter savedPainter(&savedPixmap);
// 使用 QPainter 从 bufferPixmap 复制指定区域
savedPainter.drawPixmap(sourceRect,bufferPixmap);
// 将 QPixmap 转换为 QImage
QImage image = savedPixmap.convertToImage();
if (image.save("1.png","PNG"))
{
QMessageBox::information(0, "Save Image", "Image saved successfully.");
}
以上代码中,原来的图片尺寸是950*80,sourceRect为(0,0,237,80),即图片宽度为原来的1/4,高度不变,绘制的时候,并没有按照我预期的剪裁,而是将原来的图片尺寸是950*80压缩变形成237*80,提供的三个函数如下
void QPainter::drawPixmap ( const QPoint & p, const QPixmap & pm, const QRect & sr )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Draws the rectangle sr of pixmap pm with its origin at point p.
void QPainter::drawPixmap ( const QPoint & p, const QPixmap & pm )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Draws the pixmap pm with its origin at point p.
void QPainter::drawPixmap ( const QRect & r, const QPixmap & pm )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Draws the pixmap pm into the rectangle r. The pixmap is scaled to fit the rectangle, if image and rectangle size disagree.
将其中的 savedPainter.drawPixmap(sourceRect,bufferPixmap);改成如下即可:
savedPainter.drawPixmap(QPoint(0, 0), bufferPixmap, sourceRect);
可是我用painter目的是绘制在界面上,那么可以考虑
painter.drawPixmap(0, 0, bufferPixmap,sourceRect.x(),sourceRect.y(),sourceRect.width(),sourceRect.height());
如何遍历字体列表:
// 创建 QFontDatabase 对象
QFontDatabase fontDb;
// 获取系统中可用的字体族列表
QStringList fontFamilies = fontDb.families();
// 遍历并打印字体族名称
for (const QString& family : fontFamilies) {
qDebug(family);
}