Skip to content

Commit 0e645ef

Browse files
add screen shot code
1 parent c0eb036 commit 0e645ef

11 files changed

+244
-27
lines changed

qt_screenshot/CMakeLists.txt

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(qt_clion)
2+
project(fireshot)
33

44
set(CMAKE_CXX_STANDARD 11)
55
set(CMAKE_AUTOMOC ON)
66
set(CMAKE_AUTORCC ON)
77
set(CMAKE_AUTOUIC ON)
88

9+
set(ui ${CMAKE_SOURCE_DIR}/forms)
10+
set(CMAKE_AUTOUIC_SEARCH_PATHS ${ui})
11+
12+
913
set(QT_VERSION 5)
1014
set(REQUIRED_LIBS Core Gui Widgets Network)
1115
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Network)
1216

17+
#宏定义在这里添加
18+
#add_definitions(
19+
# -D_ENABLE_LOGGING
20+
#)
1321
add_compile_options(-D USE_SPDLOG_)
1422

1523
if(WIN32)
1624
set(CMAKE_PREFIX_PATH "D:/software/Qt5.13.0/mingw73_64/lib/cmake/Qt5")
25+
set(QT_QMAKE_EXECUTABLE "D:/software/Qt/5.13.0/mingw73_64/bin")
1726
else()
1827
set(CMAKE_PREFIX_PATH "/usr/lib/x86_64-linux-gnu/cmake/Qt5")
1928
endif()
@@ -25,6 +34,18 @@ if (NOT CMAKE_PREFIX_PATH)
2534
endif ()
2635

2736

37+
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIB_DIR})
38+
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIB_DIR})
39+
#set(CMAKE_PDB_OUTPUT_DIRECTORY ${LIB_DIR})
40+
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIB_DIR})
41+
42+
#set(RESOURCE_SOURCES
43+
# source/qml/qml.qrc
44+
# # source/image/image.qrc
45+
# )
46+
#set(QML_SOURCES
47+
# source/qml/main.qml)
48+
2849
## 添加用户界面文件
2950
#set(FORMS
3051
# mainwindow.ui
@@ -43,19 +64,38 @@ else(UNIX)
4364

4465
endif()
4566

67+
set(ui_files
68+
${CMAKE_CURRENT_SOURCE_DIR}/forms/tray_settings.ui
69+
)
70+
71+
set(qrc_files
72+
${CMAKE_CURRENT_SOURCE_DIR}/resources/res.qrc)
73+
74+
#find_package(Qt5Widgets REQUIRED QUIET)
75+
#
76+
#QT5_WRAP_UI (project_FORMS_HEADERS forms/tray_setting.ui)
77+
#QT5_ADD_RESOURCES (project_RESOURCE_CPPS **.qrc)
78+
79+
4680
# add core
4781
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/core core_src)
4882
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/utils utils_src)
83+
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/config config_src)
4984

5085
add_executable(${PROJECT_NAME}
5186
main.cpp
5287
fireshot.cpp
5388
${core_src}
5489
${utils_src}
55-
${CMAKE_CURRENT_SOURCE_DIR}/resources/res.qrc
90+
${config_src}
5691
)
5792

58-
93+
target_sources(
94+
${PROJECT_NAME}
95+
PRIVATE
96+
${CMAKE_CURRENT_SOURCE_DIR}/forms/tray_settings.ui
97+
${qrc_files}
98+
)
5999
#find_package(PkgConfig REQUIRED)
60100
#pkg_search_module(GTK REQUIRED gtk+-3.0)
61101
#

