#include "mytitlebar.h"
#include <QHBoxLayout>
#include <QPainter>
#include <QFile>
#include <QMouseEvent>
#include <QDebug>
#define BUTTON_HEIGHT 30 // 按钮高度;
#define BUTTON_WIDTH 30 // 按钮宽度;
#define TITLE_HEIGHT 30 // 标题栏高度;
MyTitleBar::MyTitleBar(QWidget *parent)
: QWidget(parent)
, m_colorR(153)
, m_colorG(153)
, m_colorB(153)
, m_isPressed(false)
, m_buttonType(MIN_MAX_BUTTON)
, m_windowBorderWidth(0)
, m_isTransparent(false)
, m_isMoveParentWindow(true)
{
// 初始化;
initControl();
initConnections();
loadStyleSheet("MyTitle");
}
MyTitleBar::~MyTitleBar()
{
}
// 初始化控件;
void MyTitleBar::initControl()
{
m_pIcon = new QLabel;
m_pTitleContent = new QLabel;
m_pButtonMin = new QPushButton;
m_pButtonRestore = new QPushButton;
m_pButtonMax = new QPushButton;
m_pButtonClose = new QPushButton;
m_pButtonMin->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pButtonRestore->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pButtonMax->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pButtonClose->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pTitleContent->setObjectName("TitleContent");
m_pButtonMin->setObjectName("ButtonMin");
m_pButtonRestore->setObjectName("ButtonRestore");
m_pButtonMax->setObjectName("ButtonMax");
m_pButtonClose->setObjectName("ButtonClose");
QHBoxLayout* mylayout = new QHBoxLayout(this);
mylayout->addWidget(m_pIcon);
mylayout->addWidget(m_pTitleContent);
mylayout->addWidget(m_pButtonMin);
mylayout->addWidget(m_pButtonRestore);
mylayout->addWidget(m_pButtonMax);
mylayout->addWidget(m_pButtonClose);
mylayout->setContentsMargins(5, 0, 0, 0);
mylayout->setSpacing(0);
m_pTitleContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
this->setFixedHeight(TITLE_HEIGHT);
this->setWindowFlags(Qt::FramelessWindowHint);
}
// 信号槽的绑定;
void MyTitleBar::initConnections()
{
connect(m_pButtonMin, SIGNAL(clicked()), this, SLOT(onButtonMinClicked()));
connect(m_pButtonRestore, SIGNAL(clicked()), this, SLOT(onButtonRestoreClicked()));
connect(m_pButtonMax, SIGNAL(clicked()), this, SLOT(onButtonMaxClicked()));
connect(m_pButtonClose, SIGNAL(clicked()), this, SLOT(onButtonCloseClicked()));
}
// 设置标题栏背景色,在paintEvent事件中进行绘制标题栏背景色;
//在构造函数中给了默认值,可以外部设置颜色值改变标题栏背景色;
void MyTitleBar::setBackgroundColor(int r, int g, int b, bool isTransparent)
{
m_colorR = r;
m_colorG = g;
m_colorB = b;
m_isTransparent = isTransparent;
// 重新绘制(调用paintEvent事件);
update();
}
// 设置标题栏图标;
void MyTitleBar::setTitleIcon(QString filePath)
{
QPixmap titleIcon(filePath);
m_pIcon->setPixmap(titleIcon.scaled(25 , 25));
}
// 设置标题内容;
void MyTitleBar::setTitleContent(QString titleContent, int titleFontSize)
{
// 设置标题字体大小;
QFont font = m_pTitleContent->font();
font.setPointSize(titleFontSize);
m_pTitleContent->setFont(font);
// 设置标题内容;
m_pTitleContent->setText(titleContent);
m_titleContent = titleContent;
}
// 设置标题栏长度;
void MyTitleBar::setTitleWidth(int width)
{
this->setFixedWidth(width);
}
// 设置标题栏上按钮类型;
// 由于不同窗口标题栏上的按钮都不一样,所以可以自定义标题栏中的按钮;
// 这里提供了四个按钮,分别为最小化、还原、最大化、关闭按钮,如果需要其他按钮可自行添加设置;
void MyTitleBar::setButtonType(ButtonType buttonType)
{
m_buttonType = buttonType;
switch (buttonType)
{
case MIN_BUTTON:
{
m_pButtonRestore->setVisible(false);
m_pButtonMax->setVisible(false);
}
break;
case MIN_MAX_BUTTON:
{
m_pButtonRestore->setVisible(false);
}
break;
case ONLY_CLOSE_BUTTON:
{
m_pButtonMin->setVisible(false);
m_pButtonRestore->setVisible(false);
m_pButtonMax->setVisible(false);
}
break;
default:
break;
}
}
// 设置标题栏中的标题是否会自动滚动,跑马灯的效果;
// 一般情况下标题栏中的标题内容是不滚动的,但是既然自定义就看自己需要嘛,想怎么设计就怎么搞O(∩_∩)O!
void MyTitleBar::setTitleRoll()
{
connect(&m_titleRollTimer, SIGNAL(timeout()), this, SLOT(onRollTitle()));
m_titleRollTimer.start(200);
}
void MyTitleBar::setWindowBorderWidth(int borderWidth)
{
m_windowBorderWidth = borderWidth;
}
// 保存窗口最大化前窗口的位置以及大小;
void MyTitleBar::saveRestoreInfo(const QPoint point, const QSize size)
{
m_restorePos = point;
m_restoreSize = size;
}
// 获取窗口最大化前窗口的位置以及大小;
void MyTitleBar::getRestoreInfo(QPoint& point, QSize& size)
{
point = m_restorePos;
size = m_restoreSize;
}
// 设置是否通过标题栏移动窗口;
void MyTitleBar::setMoveParentWindowFlag(bool isMoveParentWindow)
{
m_isMoveParentWindow = isMoveParentWindow;
}
// 绘制标题栏背景色;
void MyTitleBar::paintEvent(QPaintEvent *event)
{
if (!m_isTransparent)
{
//设置背景色;
QPainter painter(this);
QPainterPath pathBack;
pathBack.setFillRule(Qt::WindingFill);
pathBack.addRoundedRect(QRect(0, 0, this->width(), this->height()), 3, 3);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(pathBack, QBrush(QColor(m_colorR, m_colorG, m_colorB)));
}
// 当窗口最大化或者还原后,窗口长度变了,标题栏的长度应当一起改变;
if (this->width() != (this->parentWidget()->width() - m_windowBorderWidth))
{
this->setFixedWidth(this->parentWidget()->width() - m_windowBorderWidth);
}
QWidget::paintEvent(event);
}
// 双击响应事件,主要是实现双击标题栏进行最大化和最小化操作;
void MyTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
// 只有存在最大化、还原按钮时双击才有效;
if (m_buttonType == MIN_MAX_BUTTON)
{
// 通过最大化按钮的状态判断当前窗口是处于最大化还是原始大小状态;
// 或者通过单独设置变量来表示当前窗口状态;
if (m_pButtonMax->isVisible())
{
onButtonMaxClicked();
}
else
{
onButtonRestoreClicked();
}
}
return QWidget::mouseDoubleClickEvent(event);
}
// 以下通过mousePressEvent、mouseMoveEvent、mouseReleaseEvent三个事件实现了鼠标拖动标题栏移动窗口的效果;
void MyTitleBar::mousePressEvent(QMouseEvent *event)
{
if (m_buttonType == MIN_MAX_BUTTON)
{
// 在窗口最大化时禁止拖动窗口;
if (m_pButtonMax->isVisible())
{
m_isPressed = true;
m_startMovePos = event->globalPos();
}
}
else
{
m_isPressed = true;
m_startMovePos = event->globalPos();
qDebug() << "MyTitleBar::mousePressEvent" << m_startMovePos.x() << m_startMovePos.y();
}
return QWidget::mousePressEvent(event);
}
void MyTitleBar::mouseMoveEvent(QMouseEvent *event)
{
if (m_isPressed && m_isMoveParentWindow)
{
QPoint movePoint = event->globalPos() - m_startMovePos;
QPoint widgetPos = this->parentWidget()->pos() + movePoint;
m_startMovePos = event->globalPos();
qDebug() << "MyTitleBar::mouseMoveEvent" << widgetPos.x() << widgetPos.y();
// this->parentWidget()->move(widgetPos.x(), widgetPos.y());
}
return QWidget::mouseMoveEvent(event);
}
void MyTitleBar::mouseReleaseEvent(QMouseEvent *event)
{
m_isPressed = false;
return QWidget::mouseReleaseEvent(event);
}
// 加载本地样式文件;
// 可以将样式直接写在文件中,程序运行时直接加载进来;
void MyTitleBar::loadStyleSheet(const QString &sheetName)
{
QFile file(":/Resources/" + sheetName + ".css");
file.open(QFile::ReadOnly);
if (file.isOpen())
{
QString styleSheet = this->styleSheet();
styleSheet += QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);
}
}
// 以下为按钮操作响应的槽;
void MyTitleBar::onButtonMinClicked()
{
emit signalButtonMinClicked();
}
void MyTitleBar::onButtonRestoreClicked()
{
m_pButtonRestore->setVisible(false);
m_pButtonMax->setVisible(true);
emit signalButtonRestoreClicked();
}
void MyTitleBar::onButtonMaxClicked()
{
m_pButtonMax->setVisible(false);
m_pButtonRestore->setVisible(true);
emit signalButtonMaxClicked();
}
void MyTitleBar::on
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
模仿QQ登录窗口(Qt实例).zip (86个子文件)
WindowRotation
WindowRotation.sln 962B
WindowRotation
mytitlebar.h 3KB
WindowRotation.cpp 126B
loginnetsetwindow.h 747B
Resources
MyTitle
restore.png 1KB
close.png 3KB
min1.png 1KB
close1.png 3KB
max.png 1KB
arrowback.png 3KB
close11.png 4KB
min.png 3KB
NetSetWindow
headBack.gif 301KB
MyTitle.css 1KB
LoginWindow
deleteAccount.png 3KB
QQ登录界面图片资源
state_notdisturb.png 3KB
closeback.png 3KB
checkbox.png 317B
state_away.png 3KB
HeadImage.png 9KB
back.gif 1.16MB
keyboard.png 3KB
more_accountlogin.png 643B
state_Qme.png 3KB
minback.png 3KB
arrowback.png 3KB
state_online.png 3KB
state_hide.png 3KB
state_busy.png 3KB
flicker.png 516B
drop_down_Button.png 296B
HeadImage - 副本 (2).png 9KB
keyboard_hover.png 3KB
keyboard_press.png 3KB
menu_border.png 594B
checkbox.png 317B
新建文件夹 (2)
arrow_press.png 3KB
min_press.bmp 3KB
close_press.png 3KB
arrow_hover.png 3KB
close.png 3KB
min_.png 3KB
close_hover.png 3KB
min_hover.bmp 3KB
arrow.png 3KB
HeadImage.png 9KB
back.gif 1.16MB
keyboard.png 3KB
more_accountlogin.png 643B
LoginWindow.css 5KB
LoginState
state_notdisturb.png 3KB
state_away.png 3KB
state_Qme.png 3KB
state_online.png 3KB
state_hide.png 3KB
state_busy.png 3KB
QQ登录界面图片资源.rar 1.2MB
arrowback.png 3KB
headImage
head_0.png 77KB
head_2.png 78KB
head_1.png 9KB
keyboard - 副本.png 3KB
HeadImage - 副本.png 6KB
flicker.png 516B
drop_down_Button.png 296B
WindowRotation.vcxproj.filters 6KB
rotatewidget.cpp 4KB
loginwindow.cpp 7KB
WindowRotation.vcxproj 25KB
mytitlebar.cpp 8KB
WindowRotation.qrc 2KB
loginnetsetwindow.cpp 3KB
WindowRotation.h 237B
loginnetsetwindow.ui 16KB
GeneratedFiles
x64
Debug
main.cpp 212B
basewindow.h 590B
loginwindow.ui 8KB
accountitem.h 956B
accountitem.cpp 2KB
WindowRotation.ui 624B
rotatewidget.h 714B
basewindow.cpp 4KB
loginwindow.h 2KB
WindowRotation.vcxproj.user 629B
.vs
WindowRotation
v14
.suo 727KB
x64
Debug
WindowRotation.exe 2.26MB
共 86 条
- 1
前行中的小猪
- 粉丝: 3w+
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页