目前还没有达到自己满意的地步,魔方别人写的的,先提供参考,后面在加入新的东西

头文件

#ifndef TITLEBAR_H
#define TITLEBAR_H #include <QWidget> class QLabel;
class QPushButton; class TitleBar : public QWidget
{
Q_OBJECT
public:
explicit TitleBar(QWidget *parent = 0);
~TitleBar(); protected:
/*
* 双击标题栏进行界面最大化/还原
*/
virtual void mouseDoubleClickEvent(QMouseEvent *event);
/*
* 进行鼠标界面的拖动
*/
virtual void mousePressEvent(QMouseEvent *event);
/*
* 设置界面标题与图标
*/
virtual bool eventFilter(QObject *watched, QEvent *event); private slots:
/*
* 进行最小化、最大化/还原。关闭操作
*/
void onClicked();
private:
/*
* 最大化/还原
*/
void updateMaximize();
private:
QLabel* m_pIconLabel;
QLabel* m_pTitleLabel;
QPushButton* m_pMiniMizeButton;
QPushButton* m_pMaximizeButton;
QPushButton* m_pCloseButton;
}; #endif // TITLEBAR_H

cpp文件

#include "titlebar.h"
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>
#include <QSizePolicy>
#include <QIcon>
#ifdef Q_OS_WIN
#pragma comment(lib,"user32.lib")
#include <qt_windows.h>
#endif TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
/*
* 设置标题栏高度
*/
this->setFixedHeight(30);
/*
* 初始化标题栏Button及Lable
*/
m_pIconLabel = new QLabel(this);
m_pTitleLabel = new QLabel(this);
m_pMiniMizeButton = new QPushButton(this);
m_pMaximizeButton = new QPushButton(this);
m_pCloseButton = new QPushButton(this); m_pIconLabel->setFixedSize(20,20);
m_pIconLabel->setScaledContents(true); m_pTitleLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); m_pCloseButton->setFixedSize(27,22);
m_pMaximizeButton->setFixedSize(27,22);
m_pMiniMizeButton->setFixedSize(27,22); m_pTitleLabel->setObjectName("whiteLabel");
m_pMiniMizeButton->setObjectName("minimizeButton");
m_pMaximizeButton->setObjectName("maximizeButton");
m_pCloseButton->setObjectName("closeButton"); m_pMaximizeButton->setToolTip("Maximize");
m_pMiniMizeButton->setToolTip("Minimize");
m_pCloseButton->setToolTip("Close"); QHBoxLayout* pLayout = new QHBoxLayout(this);
pLayout->addWidget(m_pIconLabel);
pLayout->addSpacing(5);
pLayout->addWidget(m_pTitleLabel);
pLayout->addWidget(m_pMiniMizeButton);
pLayout->addWidget(m_pMaximizeButton);
pLayout->addWidget(m_pCloseButton);
pLayout->setSpacing(0); this->setLayout(pLayout); connect(m_pMiniMizeButton,SIGNAL(clicked(bool)),this,SLOT(onClicked()));
connect(m_pMaximizeButton,SIGNAL(clicked(bool)),this,SLOT(onClicked()));
connect(m_pCloseButton,SIGNAL(clicked(bool)),this,SLOT(onClicked())); } TitleBar::~TitleBar()
{ } void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event); emit m_pMaximizeButton->click();
} void TitleBar::mousePressEvent(QMouseEvent *event)
{
#ifdef Q_OS_WIN
if(ReleaseCapture())
{
QWidget* pWindow = this->window();
if(pWindow->isTopLevel())
{
SendMessage(HWND(pWindow->winId()),WM_SYSCOMMAND,SC_MOVE + HTCAPTION,0);
}
}
event->ignore();
#else
#endif } bool TitleBar::eventFilter(QObject *watched, QEvent *event)
{
switch (event->type()) {
case QEvent::WindowTitleChange:
{
QWidget* pWidget = qobject_cast<QWidget*>(watched);
if(pWidget)
{
m_pTitleLabel->setText(pWidget->windowTitle());
return true;
}
}
case QEvent::WindowIconChange:
{
QWidget* pWidget = qobject_cast<QWidget*>(watched);
if(pWidget)
{
QIcon icon = pWidget->windowIcon();
m_pIconLabel->setPixmap(icon.pixmap(m_pIconLabel->size()));
return true;
}
}
case QEvent::WindowStateChange:
case QEvent::Resize:
{
updateMaximize();
return true;
}
}
return QWidget::eventFilter(watched,event);
} void TitleBar::onClicked()
{
QPushButton* pButton = qobject_cast<QPushButton*>(sender());
QWidget* pWidget = this->window();
if(pWidget->isTopLevel())
{
if(pButton == m_pMiniMizeButton)
{
pWidget->showMinimized();
}
else if(pButton == m_pMaximizeButton)
{
pWidget->isMaximized()?pWidget->showNormal():pWidget->showMaximized();
}
else if(pButton == m_pCloseButton)
{
pWidget->close();
}
} } void TitleBar::updateMaximize()
{
QWidget* pWidget = this->window();
if(pWidget->isTopLevel())
{
bool bMaximize = pWidget->isMaximized();
if(bMaximize)
{
m_pMaximizeButton->setToolTip("Restore");
m_pMaximizeButton->setProperty("maximizePorperty","restore");
}
else
{
m_pMaximizeButton->setToolTip("Maximize");
m_pMaximizeButton->setProperty("maximizePorperty","maximize");
}
m_pMaximizeButton->setStyle(QApplication::style());
}
}

使用

#include "widget.h"
#include "ui_widget.h"
#include "titlebar.h"
#include <QPalette>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint | windowFlags()); //remove the title bar TitleBar* pTitleBar = new TitleBar(this);
this->installEventFilter(pTitleBar); this->resize(400,300);
this->setWindowTitle("Thunder");
// this->setWindowIcon(QIcon());
QPalette pal(palette());
pal.setColor(QPalette::Background,QColor(50,50,50));
setAutoFillBackground(true);
setPalette(pal); QVBoxLayout *pLayout = new QVBoxLayout();
pLayout->addWidget(pTitleBar);
pLayout->addStretch();
pLayout->setSpacing(0);
pLayout->setContentsMargins(0,0,0,0);
this->setLayout(pLayout); } Widget::~Widget()
{
delete ui;
}

