ZBLOG

Qt实现要求: (1) 可打开一幅 BMP 图片。 (2) 可在图片上添加直线和文字。 (3) 可以选择直线或文字,可以删除选中的直线或文字。 (4) 可保存修改后的图片。 ◼ 原图片和添加的直线、文字等内容一并保存到同一 BMP 图片文件, 不能分开保存在不同文件里。 ◼ 用自己编写的软件打开保存的文件,原图片和添加的直线、文字等内 容一并显示。另外,软件界面上提供一个按钮:可以隐藏添加的直线、 文字等内容,只显示原图;可以取消隐藏,恢复显示添加的直线、文 字等内容。 ◼ 用 Wind...

根据您的要求,以下是一个使用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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=10776

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?