【Qt】Pixmap

来源:互联网 发布:时序数据分类基本原理 编辑:程序博客网 时间:2024/05/29 14:18

知识补充:

QPixmap类

http://doc.qt.io/qt-5/qpixmap.html

QPixmap类代表图像,实现在 QtGui共享库中。

   构造函数:

   以下构造函数生成的 QPixmap对象为空图像:

1. QPixmap();     // 构造一个大小为 0 的空图像  
   以下构造函数生成大小的 QPixmap对象,但图像数据未初始化:

1. QPixmap(const QSize &size);   

  // 构造大小为 size 的图像,图像数据未初始化  

2. QPixmap(int width, int height);   

 // 等价于 QPixmap(QSize(width, height));  
  以下构造函数能够从指定的文件中加载图像并生成 QPixmap对象:

1. QPixmap(const QString &filename, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);  
   其各个参数的含义解释如下。

    1) filename: 文件名。

    2) format: 字符串,表示图像文件的格式,如果为 0,将进行自动识别。

    3) flags:表示颜色的转换模式。

    如果图像文件加载失败则产生空图像,这里 flags参数有以下取值。

    1) Qt::AutoColor:由系统自动决定。

    2) Qt::ColorOnly:彩色模式。

    3) Qt::MonoOnly:单色模式。

 

    图像参数

    以下成员函数可以获得 QPixmap对象所表示的图像的相关信息:

1. int depth() const;     // 颜色深度,既每像素所占的比特数  

2. int width() const;     // 图像宽度,单位是像素  

3. int height() const;   // 图像高度,单位是像素  

4. QSize size() cosnt;  // 图像的大小,即 QSize(width(), height());  

5. QRect rect() const;   // 图像的矩形区域,即 QRect(QPoint(0,0),size());  

    加载和保存图像

    用下面的成员函数可以从文件加载图像:

1. bool load(const QString &filename, const char *fornat = 0, Qt::ImageCoversionFlags flags = Qt::AutoColor);  

    这里各个参数的含义与构造函数中一样,返回值为 true表示加载成功,false 表示加载失败。相反的操作是将 Qpixmap 代表的图像保存到文件,可用以下成员函数:

1. bool save(const QString &filename, const char *format = 0, int quality = -1) const;  
    其各个参数及返回值的含义解释如下。

    1) filename:文件名。

    2) format:字符串,表示图像文件的格式,如果为 0,将根据文件名的后缀自动确定文件格式。

    3)quality:对于有损压缩的文件格式来说,它表示图像保存的质量,质量越低压缩率越大。取值范围为 0~100,-1 表示采用默认值。

    4)返回值:true 表示保存成功,false 表示保存失败。

    判断

    以下成员函数可以判断 QPixmap对象是否为空图像:

1. bool isNull() const;     // 判断是否为空图像  

#include "mainwindow.h"#include <QApplication>#include <QPainter>#include <QPainterPath>#include <QPixmap>int main( int argc, char **argv ){  QApplication app( argc, argv );  QPixmap pixmap(200, 200);  /*void QPixmap::fill(const QColor &color = Qt::white)Fills the pixmap with the given color.The effect of this function is undefined when the pixmap is being painted on.*/  pixmap.fill( Qt::white );  //The QPainterPath class provides a container for painting operations, enabling graphical shapes to be constructed and reused.  QPainterPath path;  /*关于函数的函数,自己要多到assist中去看,给的解释是最正确的也是最好的addEllipse就是根据给定区域画椭圆*/  path.addEllipse( 80, 80, 80, 80 );  path.moveTo( 120, 120 );  path.lineTo( 120, 40 );  //上面就已经完成了一条线的操作  /*void QPainterPath::arcTo(qreal x, qreal y, qreal width, qreal height, qreal startAngle, qreal sweepLength)Creates an arc that occupies the rectangle QRectF(x, y, width, height), beginning at the specified startAngle and extending sweepLength degrees counter-clockwise.*/  path.arcTo( 40, 40, 160, 160, 90, 90 );  path.lineTo( 120, 120 );  /*[static] QFont QApplication::font()[static] QFont QApplication::font(const QWidget *widget)[static] QFont QApplication::font(const char *className)前面两种都是返回各自默认的形式,最后一个是根据传入的参数设置字体字体相关的函数有setFont(), fontMetrics(), and QWidget::font().*/  QFont font = QApplication::font();  font.setPixelSize(40);  /*void QPainterPath::addText(qreal x, qreal y, const QFont &font, const QString &text)注意第三个参数就是字体的对象*/  path.addText(20,180,font,"Path" );  QPainter painter( &pixmap );  /*抗锯齿渲染QPainter进行绘制时可以使用QPainter::setRenderHint()函数渲染提示来指定是否要使用坑锯齿功能,其功能主要是对图像的边缘进行平滑处理,使其看起来更加柔和流畅。*/  painter.setRenderHint( QPainter::Antialiasing );  /*void QPainter::setPen(const QPen &pen)void QPainter::setPen(const QColor &color)void QPainter::setPen(Qt::PenStyle style)*/  painter.setPen( Qt::black );  /*   * void QPainter::setBrush(const QBrush &brush)void QPainter::setBrush(Qt::BrushStyle style)  //设置样式,颜色默认为黑色相比较与上面pen,画刷没有直接设置颜色的构造函数,难道是可以传递的时候少几个参数,其他是默认的?*/  painter.setBrush(Qt::gray);  painter.drawPath(path);  //自己在build中看到图片,也就是自己以后要添加图片是要在build目录下面的  pixmap.save( "path.png" );  return 0;}

http://blog.csdn.net/hlb0924/article/details/19069081

0 0
原创粉丝点击