这里的效果还是有问题的,还在排错中

最新文章

  1. 学习swift开源项目
  2. 字节对齐导致的iOS EXC_ARM_DA_ALIGN崩溃
  3. ACdrea 1217---Cracking&#39; RSA(高斯消元)
  4. 利用jdbc处理oracle大数据---大文件和二进制文件
  5. bzoj4702: 装箱游戏
  6. 容易被忽略的事----sql语句中select语句的执行顺序
  7. HDOJ 2030 汉字统计
  8. 第三篇——第二部分——第一文 SQL Server镜像简介
  9. 很酷的CSS3仿Facebook登录表单
  10. [ZZ] python 语言技巧
  11. CSS学习小记
  12. NOIP2015-D2T3运输计划
  13. 分享自己用的php分页类实例源码
  14. 如何给webbrowser指定IE版本
  15. Ubuntu16.04+CUDA8.0+cuDNN5.1+Python2.7+TensorFlow1.2.0环境搭建
  16. redis源码学习-skiplist
  17. Python笔记(四):异常处理机制与 open()
  18. win32之全屏窗口
  19. kuangbin专题七 HDU1698 Just a Hook (区间设值 线段树)
  20. webstorm中.vue报错(es6语法报错)-转

热门文章

  1. 2018.12.17 struts.xml 配置自定义拦截器配置
  2. 两个list相加
  3. Entity Framework 一
  4. kinect v2
  5. Spring-boot官方案例分析之data-jpa
  6. NEC 框架规范 template media
  7. aes 加密,解密
  8. Java入门(一)
  9. multimap的使用
  10. Ajax之404,200等查询