qt编写一段程序,实现导入图片,可在图片上选择是否添加水印
时间: 2023-12-27 17:05:33 浏览: 50
以下是一个使用Qt编写的简单程序,可以导入图片并在图片上添加水印。
首先,在Qt Creator中创建一个新的Qt Widgets Application项目。在主窗口上添加一个QLabel和一个QPushButton组件,用于显示图像和选择导入图像的按钮。代码如下:
```cpp
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openImage();
private:
QLabel *imageLabel;
QPushButton *openImageButton;
QPixmap currentImage;
};
#endif // MAINWINDOW_H
```
```cpp
//mainwindow.cpp
#include "mainwindow.h"
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
imageLabel = new QLabel(this);
imageLabel->setScaledContents(true);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setAlignment(Qt::AlignCenter);
setCentralWidget(imageLabel);
openImageButton = new QPushButton("Open Image", this);
connect(openImageButton, &QPushButton::clicked, this, &MainWindow::openImage);
statusBar()->addWidget(openImageButton);
}
MainWindow::~MainWindow()
{
}
void MainWindow::openImage()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "", tr("Images (*.png *.xpm *.jpg)"));
if (!fileName.isEmpty()) {
QPixmap newImage(fileName);
if (newImage.isNull()) {
QMessageBox::warning(this, tr("Error"), tr("Cannot load %1.").arg(fileName));
return;
}
currentImage = newImage;
imageLabel->setPixmap(currentImage);
}
}
```
然后,我们添加一个QCheckBox组件,用于选择是否添加水印。我们还将添加一个QPainter和QLineEdit组件,以便用户可以在图像上输入文本作为水印。代码如下:
```cpp
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QPainter>
#include <QLineEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openImage();
void addWatermark(bool checked);
private:
QLabel *imageLabel;
QPushButton *openImageButton;
QCheckBox *watermarkCheckBox;
QLineEdit *watermarkLineEdit;
QPixmap currentImage;
};
#endif // MAINWINDOW_H
```
```cpp
//mainwindow.cpp
#include "mainwindow.h"
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
imageLabel = new QLabel(this);
imageLabel->setScaledContents(true);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setAlignment(Qt::AlignCenter);
setCentralWidget(imageLabel);
openImageButton = new QPushButton("Open Image", this);
connect(openImageButton, &QPushButton::clicked, this, &MainWindow::openImage);
statusBar()->addWidget(openImageButton);
watermarkCheckBox = new QCheckBox("Add Watermark", this);
connect(watermarkCheckBox, &QCheckBox::toggled, this, &MainWindow::addWatermark);
statusBar()->addWidget(watermarkCheckBox);
watermarkLineEdit = new QLineEdit(this);
statusBar()->addWidget(watermarkLineEdit);
}
MainWindow::~MainWindow()
{
}
void MainWindow::openImage()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "", tr("Images (*.png *.xpm *.jpg)"));
if (!fileName.isEmpty()) {
QPixmap newImage(fileName);
if (newImage.isNull()) {
QMessageBox::warning(this, tr("Error"), tr("Cannot load %1.").arg(fileName));
return;
}
currentImage = newImage;
imageLabel->setPixmap(currentImage);
}
}
void MainWindow::addWatermark(bool checked)
{
if (checked) {
QPainter painter(¤tImage);
QColor watermarkColor = QColor(Qt::red);
QFont watermarkFont = QFont("Arial", 30);
painter.setFont(watermarkFont);
painter.setPen(watermarkColor);
painter.drawText(currentImage.width() - watermarkLineEdit->text().length() * 30, currentImage.height() - 30, watermarkLineEdit->text());
imageLabel->setPixmap(currentImage);
} else {
imageLabel->setPixmap(currentImage);
}
}
```
在此示例中,我们将水印添加到图像的右下角。当用户单击复选框时,程序将调用addWatermark()函数,该函数使用QPainter在图像上绘制文本。
这是一个简单的演示,可以帮助你入门Qt图像处理。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)