qt_screenshot/CMakeLists.txt.user

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.9.1, 2023-10-11T23:52:37. -->
3+
<!-- Written by QtCreator 4.9.1, 2023-10-12T10:15:34. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
7-
<value type="QByteArray">{a33e5fbf-84cc-47c5-88b8-4639e8732808}</value>
7+
<value type="QByteArray">{5f89e11e-2c41-419f-96f7-18ebd964d82f}</value>
88
</data>
99
<data>
1010
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -55,22 +55,24 @@
5555
<data>
5656
<variable>ProjectExplorer.Project.PluginSettings</variable>
5757
<valuemap type="QVariantMap">
58-
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
58+
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
59+
<value type="QString">-fno-delayed-template-parsing</value>
60+
</valuelist>
5961
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
6062
</valuemap>
6163
</data>
6264
<data>
6365
<variable>ProjectExplorer.Project.Target.0</variable>
6466
<valuemap type="QVariantMap">
65-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">桌面</value>
66-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">桌面</value>
67-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{9dfbdac3-1b15-41db-bb48-96d8b0d23b9a}</value>
68-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
67+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.13.0 MinGW 64-bit</value>
68+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.13.0 MinGW 64-bit</value>
69+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5130.win64_mingw73_kit</value>
70+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
6971
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
7072
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
7173
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
7274
<valuelist type="QVariantList" key="CMake.Configuration"/>
73-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_screenshot-unknown-Default</value>
75+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_screenshot-Desktop_Qt_5_13_0_MinGW_64_bit-Default</value>
7476
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
7577
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
7678
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
@@ -114,7 +116,7 @@
114116
<valuelist type="QVariantList" key="CMake.Configuration">
115117
<value type="QString">CMAKE_BUILD_TYPE:STRING=Debug</value>
116118
</valuelist>
117-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_screenshot-unknown-Debug</value>
119+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_screenshot-Desktop_Qt_5_13_0_MinGW_64_bit-Debug</value>
118120
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
119121
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
120122
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
@@ -158,7 +160,7 @@
158160
<valuelist type="QVariantList" key="CMake.Configuration">
159161
<value type="QString">CMAKE_BUILD_TYPE:STRING=Release</value>
160162
</valuelist>
161-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_screenshot-unknown-Release</value>
163+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_screenshot-Desktop_Qt_5_13_0_MinGW_64_bit-Release</value>
162164
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
163165
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
164166
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
@@ -202,7 +204,7 @@
202204
<valuelist type="QVariantList" key="CMake.Configuration">
203205
<value type="QString">CMAKE_BUILD_TYPE:STRING=RelWithDebInfo</value>
204206
</valuelist>
205-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_screenshot-unknown-Release-with-Debug-Information</value>
207+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_screenshot-Desktop_Qt_5_13_0_MinGW_64_bit-Release-with-Debug-Information</value>
206208
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
207209
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
208210
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
@@ -246,7 +248,7 @@
246248
<valuelist type="QVariantList" key="CMake.Configuration">
247249
<value type="QString">CMAKE_BUILD_TYPE:STRING=MinSizeRel</value>
248250
</valuelist>
249-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_screenshot-unknown-Minimum-Size-Release</value>
251+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_screenshot-Desktop_Qt_5_13_0_MinGW_64_bit-Minimum-Size-Release</value>
250252
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
251253
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
252254
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
@@ -356,7 +358,7 @@
356358
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName"></value>
357359
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">qt_clion</value>
358360
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.CMakeRunConfiguration.qt_clion
359-
/work/QT_examples/qt_screenshot/</value>
361+
F:/work/QT_examples/qt_screenshot/</value>
360362
<value type="QString" key="RunConfiguration.Arguments"></value>
361363
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
362364
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
@@ -365,7 +367,7 @@
365367
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
366368
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
367369
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
368-
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/work/QT_examples/build-qt_screenshot-unknown-Debug</value>
370+
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/wangyz38535/AppData/Local/Temp/QtCreator-LEZoch/qtc-cmake-XkbWCzHW</value>
369371
</valuemap>
370372
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
371373
</valuemap>

qt_screenshot/config/tray_setting.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
#include <QDialog>
5+
6+
#include "tray_setting.h"
7+
8+
#include "ui_tray_settings.h"
9+
10+
SettingDlg::SettingDlg(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) {
11+
ui->setupUi(this);
12+
setWindowFlags(windowFlags() | Qt::Tool | Qt::FramelessWindowHint);
13+
14+
15+
16+
}
17+
18+
SettingDlg::~SettingDlg() = default;

