Browse Source

20220617,初稿v1.0.0

master
tianzhendong 2 years ago
commit
69de5b9bb4
  1. 84
      .gitignore
  2. 53
      CMakeLists.txt
  3. 9
      README.md
  4. BIN
      favicon.ico
  5. BIN
      resources/close.png
  6. BIN
      resources/fullscreen.png
  7. BIN
      resources/fullscreen2.png
  8. BIN
      resources/fullscreen3.png
  9. BIN
      resources/fullscreen4.png
  10. BIN
      resources/max.png
  11. BIN
      resources/min.png
  12. 9
      src/main.cpp
  13. 104
      src/widget.cpp
  14. 45
      src/widget.h
  15. 345
      src/widget.ui

84
.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/

53
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
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/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"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()

9
README.md

@ -0,0 +1,9 @@
# qt widget项目模板
## 环境
- Clion 2022.1.2
- qt 5.14.2
## 实现
- 去除自带菜单栏
- 自定义最小化、最大化、关闭按钮
- 自定义鼠标拖拽功能
- 项目图标

BIN
favicon.ico

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
resources/close.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 B

BIN
resources/fullscreen.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 B

BIN
resources/fullscreen2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 555 B

BIN
resources/fullscreen3.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 B

BIN
resources/fullscreen4.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

BIN
resources/max.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

BIN
resources/min.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

9
src/main.cpp

@ -0,0 +1,9 @@
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Widget w;
w.show();
return QApplication::exec();
}

104
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();
}

45
src/widget.h

@ -0,0 +1,45 @@
//
// Created by 12038 on 2022/6/15.
//
#ifndef DUPLICATEFILESCHECK_WIDGET_H
#define DUPLICATEFILESCHECK_WIDGET_H
#include <QMouseEvent>
#include <QWidget>
#include <QPoint>
#include <QGraphicsDropShadowEffect>
#include <QMessageBox>
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

345
src/widget.ui

@ -0,0 +1,345 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="layoutMain">
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<item row="0" column="0">
<widget class="QWidget" name="shadowWidget" native="true">
<property name="styleSheet">
<string notr="true">#shadowWidget{
background-color: rgb(255, 255, 255);
border-radius: 5px;
}
</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="labelTitle">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>微软雅黑</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>windowTitle</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMin">
<property name="minimumSize">
<size>
<width>42</width>
<height>42</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>42</width>
<height>42</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton
{
border:none;
}
QPushButton:hover
{
background-color: rgb(232, 232, 232);
}
QPushButton:pressed
{
background-color: rgb(162, 162, 162);
}
</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMin_2">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>42</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>30</width>
<height>42</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton
{
border:none;
}
QPushButton:hover
{
background-color: rgb(232, 232, 232);
}
QPushButton:pressed
{
background-color: rgb(162, 162, 162);
}
</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMax">
<property name="minimumSize">
<size>
<width>42</width>
<height>42</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>42</width>
<height>42</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton
{
border:none;
}
QPushButton:hover
{
background-color: rgb(232, 232, 232);
}
QPushButton:pressed
{
background-color: rgb(162, 162, 162);
}
</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMin_3">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>42</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>30</width>
<height>42</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton
{
border:none;
}
QPushButton:hover
{
background-color: rgb(232, 232, 232);
}
QPushButton:pressed
{
background-color: rgb(162, 162, 162);
}
</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<property name="minimumSize">
<size>
<width>42</width>
<height>42</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>42</width>
<height>42</height>
</size>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton
{
border:none;
border-top-right-radius: 5px;
}
QPushButton:hover
{
background-color: rgb(253, 0, 0);
}
QPushButton:pressed
{
background-color: rgb(211, 0, 0);
}
</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Loading…
Cancel
Save