日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT 中QPainter绘制文字、图片

發(fā)布時(shí)間:2023/12/18 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT 中QPainter绘制文字、图片 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 把字符串繪制在QPixmap上

  • QPixmap MainWindow::text2Pixmap(QString text)

  • {

  • QFontMetrics fmt(m_font);

  • QPixmap result(fmt.width(text), fmt.height());

  • ?
  • QRect rect(0,0,fmt.width(text), fmt.height());

  • result.fill(Qt::transparent);

  • QPainter painter(&result);

  • painter.setFont(m_font);

  • painter.setPen(QColor(255,143,36));

  • //painter.drawText(const QRectF(fmt.width(text), fmt.height()),Qt::AlignLeft, text);

  • painter.drawText((const QRectF)(rect),text);

  • return result;

  • }

  • 2. 對圖片進(jìn)行水平拼接

  • //多張圖片拼接合成一張圖片

  • QPixmap MainWindow::pinjie(QVector<QPixmap> image)

  • {

  • ?
  • int image_width=0;

  • int max_height = 0;

  • QVector <QPixmap > ::iterator it;

  • for(it = image.begin();it!=image.end();++it)

  • {

  • int width = (*it).width();

  • image_width += width;

  • image_width +=5;

  • if((*it).height()>max_height)

  • {

  • max_height =(*it).height();

  • }

  • }

  • QPixmap result_image_h(image_width,max_height);

  • result_image_h.fill(Qt::transparent);

  • QPainter painter_h;

  • painter_h.begin(&result_image_h);

  • int x_number=0;

  • for(it = image.begin();it!=image.end();++it)

  • {

  • painter_h.drawPixmap(x_number,0,(*it));

  • x_number += (*it).width();

  • x_number +=5;

  • }

  • painter_h.end();

  • return result_image_h;

  • }

  • 3. 把一張圖片繪制在空白圖片上

  • QPixmap MainWindow::zoomOutPix(QPixmap pix ,int addWidth, int addHeight)

  • {

  • // addWidth pix增加的寬度,addHeight pix增加的高度

  • QPixmap res(pix.width() + addWidth, pix.height() + addHeight);

  • res.fill(Qt::transparent);

  • QPainter painter(&res);

  • painter.drawPixmap(addWidth/2, addHeight/2, pix.width(), pix.height(), pix);

  • return res;

  • }

  • 4. 把一張圖片繪制在另外一張圖片上

  • //一張圖片繪制在另外一張圖片上

  • QPixmap MainWindow::dijia(QPixmap p1, QPixmap p2)

  • {

  • //p1作為底片, p2繪制在p1圖片上

  • QPainter painter(&p1);

  • painter.drawPixmap(0,0,p2.width(),p2.height(), p2);

  • return p1;

  • ?
  • }

  • 5. 圖片的縮放

    使用scaled函數(shù)進(jìn)行圖片的縮放。

  • //縮放

  • QString str = QString::fromLocal8Bit("(侏羅紀(jì))");

  • QPixmap p = text2Pixmap(str);

  • p = p.scaled(100, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);

  • 6. Qt快捷鍵

    添加注釋: Ctrl + /? ? ? ?

    代碼格式快速對齊:Ctrl? + A ,Ctrl + I

    總結(jié)

    以上是生活随笔為你收集整理的QT 中QPainter绘制文字、图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。