在头文件mainwindow.h中先声明以下类:
1 #include <QImage>
2 #include <QPixmap>
3 #include <QFileDialog>
4 #include <QMessageBox>
5 #include <QScreen>
6 #include <QGuiApplication>
在私有对象下声明这几个变量,用于存放文件夹地址。
1 /* 保存路径*/
2 QString runPath;
3 QString hglpName;
4 QString hglpPath;
1 runPath = QCoreApplication::applicationDirPath();       //获取exe路径
2 hglpName = "photo";
3 hglpPath = QString("%1/%2").arg(runPath).arg(hglpName);
打开按钮槽函数实现:
 1 void MainWindow::on_pushButton_clicked()
 2 {
 3     QString filename=QFileDialog::getOpenFileName(this,tr("选择图像"),"",tr("Images (*.png *.bmp *.jpg)"));
 4     if(filename.isEmpty())
 5         return;
 6     else
 7     {
 8         QImage img;
 9         if(!(img.load(filename))) //加载图像
10         {
11             QMessageBox::information(this, tr("打开图像失败"),tr("打开图像失败!"));
12             return;
13         }
14         ui->label->setPixmap(QPixmap::fromImage(img.scaled(ui->label->size())));
15     }
16 }
另存为按钮槽函数实现;
1 void MainWindow::on_pushButton_2_clicked()
2 {
3     QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)")); //选择路径
4     QScreen *screen = QGuiApplication::primaryScreen();
5     screen->grabWindow(ui->label->winId()).save(filename1);
6 }
保存按钮槽函数实现;
1 void MainWindow::on_pushButton_3_clicked()
2 {
3     QScreen *screen = QGuiApplication::primaryScreen();
4     screen->grabWindow(ui->label->winId()).save(QString("%1/34.jpg").arg(hglpPath));
5 }
原文:https://www.cnblogs.com/ybqjymy/p/13467911.html