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

头文件

#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;
}

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

Qt 个性化标题栏,自定义标题栏的更多相关文章

  1. Qt自定义标题栏

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt自定义标题栏     本文地址:http://techieliang.com/2017/1 ...

  2. Qt之界面(自定义标题栏、无边框、可移动、缩放)

    效果 自定义标题栏 titleBar.h #ifndef TITLEBAR_H #define TITLEBAR_H #include <QLabel> #include <QPus ...

  3. setFeatureInt、android 自定义标题栏

    Android 自带的toolbar 往往不能很好的的满足我们的个性化要求.因此我们经常使用自定的的标题栏.而Android系统本身也允许我们自定以标题栏. 记录一下,自定义标题栏常遇到的问题.先上效 ...

  4. Qt::QWidget 无默认标题栏边框的拖拽修改大小方式

    开发环境:win10+vs2015+qt5.9.1 背景:开发过程中,一般很少会使用系统提供的标题栏和边框:往往都是自定义一个自己设计的方案.这时候在QWidget中需要加上flag:Qt::Fram ...

  5. UWP中实现自定义标题栏

    UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...

  6. WPF 自定义标题栏 自定义菜单栏

    自定义标题栏 自定义列表,可以直接修改WPF中的ListBox模板,也用这样类似的效果.但是ListBox是不能设置默认选中状态的. 而我们需要一些复杂的UI效果,还是直接自定义控件来的快 GitHu ...

  7. 【Win10开发】自定义标题栏

    UWP 现在已经可以自定义标题栏了,毕竟看灰色时间长了也会厌烦,开发者们还是希望能够将自己的UI做的更加漂亮,更加与众不同.那么废话不多说,我们开始吧! 首先要了解ApplicationViewTit ...

  8. Android开发-取消程序标题栏或自定义标题栏

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在Android开发中,跟据需要我们有时候需要自定义应用程序的标题栏或者取消程序的标题栏,下面本菜鸟在此记录与分享一下自己使用的方法 ...

  9. Android应用开发基础篇(14)-----自定义标题栏

    一.概述 每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色.大小等都是固定的,给人的感觉比较单调.但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽 ...

  10. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

随机推荐

  1. XCode插件因为升级不能用了怎么办?几个步骤教你搞定

    之前XCode安装了自动注释的插件 VVDomenter.升级之后不能使用了怎么办?跟着我做吧. 1.打开xcode插件所在的目录:~/library/Application Support/Deve ...

  2. P1800 software_NOI导刊2010提高(06)

    P1800 software_NOI导刊2010提高(06) 题目描述 一个软件开发公司同时要开发两个软件,并且要同时交付给用户,现在公司为了尽快完成这一任务,将每个软件划分成m个模块,由公司里的技术 ...

  3. EJB3 调用的存储过程

    要调用存储过程,我们可以通过 EntityManager 对象的 createNativeQuery()方法执行 SQL 语句 (注意:这里说的是SQL 语句,不是 EJB3 QL), 调用存储过程的 ...

  4. Linux中软件使用笔记

    刚刚接触Linux的小白,难免会碰到各种小问题,不要灰心,总有办法的... 1.搜狗输入法崩溃,打不出中文?都是乱码?一招制敌! 在Terminal中输入下面命令后重启电脑即可重生- 还有,是Sogo ...

  5. Excel 批量重命名照片

    理历史照片的时候发现,用文件夹进行分类之后,还有很多照片,如果继续分类,就会导致每个文件夹照片过少,查看不便,但是如果不分类,手机原始的命名方式没有办法满足查看需求,故而,产生了对照片进行批量重命名的 ...

  6. js事件委托代码优化【感悟总结】

    前两天接手了同事的一个项目,是一个网站首页,其中有段代码很累赘,要实现的功能就是, 通过给父元素添加鼠标移入移出事件,来控制子元素显示隐藏. html代码,一共有四个父元素div,每个父元素嵌套一个子 ...

  7. Lucene的原理和应用

    随着互联网的迅速普及与发展,网络舆论对社会生活的影响力越来越大, 网络口碑研究也逐渐形成一个新兴行业.有效的网络口碑研究,需要全方位地倾听网民的声音. 信息检索技术的应用,有效地提高了网络口碑研究的工 ...

  8. C++定义一个简单的Computer类

    /*定义一个简单的Computer类 有数据成员芯片(cpu).内存(ram).光驱(cdrom)等等, 有两个公有成员函数run.stop.cpu为CPU类的一个对象, ram为RAM类的一个对象, ...

  9. 1486: [HNOI2009]最小圈

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 3129  Solved: 1543[Submit][Status][Discuss] Descripti ...

  10. Keepalived 配置高可用

    VRRP协议及Keepalived原理使用   VRRP 协议即 Virtual Router Redundancy Protocol,虚拟路由器冗余协议, 为了解决局域网内默认网关单点失效的问题.  ...