parent
dba2ffa1a7
commit
b0462c2fc4
@ -0,0 +1,36 @@
|
|||||||
|
#include <QApplication>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QPixmap>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
// 将矩形范围内绘制为指定颜色的矩形,并添加透明度
|
||||||
|
void drawRectWithTransparency(QPixmap& pixmap, const QRect& rect, const QColor& color, int transparency)
|
||||||
|
{
|
||||||
|
QPainter painter(&pixmap);
|
||||||
|
painter.setCompositionMode(QPainter::CompositionMode_Clear);
|
||||||
|
painter.fillRect(rect, QColor(0, 0, 0, 0));
|
||||||
|
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
||||||
|
painter.fillRect(rect, QColor(color.red(), color.green(), color.blue(), transparency));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
// 创建窗口和标签
|
||||||
|
QWidget window;
|
||||||
|
QLabel label(&window);
|
||||||
|
|
||||||
|
// 加载图片
|
||||||
|
QPixmap pixmap(":1.png");
|
||||||
|
QRect rect(360, 360, 300, 250);
|
||||||
|
drawRectWithTransparency(pixmap, rect, QColor(255, 0, 0), 100);
|
||||||
|
label.setPixmap(pixmap);
|
||||||
|
|
||||||
|
// 显示窗口和标签
|
||||||
|
window.show();
|
||||||
|
label.show();
|
||||||
|
|
||||||
|
return app.exec();
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
#include <QApplication>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QPixmap>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
|
||||||
|
void drawRectOnImage(QString imagePath, QRect rect1, QRect rect2, QColor color1, QColor color2)
|
||||||
|
{
|
||||||
|
// 加载图片
|
||||||
|
QPixmap pixmap(imagePath);
|
||||||
|
|
||||||
|
// 指定范围并绘制透明红色和黑色矩形
|
||||||
|
QPainter painter(&pixmap);
|
||||||
|
painter.setCompositionMode(QPainter::CompositionMode_Clear);
|
||||||
|
painter.fillRect(rect1, QColor(0, 0, 0, 0));
|
||||||
|
painter.fillRect(rect2, color2); // 黑色矩形
|
||||||
|
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
||||||
|
painter.fillRect(rect1, color1);
|
||||||
|
|
||||||
|
// 显示图片
|
||||||
|
QWidget *mainWindow = new QWidget();
|
||||||
|
QVBoxLayout *layout = new QVBoxLayout(mainWindow);
|
||||||
|
QLabel *label = new QLabel(mainWindow);
|
||||||
|
layout->addWidget(label);
|
||||||
|
label->setFixedSize(pixmap.size());
|
||||||
|
label->setPixmap(pixmap);
|
||||||
|
mainWindow->show();
|
||||||
|
|
||||||
|
// 进入主事件循环
|
||||||
|
QApplication::exec();
|
||||||
|
delete mainWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
// 绘制矩形到图片中,并在标签上显示图片
|
||||||
|
QRect rect1(360, 360, 300, 250);
|
||||||
|
QRect rect2(400, 400, 300, 250);
|
||||||
|
QColor color1(255, 0, 0, 100);
|
||||||
|
QColor color2(0, 0, 255, 100);
|
||||||
|
drawRectOnImage(":1.png", rect1, rect2, color1, color2);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in new issue