1. 把字符串繪製在QPixmap上函數
-
QPixmap MainWindow::text2Pixmap(QString text)
spa -
{
code -
QFontMetrics fmt(m_font);
orm -
QPixmap result(fmt.width(text), fmt.height());
blog -
QRect rect(0,0,fmt.width(text), fmt.height());
圖片 -
result.fill(Qt::transparent);
字符串 -
QPainter painter(&result);
it -
painter.setFont(m_font);
io -
painter.setPen(QColor(255,143,36));
form -
//painter.drawText(const QRectF(fmt.width(text), fmt.height()),Qt::AlignLeft, text);
-
painter.drawText((const QRectF)(rect),text);
-
return result;
-
}
2. 對圖片進行水平拼接
-
//多張圖片拼接合成一張圖片
-
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函數進行圖片的縮放。
-
//縮放
-
QString str = QString::fromLocal8Bit("(侏羅紀)");
-
QPixmap p = text2Pixmap(str);
-
p = p.scaled(100, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
6. Qt快捷鍵
添加註釋: Ctrl + /
代碼格式快速對齊:Ctrl + A ,Ctrl + I