qt_screenshot/config/tray_setting.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/10/12.
3+
//
4+
5+
#ifndef QT_CLION_TRY_SETTING_H
6+
#define QT_CLION_TRY_SETTING_H
7+
8+
#include <QDialog>
9+
10+
#include <QPushButton>
11+
#include <QLabel>
12+
#include <QCheckBox>
13+
14+
#include "ui_tray_settings.h"
15+
16+
class SettingDlg : public QDialog {
17+
Q_OBJECT
18+
public:
19+
SettingDlg(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
20+
~SettingDlg() override;
21+
22+
23+
private:
24+
Ui::Settings * ui;
25+
};
26+
27+
28+
#endif //QT_CLION_TRY_SETTING_H

qt_screenshot/core/screen_shot.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#include "screen_shot.h"

qt_screenshot/core/screen_shot.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#ifndef FIRESHOT_SCREEN_SHOT_H
6+
#define FIRESHOT_SCREEN_SHOT_H
7+
8+
9+
class screen_shot {
10+
11+
};
12+
13+
14+
#endif //FIRESHOT_SCREEN_SHOT_H

qt_screenshot/fireshot.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,59 @@
55
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
66

77
#include <QAction>
8+
#include <QMenu>
9+
#include <QDesktopWidget>
810

911
#include "fireshot.h"
1012
#include "utils/spdlog_wrapper.h"
1113

12-
FireShot::FireShot(QWidget *parent) : QDialog(parent) {
14+
15+
16+
FireShot::FireShot(QWidget *parent) :
17+
QDialog(parent), m_lpTrayIcon(new QSystemTrayIcon(this)),
18+
m_lpTrayIconMenu(new QMenu(this)),
19+
m_lpSettingDlg(new SettingDlg(this)) {
1320

1421
}
1522

1623
FireShot::~FireShot() {
1724
SPD_FLUSH();
1825
}
19-
int32_t FireShot::CreateActions() {
2026

27+
void FireShot::OnShowSetting()
28+
{
29+
if (!m_lpSettingDlg->isVisible())
30+
{
31+
m_lpSettingDlg->showNormal();
32+
int w = QApplication::desktop()->width(),
33+
h = QApplication::desktop()->height();
34+
35+
int w1 = m_lpSettingDlg->width(),
36+
h1 = m_lpSettingDlg->height();
37+
38+
m_lpSettingDlg->move((w - w1) / 2, (h - h1) / 2);
39+
}
40+
41+
42+
}
43+
44+
int32_t FireShot::CreateActions() {
45+
// construct tray setting icon
2146
m_lpSettingAction = new QAction(tr("&Settings"), this);
2247
QIcon settingIcon(":/icons/icon/menu-setting.png");
23-
m_lpShotAction; // 截图按钮
24-
m_lpQuitAction; // 退出按钮
48+
m_lpSettingAction->setIcon(settingIcon);
49+
connect(m_lpSettingAction, SIGNAL(triggered()), this, SLOT(OnShowSetting));
50+
// construct tray shot icon
51+
m_lpShotAction = new QAction(tr("S$hot"), this);
52+
QIcon shotIcon(":/icons/icon/menu-shot.png");
53+
m_lpShotAction->setIcon(shotIcon);
54+
connect(m_lpShotAction, &QAction::triggered, this, &FireShot::OnStartShot);
55+
56+
m_lpQuitAction = new QAction(tr("&Quit"), this);
57+
QIcon quitIcon(":/icons/icon/menu-exit.png");
58+
m_lpQuitAction->setIcon(quitIcon);
59+
connect(m_lpQuitAction, &QAction::triggered, this, &FireShot::OnExitShot);
60+
; // 退出按钮
2561

2662
return 0;
2763
}

qt_screenshot/fireshot.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include <QDialog>
99
#include <QSystemTrayIcon>
10+
#include <QDialog>
1011

12+
#include "config/tray_setting.h"
1113

1214
class FireShot : public QDialog {
1315
Q_OBJECT
@@ -16,14 +18,17 @@ Q_OBJECT
1618
explicit FireShot(QWidget *parent = nullptr);
1719
int32_t CreateActions();
1820

21+
void OnShowSetting();
1922

2023
~FireShot() override;
2124

2225
private:
2326
QSystemTrayIcon* m_lpTrayIcon; // 放置一个系统托盘图标
27+
QMenu* m_lpTrayIconMenu;
2428
QAction* m_lpSettingAction; // 系统托盘上的设置按钮
2529
QAction* m_lpShotAction; // 截图按钮
2630
QAction* m_lpQuitAction; // 退出按钮
31+
SettingDlg *m_lpSettingDlg; // tray settings
2732

2833
};
2934

qt_screenshot/fireshot.pro

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2023-09-29T11:06:42
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui widgets network
8+
9+
TARGET = fireshot
10+
TEMPLATE = app
11+
12+
# The following define makes your compiler emit warnings if you use
13+
# any feature of Qt which has been marked as deprecated (the exact warnings
14+
# depend on your compiler). Please consult the documentation of the
15+
# deprecated API in order to know how to port your code away from it.
16+
DEFINES += QT_DEPRECATED_WARNINGS
17+
18+
INCLUDEPATH += 3rd
19+
20+
21+
# You can also make your code fail to compile if you use deprecated APIs.
22+
# In order to do so, uncomment the following line.
23+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
24+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
25+
26+
CONFIG += c++11
27+
28+
SOURCES += \
29+
main.cpp \
30+
fireshot.cpp \
31+
utils/spdlog_wrapper.cpp \
32+
config/tray_setting.cpp \
33+
34+
HEADERS += \
35+
fireshot.h \
36+
utils/spdlog_wrapper.h \
37+
config/tray_setting.h \
38+
39+
FORMS += \
40+
forms/tray_settings.ui \
41+
42+
43+
RESOURCES += resources/res.qrc
44+
45+
46+
# Default rules for deployment.
47+
qnx: target.path = /tmp/$${TARGET}/bin
48+
else: unix:!android: target.path = /opt/$${TARGET}/bin
49+
!isEmpty(target.path): INSTALLS += target

0 commit comments

Comments
 (0)