一个简单的窗口标题栏控件实现

1、瞎叨叨

  • 用于设置了无边框的窗体
  • 可拖动
  • 尺寸只做了最小化、全屏和还原三种
  • 写得很烂
  • 效果:

 2、源代码

头文件:

// TopMenu.h
#pragma once 

#include <QWidget>
#include "ui_TopMenu.h"
#include "qstyle.h"

class TopMenu : public QWidget
{
	Q_OBJECT

public:
	TopMenu(QWidget *parent = nullptr);
	~TopMenu();

protected:
	/// @brief 鼠标按下事件,用于记录鼠标按下位置
	void mousePressEvent(QMouseEvent* e);

	/// @brief 鼠标移动事件,用于指导窗口移动
	void mouseMoveEvent(QMouseEvent* e);

	/// @brief 鼠标双击事件,用于双击放大缩小
	void mouseDoubleClickEvent(QMouseEvent* e);

	/// @brief 事件过滤器,用于让窗口按钮别响应拖动
	bool eventFilter(QObject* obj, QEvent* event) override;

private:
	Ui::TopMenuClass	ui;
	QWidget*			m_parent		= nullptr;	//父控件
	QPoint				m_clickPos;					//鼠标按下的位置
	QPoint				m_sizeChangedPos;			//窗口变换大小的位置
	bool				m_bOnMouseMove;				//是否要响应鼠标移动(用于排除双击引起的鼠标移动事件)

	QIcon				m_icoCLose;
	QIcon				m_icoMin;
	QIcon				m_icoNormal;
	QIcon				m_icoFull;

	bool				m_bIsFull;					//是否全屏
	QSize				m_normalSize;				//原大小

	/// @brief 设置图标用
	void InitUI();

private slots:
	void on_btnMin_clicked();
	void on_btnFull_clicked();
	void on_btnClose_clicked();
};

cpp文件:

// TopMenu.cpp
#include "TopMenu.h"
#include "qevent.h"

TopMenu::TopMenu(QWidget *parent)
	: QWidget(parent)
{
	m_parent = parent;
	ui.setupUi(this);
	InitUI();
	m_bIsFull = m_parent->isFullScreen();
	m_normalSize = m_parent->size();
}

TopMenu::~TopMenu()
{}

void TopMenu::InitUI()
{
	m_icoCLose = QApplication::style()->standardIcon(QStyle::SP_TitleBarCloseButton);
	m_icoFull = QApplication::style()->standardIcon(QStyle::SP_TitleBarShadeButton);
	m_icoMin = QApplication::style()->standardIcon(QStyle::SP_TitleBarMinButton);
	m_icoNormal = QApplication::style()->standardIcon(QStyle::SP_TitleBarNormalButton);
	this->ui.btnClose->setIcon(m_icoCLose);
	this->ui.btnFull->setIcon(m_icoFull);
	this->ui.btnMin->setIcon(m_icoMin);
	this->ui.btnClose->installEventFilter(this);
	this->ui.btnFull->installEventFilter(this);
	this->ui.btnMin->installEventFilter(this);
}

void TopMenu::on_btnMin_clicked()
{
	m_parent->showMinimized();
}

void TopMenu::on_btnFull_clicked()
{
	if (m_bIsFull)
	{
		this->ui.btnFull->setIcon(m_icoFull);
		m_parent->showNormal();
		this->ui.btnFull->setToolTip(QString("Full Screen"));
		m_bIsFull = false;
		m_parent->move(m_sizeChangedPos);
	}
	else
	{
		m_sizeChangedPos = m_parent->pos();
		this->ui.btnFull->setIcon(m_icoNormal);
		m_parent->showMaximized();
		this->ui.btnFull->setToolTip(QString("Normal"));
		this->hide();
		m_bIsFull = true;
	}
}

void TopMenu::on_btnClose_clicked()
{
	m_parent->close();
}

void TopMenu::mousePressEvent(QMouseEvent* e)
{
	m_bOnMouseMove = true;
	if (e->button() == Qt::LeftButton)
	{
		m_clickPos = e->pos();
		if (m_clickPos.y() == 0 && !m_bIsFull)
			this->hide();
	}
}

void TopMenu::mouseMoveEvent(QMouseEvent* e)
{
	if (!m_bOnMouseMove)
		return;
    if (e->buttons() && Qt::LeftButton && !m_parent	->isFullScreen())
        m_parent->move(m_parent->pos() + e->pos() - m_clickPos);
}

void TopMenu::mouseDoubleClickEvent(QMouseEvent* e)
{
	on_btnFull_clicked();
	m_bOnMouseMove = false;
}

bool TopMenu::eventFilter(QObject* obj, QEvent* event)
{
	if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonDblClick)
	{
		if (obj == this->ui.btnClose || obj == this->ui.btnFull || obj == this->ui.btnMin)
		{
			return true;
		}
	}
	return false;
}



 ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TopMenuClass</class>
 <widget class="QWidget" name="TopMenuClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1073</width>
    <height>355</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>0</width>
    <height>30</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>16777215</width>
    <height>16777215</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>TopMenu</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <property name="spacing">
    <number>0</number>
   </property>
   <property name="leftMargin">
    <number>0</number>
   </property>
   <property name="topMargin">
    <number>0</number>
   </property>
   <property name="rightMargin">
    <number>0</number>
   </property>
   <property name="bottomMargin">
    <number>0</number>
   </property>
   <item>
    <widget class="QFrame" name="frame">
     <property name="maximumSize">
      <size>
       <width>16777215</width>
       <height>30</height>
      </size>
     </property>
     <property name="styleSheet">
      <string notr="true">QFrame{
background-color: rgb(0, 85, 255);
font-size:9pt;
font-weight:bold;
font:&quot;黑体&quot;;
color:white;
border:none;
}
QToolButton{background-color: rgb(0, 85, 255);border:none;}
QToolButton:hover{
border-style:dashed;
background:rgb(0, 170, 255);
}</string>
     </property>
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Raised</enum>
     </property>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <property name="spacing">
       <number>0</number>
      </property>
      <property name="sizeConstraint">
       <enum>QLayout::SetNoConstraint</enum>
      </property>
      <property name="leftMargin">
       <number>10</number>
      </property>
      <property name="topMargin">
       <number>0</number>
      </property>
      <property name="rightMargin">
       <number>0</number>
      </property>
      <property name="bottomMargin">
       <number>0</number>
      </property>
      <item>
       <widget class="QLabel" name="label">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="text">
         <string>Windows Name</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>618</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item>
       <widget class="QToolButton" name="btnMin">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Fixed" vsizetype="Expanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>30</width>
          <height>0</height>
         </size>
        </property>
        <property name="toolTip">
         <string>Min</string>
        </property>
        <property name="styleSheet">
         <string notr="true"/>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QToolButton" name="btnFull">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Fixed" vsizetype="Expanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>30</width>
          <height>0</height>
         </size>
        </property>
        <property name="toolTip">
         <string>Full Screen</string>
        </property>
        <property name="styleSheet">
         <string notr="true"/>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QToolButton" name="btnClose">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Fixed" vsizetype="Expanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>30</width>
          <height>0</height>
         </size>
        </property>
        <property name="toolTip">
         <string>Close</string>
        </property>
        <property name="styleSheet">
         <string notr="true">QToolButton:hover{
background:red;
}</string>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值