commit 69de5b9bb4923db889b70a1e297f68f5366e61c5 Author: tianzhendong <1203886034@qq.com> Date: Fri Jun 17 11:57:44 2022 +0800 20220617,初稿v1.0.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c96e9d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,84 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe +build*/ +build-* +build-*/ +build* +!.vscode +debug/ +debug +release/ +release +cmake-build*/ +.idea/ + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..92ddf9f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,53 @@ +cmake_minimum_required(VERSION 3.22) +project(CustomWindow) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.14.2/5.14.2/mingw73_64") + +find_package(Qt5 COMPONENTS + Core + Gui + Widgets + REQUIRED) + +include_directories(resources) + +add_executable(CustomWindow src/main.cpp app_win32.rc src/widget.cpp src/widget.h src/widget.ui) +target_link_libraries(CustomWindow + Qt5::Core + Qt5::Gui + Qt5::Widgets + ) + +if (WIN32) + set(DEBUG_SUFFIX) + if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB Core Gui Widgets) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/README.md b/README.md new file mode 100644 index 0000000..b6b16ce --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# qt widget项目模板 +## 环境 +- Clion 2022.1.2 +- qt 5.14.2 +## 实现 +- 去除自带菜单栏 +- 自定义最小化、最大化、关闭按钮 +- 自定义鼠标拖拽功能 +- 项目图标 diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..4f497c5 Binary files /dev/null and b/favicon.ico differ diff --git a/resources/close.png b/resources/close.png new file mode 100644 index 0000000..a8c839d Binary files /dev/null and b/resources/close.png differ diff --git a/resources/fullscreen.png b/resources/fullscreen.png new file mode 100644 index 0000000..80d3ea1 Binary files /dev/null and b/resources/fullscreen.png differ diff --git a/resources/fullscreen2.png b/resources/fullscreen2.png new file mode 100644 index 0000000..74c8bfa Binary files /dev/null and b/resources/fullscreen2.png differ diff --git a/resources/fullscreen3.png b/resources/fullscreen3.png new file mode 100644 index 0000000..466b711 Binary files /dev/null and b/resources/fullscreen3.png differ diff --git a/resources/fullscreen4.png b/resources/fullscreen4.png new file mode 100644 index 0000000..401b0a8 Binary files /dev/null and b/resources/fullscreen4.png differ diff --git a/resources/max.png b/resources/max.png new file mode 100644 index 0000000..bdb8615 Binary files /dev/null and b/resources/max.png differ diff --git a/resources/min.png b/resources/min.png new file mode 100644 index 0000000..fbd7577 Binary files /dev/null and b/resources/min.png differ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..3f6682d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,9 @@ +#include +#include "widget.h" + +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + Widget w; + w.show(); + return QApplication::exec(); +} diff --git a/src/widget.cpp b/src/widget.cpp new file mode 100644 index 0000000..1b33abe --- /dev/null +++ b/src/widget.cpp @@ -0,0 +1,104 @@ +// +// Created by 12038 on 2022/6/15. +// + +// You may need to build the project (run Qt uic code generator) to get "ui_Widget.h" resolved + +#include "widget.h" +#include "ui_widget.h" + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); + //取消菜单栏 + this->setWindowFlags(Qt::FramelessWindowHint); + + //阴影边框效果 + QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(); + shadow->setBlurRadius(10); + shadow->setColor(Qt::black); + shadow->setOffset(0); + + ui->shadowWidget->setGraphicsEffect(shadow); + + //父窗口透明 + this->setAttribute(Qt::WA_TranslucentBackground); + + //最大化最小化关闭功能实现 + connect(ui->btnMax, SIGNAL(clicked()), this, SLOT(btnMaxClickedSlot())); + connect(ui->btnMin, SIGNAL(clicked()), this, SLOT(btnMinClickedSlot())); + connect(ui->btnClose, SIGNAL(clicked()), this, SLOT(btnCloseClickedSlot())); + + ui->btnMin->setStyleSheet("border-image: url(../resources/min.png)"); + ui->btnMax->setStyleSheet("border-image: url(../resources/fullscreen3.png)"); + ui->btnClose->setStyleSheet("border-image: url(../resources/close.png)"); +} + +Widget::~Widget() +{ + delete ui; +} + +void Widget::mousePressEvent(QMouseEvent *event) +{ +// QWidget::mousePressEvent(event); + QPoint mouseStartPoint = event->globalPos(); + QPoint windowLeftTopPoint = this->geometry().topLeft(); + this->mousePosInWindow = mouseStartPoint - windowLeftTopPoint; +} + +void Widget::mouseMoveEvent(QMouseEvent *event) +{ +// QWidget::mouseMoveEvent(event); + if(this->mousePosInWindow == QPoint()) return; + QPoint mousePoint = event->globalPos(); + QPoint windowLeftTopPoint = mousePoint - this->mousePosInWindow; + this->move(windowLeftTopPoint); +} + +void Widget::mouseReleaseEvent(QMouseEvent *) +{ + this->mousePosInWindow = QPoint(); +} + +void Widget::closeEvent(QCloseEvent *event) +{ + QMessageBox::StandardButton button; + button=QMessageBox::question(this,tr("退出程序"),QString(tr("确认退出程序?")),QMessageBox::Yes|QMessageBox::No); + if(button==QMessageBox::No) + { + event->ignore(); // 忽略退出信号,程序继续进行 + } + else if(button==QMessageBox::Yes) + { + event->accept(); // 接受退出信号,程序退出 + } +} + +void Widget::btnMaxClickedSlot() +{ + ui->btnMax->setStyleSheet("border-image: url(../resources/fullscreen4.png)"); + if(this->isMaximized()){ + ui->layoutMain->setMargin(9); + ui->btnMax->setStyleSheet("border-image: url(../resources/fullscreen3.png)"); + this->showNormal(); + } + else{ + ui->layoutMain->setMargin(0); + ui->btnMax->setStyleSheet("border-image: url(../resources/fullscreen4.png)"); + this->showMaximized(); + } +} + +void Widget::btnMinClickedSlot() +{ + this->showMinimized(); +} + +void Widget::btnCloseClickedSlot() +{ + this->close(); +} + diff --git a/src/widget.h b/src/widget.h new file mode 100644 index 0000000..4ad8134 --- /dev/null +++ b/src/widget.h @@ -0,0 +1,45 @@ +// +// Created by 12038 on 2022/6/15. +// + +#ifndef DUPLICATEFILESCHECK_WIDGET_H +#define DUPLICATEFILESCHECK_WIDGET_H + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ +Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +protected: + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + + void closeEvent(QCloseEvent *event); + +private slots: + void btnMaxClickedSlot(); + void btnMinClickedSlot(); + void btnCloseClickedSlot(); + +private: + Ui::Widget *ui; + + QPoint mousePosInWindow = QPoint(); +}; + + +#endif //DUPLICATEFILESCHECK_WIDGET_H diff --git a/src/widget.ui b/src/widget.ui new file mode 100644 index 0000000..fd33aef --- /dev/null +++ b/src/widget.ui @@ -0,0 +1,345 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + Widget + + + + 9 + + + 9 + + + 9 + + + 9 + + + + + #shadowWidget{ + background-color: rgb(255, 255, 255); + border-radius: 5px; + } + + + + + 9 + + + 9 + + + 9 + + + 0 + + + 0 + + + + + 0 + + + 9 + + + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 微软雅黑 + 14 + + + + windowTitle + + + Qt::AlignCenter + + + + + + + + 42 + 42 + + + + + 42 + 42 + + + + + 14 + 50 + false + + + + QPushButton + { + border:none; + } + + QPushButton:hover + { + background-color: rgb(232, 232, 232); + } + QPushButton:pressed + { + background-color: rgb(162, 162, 162); + } + + + + + + + + + + + + + + false + + + + 30 + 42 + + + + + 30 + 42 + + + + + 14 + 50 + false + + + + QPushButton + { + border:none; + } + + QPushButton:hover + { + background-color: rgb(232, 232, 232); + } + QPushButton:pressed + { + background-color: rgb(162, 162, 162); + } + + + + + + + + + + + + + + + 42 + 42 + + + + + 42 + 42 + + + + + 14 + 50 + false + + + + QPushButton + { + border:none; + } + + QPushButton:hover + { + background-color: rgb(232, 232, 232); + } + QPushButton:pressed + { + background-color: rgb(162, 162, 162); + } + + + + + + + + + + + + + + false + + + + 30 + 42 + + + + + 30 + 42 + + + + + 14 + 50 + false + + + + QPushButton + { + border:none; + } + + QPushButton:hover + { + background-color: rgb(232, 232, 232); + } + QPushButton:pressed + { + background-color: rgb(162, 162, 162); + } + + + + + + + + + + + + + + + 42 + 42 + + + + + 42 + 42 + + + + + 14 + 50 + false + + + + QPushButton + { + border:none; + border-top-right-radius: 5px; + } + + QPushButton:hover + { + background-color: rgb(253, 0, 0); + } + QPushButton:pressed + { + background-color: rgb(211, 0, 0); + } + + + + + + + + + + + + + + + + + 0 + 0 + + + + + + + + + + + + + +