根据您的要求,以下是一个使用Qt实现的简单示例程序:
#include <QtWidgets>
#include <QPainter>
#include <QFileDialog>
class ImageEditor : public QWidget {
public:
ImageEditor(QWidget *parent = nullptr) : QWidget(parent) {
setWindowTitle("Image Editor");
openButton = new QPushButton("Open");
addButton = new QPushButton("Add");
deleteButton = new QPushButton("Delete");
saveButton = new QPushButton("Save");
toggleButton = new QPushButton("Toggle");
connect(openButton, &QPushButton::clicked, this, &ImageEditor::openImage);
connect(addButton, &QPushButton::clicked, this, &ImageEditor::addContent);
connect(deleteButton, &QPushButton::clicked, this, &ImageEditor::deleteContent);
connect(saveButton, &QPushButton::clicked, this, &ImageEditor::saveImage);
connect(toggleButton, &QPushButton::clicked, this, &ImageEditor::toggleContent);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(openButton);
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(deleteButton);
buttonLayout->addWidget(saveButton);
QVBoxLayout *layout= new QVBoxLayout(this);
layout->addStretch();
layout ->addWidget(togglebutton);
imageLabel.setAlignment(Qt::AlignCenter); // 居中显示图片
QVBoxLayout* mainLayout= new QVBoxLayout;
mainLayout-> addWidget(imageLabel);
mainLayut -> addLayout(buttonlayout);
setLayoout(mainlayou)
}
private slots:
void openImage() {
QString fileName= QFileDialog: getOpenFileName(this,"Open Image",QString(), "BMP files (*.bmp)");
if(!fileName.isEmpty()){
QImage image(fileName);
if (image.isNull()) {
QMessageBox::warning(this, "Error", "Failed to open image.");
return;
}
originalImage = image; // 保存原图
modifiedImage = originalImage.copy(); // 创建一个修改后的副本
drawContent(); // 在图片上绘制内容
imageLabel.setPixmap(QPixmap::fromImage(modifiedImage));
}
}
void addContent() {
if (modifiedImage.isNull()) {
QMessageBox::warning(this, "Error", "No image is opened.");
return;
}
QPainter painter(&modifiedImage);
painter.setPen(Qt::red);
painter.drawLine(10, 10, 100, 100);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 12));
painter.drawText(50, 50, "Hello World");
drawContent();
imageLabel.setPixmap(QPixmap::fromImage(modifiedImage));
}
void deleteContent() {
modifiedimage= originalimage. copy();
drawcontent();
imagelabel.setpixmap(qpixmap:fromimage(modifiedimage));
}
void saveiamge(){
QString fileName= QFileDialog: getSaveFileName(this,"save Image",QString(),"BMP files (*.bmp)");
if(!fileName.isEmpty()){
if (!modifiedImage.save(fileName)) {
QMessageBox::warning(this, "Error", "Failed to save image.");
}
}
}
void togglecontent(){
hidecontent=!hidecontent;
if(hidecontent){
imageLabel.setPixmap(QPixmap::fromImage(originalImage));
}
else{
imageLabel.setPixmap(QPixmap::fromImage(modifiedImage));
}
private:
QImage originalImage;
QImage modifiedImage;
QLabel imageLabel;
QPushButton *openButton;
QPushButton *addButton;
QPushButton *deleteButton;
QPushButton *saveButton;
QPushButton *toggleButton;
bool hideContent = false; // 是否隐藏内容
void drawContent() {
QPainter painter(&modifiedImage);
if (!hideContent) {
painter.setPen(Qt::red);
painter.drawLine(10, 10, 100, 100);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 12));
painter.drawText(50, 50, "Hello World");
}
void keyPressEvent(QKeyEvent *event)
{
// 使用 Delete 键删除选中的直线或文字
if (event->key() == Qt::Key_Delete) {
deletecontent();
}
QWidget::keyPressEvent(event);
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
ImageEditor editor;
editor.show();
return app.exec();
}
这是一个基本的示例程序,用于打开BMP图像文件,添加直线和文字,并可以选择和删除直线和文字。修改后的图片可以保存为同一BMP文件,并在其他软件中查看。另外,界面上的按钮可用于隐藏或取消隐藏添加的直线和文字内容。
请注意,此示例可能需要根据您自己的需求进行进一步定制和扩展。
内容由零声教学AI助手提供,问题来源于学员提问