Qt-Qt5截取并保存.png,.bmp等格式图片_#include

 


代码实例:

.pro

1 QT       += core gui
2
3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4
5 CONFIG += c++11
6
7 # The following define makes your compiler emit warnings if you use
8 # any Qt feature that has been marked deprecated (the exact warnings
9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17
18 SOURCES += \
19 main.cpp \
20 mainwindow.cpp
21
22 HEADERS += \
23 mainwindow.h
24
25 FORMS += \
26 mainwindow.ui
27
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
32
33 RESOURCES += \
34

View Code

main.cpp

Qt-Qt5截取并保存.png,.bmp等格式图片_保存图片_02

Qt-Qt5截取并保存.png,.bmp等格式图片_保存图片_03

1 #include "mainwindow.h"
2
3 #include <QApplication>
4
5 int main(int argc, char *argv[])
6 {
7 QApplication a(argc, argv);
8 MainWindow w;
9 w.show();
10 return a.exec();
11

View Code

mainwindow.h

1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <QMainWindow>
5 #include <QDir>
6 #include <QDateTime>
7 #include <QFileDialog>
8
9 QT_BEGIN_NAMESPACE
10 namespace Ui { class MainWindow; }
11 QT_END_NAMESPACE
12
13 class MainWindow : public QMainWindow
14 {
15 Q_OBJECT
16
17 public:
18 MainWindow(QWidget *parent = nullptr);
19 ~MainWindow();
20
21 private slots:
22 void on_pushButton_clicked();
23
24 private:
25 Ui::MainWindow *ui;
26 };
27 #endif // MAINWINDOW_H

View Code

mainwindow.cpp

1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 MainWindow::MainWindow(QWidget *parent)
5 : QMainWindow(parent)
6 , ui(new Ui::MainWindow)
7 {
8 ui->setupUi(this);
9 setWindowTitle(QStringLiteral("Qt5截取并保存.png,.bmp等格式图片"));
10 }
11
12 MainWindow::~MainWindow()
13 {
14 delete ui;
15 }
16
17
18 void MainWindow::on_pushButton_clicked()
19 {
20 QString fileName;
21 QPixmap pix, bmp;
22
23 QString strDir = QCoreApplication::applicationDirPath()+"\\截图\\";
24 QDir dir(strDir);
25
26 if(!dir.exists())
27 {
28 dir.mkdir(strDir);
29 }
30 fileName = strDir + QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss") + ".png";
31 pix = QPixmap::grabWidget(this);
32
33 //this->m_pixMap = QPixmap::grabWindow(QApplication::desktop()->winId(), 0, 0, -1, -1);
34 //this->m_pixMap = QWidget::grab(QRect( QPoint( 0, 0 ), QSize( -1, -1 ) ));
35 // QScreen* screen = QGuiApplication::primaryScreen();
36 // this->m_pixMap = screen->grabWindow(QApplication::desktop()->winId(), 0, 0, -1, -1);
37 // 使用QWidget::grab再QT5中会有警告
38 // this function is deprecated, use QScreen::grabWindow() instead. Defaulting to primary screen.
39
40 // 使用第二种只截取当前App的界面,非桌面全屏
41 // 使用第三种没有警告,且截取的是桌面全屏
42 QString strFile = QFileDialog::getSaveFileName(this,"保存图片",fileName,"PNG(*.png);;BMP(*.bmp);;JPG(*.jpg)");
43 if(!strFile.isNull())
44 {
45 pix.save(strFile);
46 }
47 }
48
49 void MainWindow::on_pushButton_2_clicked()
50 {
51 QString fileName;
52 QPixmap pix, bmp;
53
54 QString strDir = QCoreApplication::applicationDirPath()+"\\截图\\";
55 QDir dir(strDir);
56
57 if(!dir.exists())
58 {
59 dir.mkdir(strDir);
60 }
61 fileName = strDir + QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss") + ".png";
62
63 QScreen* screen = QGuiApplication::primaryScreen();
64 pix = screen->grabWindow(QApplication::desktop()->winId(), 0, 0, -1, -1);
65
66 QString strFile = QFileDialog::getSaveFileName(this,"保存图片",fileName,"PNG(*.png);;BMP(*.bmp);;JPG(*.jpg)");
67 if(!strFile.isNull())
68 {
69 pix.save(strFile);
70 }
71 }
72
73 void MainWindow::on_pushButton_3_clicked()
74 {
75 QString fileName;
76 QPixmap pix, bmp;
77
78 QString strDir = QCoreApplication::applicationDirPath()+"\\截图\\";
79 QDir dir(strDir);
80
81 if(!dir.exists())
82 {
83 dir.mkdir(strDir);
84 }
85 fileName = strDir + QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss") + ".png";
86
87 pix = QWidget::grab(QRect( QPoint( 0, 0 ), QSize( -1, -1 ) ));
88
89 QString strFile = QFileDialog::getSaveFileName(this,"保存图片",fileName,"PNG(*.png);;BMP(*.bmp);;JPG(*.jpg)");
90 if(!strFile.isNull())
91 {
92 pix.save(strFile);
93 }
94 }
95
96 void MainWindow::on_pushButton_4_clicked()
97 {
98 QImage image1;
99 QImage image2;
100 QImage image3;
101
102 image1.load(":/new/prefix1/J10.png");
103 image2.load(":/new/prefix1/J20.png");
104 image3.load("::/new/prefix1/J16.png");
105
106 QString fileName;
107 QPixmap pix, bmp;
108 // QImage pix, bmp;
109
110 QString strDir = QCoreApplication::applicationDirPath()+"\\截图\\";
111 QDir dir(strDir);
112
113 if(!dir.exists())
114 {
115 dir.mkdir(strDir);
116 }
117 fileName = strDir + QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss") + ".png";
118 pix.load(":/new/prefix1/J10.png");
119 QPainter painter(&pix);
120 painter.drawImage(QPoint(0, -10), image1);
121 painter.drawImage(QPoint(0, 5), image2);
122 painter.drawImage(QPoint(0, 10), image3);
123
124 QString strFile = QFileDialog::getSaveFileName(this,"保存图片",fileName,"PNG(*.png);;BMP(*.bmp);;JPG(*.jpg)");
125 if(!strFile.isNull())
126 {
127 pix.save(strFile);
128 }
129
130 ui->pushButton_5->setIcon(QIcon(pix));
131

View Code

mainwindow.ui

1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
3 <class>MainWindow</class>
4 <widget class="QMainWindow" name="MainWindow">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>612</width>
10 <height>293</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>MainWindow</string>
15 </property>
16 <widget class="QWidget" name="centralwidget">
17 <widget class="QPushButton" name="pushButton">
18 <property name="geometry">
19 <rect>
20 <x>430</x>
21 <y>30</y>
22 <width>131</width>
23 <height>20</height>
24 </rect>
25 </property>
26 <property name="text">
27 <string>程序界面PNG图片</string>
28 </property>
29 </widget>
30 <widget class="QLabel" name="label">
31 <property name="geometry">
32 <rect>
33 <x>30</x>
34 <y>20</y>
35 <width>381</width>
36 <height>231</height>
37 </rect>
38 </property>
39 <property name="text">
40 <string/>
41 </property>
42 <property name="pixmap">
43 <pixmap resource="QtSaveToPng.qrc">:/new/prefix1/Earth_Color1_16k.jpeg</pixmap>
44 </property>
45 <property name="scaledContents">
46 <bool>true</bool>
47 </property>
48 </widget>
49 <widget class="QPushButton" name="pushButton_2">
50 <property name="geometry">
51 <rect>
52 <x>430</x>
53 <y>70</y>
54 <width>131</width>
55 <height>20</height>
56 </rect>
57 </property>
58 <property name="text">
59 <string>桌面界面PNG图片</string>
60 </property>
61 </widget>
62 <widget class="QPushButton" name="pushButton_3">
63 <property name="geometry">
64 <rect>
65 <x>430</x>
66 <y>110</y>
67 <width>131</width>
68 <height>20</height>
69 </rect>
70 </property>
71 <property name="text">
72 <string>自定义定PNG图片</string>
73 </property>
74 </widget>
75 <widget class="QPushButton" name="pushButton_4">
76 <property name="geometry">
77 <rect>
78 <x>430</x>
79 <y>150</y>
80 <width>131</width>
81 <height>20</height>
82 </rect>
83 </property>
84 <property name="text">
85 <string>多图合并</string>
86 </property>
87 </widget>
88 <widget class="QPushButton" name="pushButton_5">
89 <property name="geometry">
90 <rect>
91 <x>440</x>
92 <y>200</y>
93 <width>81</width>
94 <height>61</height>
95 </rect>
96 </property>
97 <property name="text">
98 <string>PushButton</string>
99 </property>
100 </widget>
101 </widget>
102 </widget>
103 <resources>
104 <include location="QtSaveToPng.qrc"/>
105 </resources>
106 <connections/>
107

View Code

 

qrc文件不上传了。

 

搜索

复制