【Qt】Pixmap

知识补充:

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QT中,可以使用QPixmap和QPainter来绘制直线。可以通过以下步骤实现: 1. 在代码中创建一个QPixmap对象,并设置其大小和格式,例如:QPixmap pixmap(800, 600);。 2. 创建一个QPainter对象,并将其与QPixmap关联,例如:QPainter painter(&pixmap);。 3. 使用QPainter的drawLine()函数绘制直线,指定直线的起点和终点坐标,例如:painter.drawLine(0, 0, 500, 500);。这将在QPixmap上绘制一条从原点到(500, 500)的直线。 4. 最后,在窗口的paintEvent()函数中,将QPixmap绘制到窗口上,例如:painter.drawPixmap(0, 0, pixmap);。 通过以上步骤,您可以在QT中使用QPixmap和QPainter绘制直线。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [QT利用pixmap绘制动图](https://download.csdn.net/download/qq_45945313/12462039)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Qt5.9中在Pixmap图片设备上绘制线段实例](https://blog.csdn.net/naibozhuan3744/article/details/79133322)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值