ZBLOG

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

根据您的要求,以下是一个使用Qt实现的简单示例程序:

  1. #include <QtWidgets>
  2. #include <QPainter>
  3. #include <QFileDialog>
  4. class ImageEditor : public QWidget {
  5. public:
  6. ImageEditor(QWidget *parent = nullptr) : QWidget(parent) {
  7. setWindowTitle("Image Editor");
  8. openButton = new QPushButton("Open");
  9. addButton = new QPushButton("Add");
  10. deleteButton = new QPushButton("Delete");
  11. saveButton = new QPushButton("Save");
  12. toggleButton = new QPushButton("Toggle");
  13. connect(openButton, &QPushButton::clicked, this, &ImageEditor::openImage);
  14. connect(addButton, &QPushButton::clicked, this, &ImageEditor::addContent);
  15. connect(deleteButton, &QPushButton::clicked, this, &ImageEditor::deleteContent);
  16. connect(saveButton, &QPushButton::clicked, this, &ImageEditor::saveImage);
  17. connect(toggleButton, &QPushButton::clicked, this, &ImageEditor::toggleContent);
  18. QHBoxLayout *buttonLayout = new QHBoxLayout;
  19. buttonLayout->addWidget(openButton);
  20. buttonLayout->addWidget(addButton);
  21. buttonLayout->addWidget(deleteButton);
  22. buttonLayout->addWidget(saveButton);
  23. QVBoxLayout *layout= new QVBoxLayout(this);
  24. layout->addStretch();
  25. layout ->addWidget(togglebutton);
  26. imageLabel.setAlignment(Qt::AlignCenter); // 居中显示图片
  27. QVBoxLayout* mainLayout= new QVBoxLayout;
  28. mainLayout-> addWidget(imageLabel);
  29. mainLayut -> addLayout(buttonlayout);
  30. setLayoout(mainlayou)
  31. }
  32. private slots:
  33. void openImage() {
  34. QString fileName= QFileDialog: getOpenFileName(this,"Open Image",QString(), "BMP files (*.bmp)");
  35. if(!fileName.isEmpty()){
  36. QImage image(fileName);
  37. if (image.isNull()) {
  38. QMessageBox::warning(this, "Error", "Failed to open image.");
  39. return;
  40. }
  41. originalImage = image; // 保存原图
  42. modifiedImage = originalImage.copy(); // 创建一个修改后的副本
  43. drawContent(); // 在图片上绘制内容
  44. imageLabel.setPixmap(QPixmap::fromImage(modifiedImage));
  45. }
  46. }
  47. void addContent() {
  48. if (modifiedImage.isNull()) {
  49. QMessageBox::warning(this, "Error", "No image is opened.");
  50. return;
  51. }
  52. QPainter painter(&modifiedImage);
  53. painter.setPen(Qt::red);
  54. painter.drawLine(10, 10, 100, 100);
  55. painter.setPen(Qt::blue);
  56. painter.setFont(QFont("Arial", 12));
  57. painter.drawText(50, 50, "Hello World");
  58. drawContent();
  59. imageLabel.setPixmap(QPixmap::fromImage(modifiedImage));
  60. }
  61. void deleteContent() {
  62. modifiedimage= originalimage. copy();
  63. drawcontent();
  64. imagelabel.setpixmap(qpixmap:fromimage(modifiedimage));
  65. }
  66. void saveiamge(){
  67. QString fileName= QFileDialog: getSaveFileName(this,"save Image",QString(),"BMP files (*.bmp)");
  68. if(!fileName.isEmpty()){
  69. if (!modifiedImage.save(fileName)) {
  70. QMessageBox::warning(this, "Error", "Failed to save image.");
  71. }
  72. }
  73. }
  74. void togglecontent(){
  75. hidecontent=!hidecontent;
  76. if(hidecontent){
  77. imageLabel.setPixmap(QPixmap::fromImage(originalImage));
  78. }
  79. else{
  80. imageLabel.setPixmap(QPixmap::fromImage(modifiedImage));
  81. }
  82. private:
  83. QImage originalImage;
  84. QImage modifiedImage;
  85. QLabel imageLabel;
  86. QPushButton *openButton;
  87. QPushButton *addButton;
  88. QPushButton *deleteButton;
  89. QPushButton *saveButton;
  90. QPushButton *toggleButton;
  91. bool hideContent = false; // 是否隐藏内容
  92. void drawContent() {
  93. QPainter painter(&modifiedImage);
  94. if (!hideContent) {
  95. painter.setPen(Qt::red);
  96. painter.drawLine(10, 10, 100, 100);
  97. painter.setPen(Qt::blue);
  98. painter.setFont(QFont("Arial", 12));
  99. painter.drawText(50, 50, "Hello World");
  100. }
  101. void keyPressEvent(QKeyEvent *event)
  102. {
  103. // 使用 Delete 键删除选中的直线或文字
  104. if (event->key() == Qt::Key_Delete) {
  105. deletecontent();
  106. }
  107. QWidget::keyPressEvent(event);
  108. }
  109. };
  110. int main(int argc, char **argv) {
  111. QApplication app(argc, argv);
  112. ImageEditor editor;
  113. editor.show();
  114. return app.exec();
  115. }

这是一个基本的示例程序,用于打开BMP图像文件,添加直线和文字,并可以选择和删除直线和文字。修改后的图片可以保存为同一BMP文件,并在其他软件中查看。另外,界面上的按钮可用于隐藏或取消隐藏添加的直线和文字内容。

请注意,此示例可能需要根据您自己的需求进行进一步定制和扩展。


内容由零声教学AI助手提供,问题来源于学员提问

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?