博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
qt 自定义标题栏
阅读量:4165 次
发布时间:2019-05-26

本文共 6810 字,大约阅读时间需要 22 分钟。

TitleBar.h

#ifndef TITLE_BAR#define TITLE_BAR#include 
class QLabel;class QPushButton;class TitleBar : public QWidget{ Q_OBJECTpublic: explicit TitleBar(QWidget *parent = 0); ~TitleBar();protected: // 双击标题栏进行界面的最大化/还原 virtual void mouseDoubleClickEvent(QMouseEvent *event); // 进行鼠界面的拖动 virtual void mousePressEvent(QMouseEvent *event); // 设置界面标题与图标 virtual bool eventFilter(QObject *obj, 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 // TITLE_BAR

TitleBar.cpp

#include 
#include
#include
#include
#include
#include
#include "TitleBar.h"#ifdef Q_OS_WIN#pragma comment(lib, "user32.lib")#include
#endifTitleBar::TitleBar(QWidget *parent) : QWidget(parent){ setFixedHeight(30); 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_pMinimizeButton->setFixedSize(27, 22); m_pMaximizeButton->setFixedSize(27, 22); m_pCloseButton->setFixedSize(27, 22); m_pTitleLabel->setObjectName("whiteLabel"); m_pMinimizeButton->setObjectName("minimizeButton"); m_pMaximizeButton->setObjectName("maximizeButton"); m_pCloseButton->setObjectName("closeButton"); m_pMinimizeButton->setToolTip("Minimize"); m_pMaximizeButton->setToolTip("Maximize"); 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); pLayout->setContentsMargins(5, 0, 5, 0); 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->clicked();}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 *obj, QEvent *event){ switch (event->type()) { case QEvent::WindowTitleChange: { QWidget *pWidget = qobject_cast
(obj); if (pWidget) { m_pTitleLabel->setText(pWidget->windowTitle()); return true; } } case QEvent::WindowIconChange: { QWidget *pWidget = qobject_cast
(obj); 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(obj, event);}void TitleBar::onClicked(){ QPushButton *pButton = qobject_cast
(sender()); QWidget *pWindow = this->window(); if (pWindow->isTopLevel()) { if (pButton == m_pMinimizeButton) { pWindow->showMinimized(); } else if (pButton == m_pMaximizeButton) { pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized(); } else if (pButton == m_pCloseButton) { pWindow->close(); } }}void TitleBar::updateMaximize(){ QWidget *pWindow = this->window(); if (pWindow->isTopLevel()) { bool bMaximize = pWindow->isMaximized(); if (bMaximize) { m_pMaximizeButton->setToolTip(tr("Restore")); m_pMaximizeButton->setProperty("maximizeProperty", "restore"); } else { m_pMaximizeButton->setProperty("maximizeProperty", "maximize"); m_pMaximizeButton->setToolTip(tr("Maximize")); } m_pMaximizeButton->setStyle(QApplication::style()); }}

widget.h

#ifndef __WIDGET__#define __WIDGET__#include "TitleBar.h"#include 
#include
#include
class Widget :public QWidget{public: Widget(QWidget* parent = 0); ~Widget();private: TitleBar *m_pTitleBar; QVBoxLayout *m_pLayout;};#endif

widget.cpp

#include "Widget.h"#include 
#include
#include
Widget::Widget(QWidget* parent) :QWidget(parent), m_pTitleBar(NULL), m_pLayout(NULL){ setWindowFlags(Qt::FramelessWindowHint); m_pTitleBar = new TitleBar(this); installEventFilter(m_pTitleBar); resize(400, 300); setWindowTitle("Custom Window"); QDir::setCurrent(QCoreApplication::applicationDirPath()); QString strLanPath = QObject::tr("%1\\skin\\public\\title.png").arg(QDir::currentPath()); strLanPath = QDir::toNativeSeparators(strLanPath); setWindowIcon(QIcon(strLanPath)); QPalette pal(palette()); pal.setColor(QPalette::Background, QColor(50, 50, 50)); setAutoFillBackground(true); setPalette(pal); m_pLayout = new QVBoxLayout(); m_pLayout->addWidget(m_pTitleBar); m_pLayout->addStretch(); m_pLayout->setSpacing(0); m_pLayout->setContentsMargins(0, 0, 0, 0); setLayout(m_pLayout);}Widget::~Widget(){ delete(m_pLayout); delete(m_pTitleBar);}
style.qss
QPushButton#closeButton {	background:transparent url(skin/public/close.png) no-repeat center center;	border:none;	width:20px;	height:20px;}QPushButton#closeButton:hover {	background:transparent url(skin/public/close_hover.png) no-repeat center center;}QPushButton#closeButton:pressed {	background:transparent url(skin/public/close_pressed.png) no-repeat center center;}QPushButton#closeButton:disabled {	background:transparent url(skin/public/close_disabled.png) no-repeat center center;}QPushButton#maximizeButton {	background:transparent url(skin/public/max.png) no-repeat center center;	border:none;	width:20px;	height:20px;}QPushButton#maximizeButton:hover {	background:transparent url(skin/public/max_hover.png) no-repeat center center;}QPushButton#maximizeButton:pressed {	background:transparent url(skin/public/max_pressed.png) no-repeat center center;}QPushButton#maximizeButton:disabled {	background:transparent url(skin/public/max_disabled.png) no-repeat center center;}QPushButton#minimizeButton {	background:transparent url(skin/public/min.png) no-repeat center center;	border:none;	width:20px;	height:20px;}QPushButton#minimizeButton:hover {	background:transparent url(skin/public/min_hover.png) no-repeat center center;}QPushButton#minimizeButton:pressed {	background:transparent url(skin/public/min_pressed.png) no-repeat center center;}QPushButton#minimizeButton:disabled {	background:transparent url(skin/public/min_disabled.png) no-repeat center center;}

转载地址:http://ovqxi.baihongyu.com/

你可能感兴趣的文章
Activity类中7个与活动生命周期回调有关的方法
查看>>
jwt与token+redis,哪种方案更好用?
查看>>
Comparator接口
查看>>
在二叉树中找到一个节点的后继节点
查看>>
寻找第K大
查看>>
String.trim
查看>>
缓存行 伪共享
查看>>
400 : perceived to be a client error 错误
查看>>
Establishing SSL connection without server's identity verification is not recommended
查看>>
扫描包不存在:pojo类找不到
查看>>
c语言中计算数组长度的方法
查看>>
java 数组定义
查看>>
java中的&和&&的区别
查看>>
Java的位运算符
查看>>
BufferedReader与Scanner的区别
查看>>
java String于常量池中的介绍
查看>>
java Text 错误: 找不到或无法加载主类 Text
查看>>
XShell连接ubantu:给ubantu安装ssh
查看>>
c语言的null和0
查看>>
二进制详解:世界上有10种人,一种懂二进制,一种不懂。
查看>>