Skip to content

Commit 3a8d922

Browse files
添加屏幕截图示例代码
1 parent 67b0737 commit 3a8d922

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

qt_screenshot/CMakeLists.txt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(qt_clion)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
set(CMAKE_AUTOMOC ON)
6+
set(CMAKE_AUTORCC ON)
7+
set(CMAKE_AUTOUIC ON)
8+
9+
set(QT_VERSION 5)
10+
set(REQUIRED_LIBS Core Gui Widgets)
11+
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
12+
13+
if(WIN32)
14+
set(CMAKE_PREFIX_PATH "D:/software/Qt5.13.0/mingw73_64/lib/cmake/Qt5")
15+
else()
16+
set(CMAKE_PREFIX_PATH "/usr/lib/x86_64-linux-gnu/cmake/Qt5")
17+
endif()
18+
19+
20+
if (NOT CMAKE_PREFIX_PATH)
21+
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
22+
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
23+
endif ()
24+
25+
26+
## 添加用户界面文件
27+
#set(FORMS
28+
# mainwindow.ui
29+
# )
30+
31+
# 自动生成用户界面文件的 C++ 代码
32+
#qt5_wrap_ui(UI_HEADERS ${FORMS})
33+
34+
# 自动生成用户界面文件的 C++ 代码
35+
# 自动生成用户界面文件的 C++ 代码
36+
#set(UI_HEADERS)
37+
if(WIN32)
38+
39+
#foreach(FORM ${FORMS})
40+
# get_filename_component(FORM_BASE ${FORM} NAME_WE)
41+
# set(UI_HEADER "${CMAKE_CURRENT_BINARY_DIR}/ui_${FORM_BASE}.h")
42+
# list(APPEND UI_HEADERS ${UI_HEADER})
43+
# add_custom_command(
44+
# OUTPUT ${UI_HEADER}
45+
# COMMAND ${Qt5_DIR}/../../../bin/uic ${CMAKE_CURRENT_SOURCE_DIR}/${FORM} -o ${UI_HEADER}
46+
# DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FORM}
47+
# )
48+
#endforeach()
49+
50+
else()
51+
# foreach(FORM ${FORMS})
52+
# get_filename_component(FORM_BASE ${FORM} NAME_WE)
53+
# set(UI_HEADER "${CMAKE_CURRENT_BINARY_DIR}/ui_${FORM_BASE}.h")
54+
# list(APPEND UI_HEADERS ${UI_HEADER})
55+
# add_custom_command(
56+
# OUTPUT ${UI_HEADER}
57+
# COMMAND /usr/bin/uic ${CMAKE_CURRENT_SOURCE_DIR}/${FORM} -o ${UI_HEADER}
58+
# DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FORM}
59+
# )
60+
# endforeach()
61+
62+
endif()
63+
64+
65+
add_executable(${PROJECT_NAME}
66+
main.cpp
67+
mainwindow.cpp)
68+
69+
target_include_directories(${PROJECT_NAME}
70+
PUBLIC ./
71+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_autogen/include)
72+
73+
74+
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
75+
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
76+
77+
78+

qt_screenshot/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <QApplication>
2+
#include <QPushButton>
3+
4+
#include "mainwindow.h"
5+
6+
int main(int argc, char *argv[]) {
7+
QApplication a(argc, argv);
8+
QPushButton button("Hello world!", nullptr);
9+
button.resize(200, 100);
10+
button.show();
11+
return QApplication::exec();
12+
}

qt_screenshot/mainwindow.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Created by wangyz38535 on 2023/9/17.
3+
//
4+
5+
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
6+
7+
#include "mainwindow.h"
8+
#include "ui_mainwindow.h"
9+
10+
11+
MainWindow::MainWindow(QWidget *parent) :
12+
QMainWindow(parent), ui(new Ui::MainWindow) {
13+
ui->setupUi(this);
14+
}
15+
16+
MainWindow::~MainWindow() {
17+
delete ui;
18+
}
19+

qt_screenshot/mainwindow.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by wangyz38535 on 2023/9/17.
3+
//
4+
5+
#ifndef TEST_MAINWINDOW_H
6+
#define TEST_MAINWINDOW_H
7+
8+
#include <QMainWindow>
9+
10+
11+
QT_BEGIN_NAMESPACE
12+
namespace Ui { class MainWindow; }
13+
QT_END_NAMESPACE
14+
15+
class MainWindow : public QMainWindow {
16+
Q_OBJECT
17+
18+
public:
19+
explicit MainWindow(QWidget *parent = nullptr);
20+
21+
~MainWindow() override;
22+
23+
private:
24+
Ui::MainWindow *ui;
25+
};
26+
27+
28+
#endif //TEST_MAINWINDOW_H

qt_screenshot/mainwindow.ui

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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>373</width>
10+
<height>314</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
</widget>
17+
<resources/>
18+
<connections/>
19+
</ui>

0 commit comments

Comments
 (0)