QT自定义无边框窗口(可移动控制和窗口大小调整)

         QT是一个功能强大的跨平台开发框架,它提供了丰富的界面设计工具和组件。在界面开发中,QT窗口自带的标题栏无法满足我们的需求。我们就需要自定义无边框窗口,包括自定义标题栏和窗口大小调整功能。本文将介绍如何在QT中实现这些功能。

一、简述

         本文介绍了如何使用Qt框架创建一个无边框窗口,并提供了详细的源码,包括窗口样式设置、移动区域控制和窗口大小调整功能。用于设置窗口为无边框窗口。可为窗口添加自定义标题栏、边框和系统菜单按钮。

二、 设计思路             

        首先,在QT中,我们可以通过设置窗口属性为Qt::FramelessWindowHint来实现无边框窗口BaseWindow(由于系统窗口被设置为Qt::FramelessWindowHint会导致窗口不能被拖动,需要通过捕获鼠标移动事件从而实现窗口移动。)。

        然后,我们可以通过一个自定义的QWidget来扮演标题栏的角色BaseTitleBar。在这个自定义的QWidget中,我们可以添加一些控件,比如窗口标题,关闭按钮等。我们通过自定义的标题栏和边框样式来实现无边框窗口的外观。使用titleBar类来定义标题栏的样式,其中包括标题文本和关闭按钮。通过重写QWidget的mousePressEventmouseMoveEventmouseReleaseEvent函数,我们可以实现拖动窗口的功能。

        最后,自定义FramelessHelper一个辅助类,我们可以自定义窗口的行为,如设置窗口的可移动和可缩放属性。

三、效果 

四、核心代码  
1、头文件

BaseWindow.h 

#ifndef BASEWINDOW_H
#define BASEWINDOW_H#include <QWidget>
#include <QMainWindow>
#include "BaseTitleBar.h"class BaseWindow : public QWidget
{Q_OBJECTpublic:BaseWindow(QWidget *parent = 0);~BaseWindow();private:void initTitleBar();void paintEvent(QPaintEvent *event);void loadStyleSheet(const QString &sheetName);private slots:void onButtonMinClicked();void onButtonRestoreClicked();void onButtonMaxClicked();void onButtonCloseClicked();protected:BaseTitleBar* m_titleBar;};#endif // BASEWINDOW_H

 BaseTitleBar.h

#ifndef BASETITLEBAR_H
#define BASETITLEBAR_H#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QTimer>enum ButtonType
{MIN_BUTTON = 0,			// 最小化和关闭按钮;MIN_MAX_BUTTON ,		// 最小化、最大化和关闭按钮;ONLY_CLOSE_BUTTON		// 只有关闭按钮;
};class BaseTitleBar : public QWidget
{Q_OBJECTpublic:BaseTitleBar(QWidget *parent = NULL);~BaseTitleBar();// 设置标题栏背景色;void setBackgroundColor(int r, int g, int b);// 设置标题栏图标;void setTitleIcon(QString filePath);// 设置标题内容;void setTitleContent(QString titleContent);// 设置标题栏长度;void setTitleWidth(int width);// 设置标题栏上按钮类型;void setButtonType(ButtonType buttonType);// 设置标题栏中的标题是否会滚动;具体可以看效果;void setTitleRoll();// 保存/获取 最大化前窗口的位置及大小;void saveRestoreInfo(const QPoint point, const QSize size);void getRestoreInfo(QPoint& point, QSize& size);private:void paintEvent(QPaintEvent *event);void mouseDoubleClickEvent(QMouseEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);// 初始化控件;void initControl();// 信号槽的绑定;void initConnections();// 加载样式文件;void loadStyleSheet(const QString &sheetName);signals:// 按钮触发的信号;void signalButtonMinClicked();void signalButtonRestoreClicked();void signalButtonMaxClicked();void signalButtonCloseClicked();private slots:// 按钮触发的槽;void onButtonMinClicked();void onButtonRestoreClicked();void onButtonMaxClicked();void onButtonCloseClicked();void onRollTitle();private:QLabel* m_pIcon;					// 标题栏图标;QLabel* m_pTitleContent;			// 标题栏内容;QPushButton* m_pButtonMin;			// 最小化按钮;QPushButton* m_pButtonRestore;		// 最大化还原按钮;QPushButton* m_pButtonMax;			// 最大化按钮;QPushButton* m_pButtonClose;		// 关闭按钮;// 标题栏背景色;int m_colorR;int m_colorG;int m_colorB;// 最大化,最小化变量;QPoint m_restorePos;QSize m_restoreSize;// 移动窗口的变量;bool m_isPressed;QPoint m_startMovePos;// 标题栏跑马灯效果时钟;QTimer m_titleRollTimer;// 标题栏内容;QString m_titleContent;// 按钮类型;ButtonType m_buttonType;
};#endif // BASETITLEBAR_H

 framelesshelper.h

#ifndef FRAMELESSHELPER_H
#define FRAMELESSHELPER_H#include <QtGui>
#include <QRubberBand>
#include <QStylePainter>
#include <QStyleOptionFocusRect>class WidgetData;
/****** FramelessHelperPrivate* 存储界面对应的数据集合,以及是否可移动、可缩放属性
*****/
class FramelessHelperPrivate
{
public:QHash<QWidget*, WidgetData*> m_widgetDataHash;bool m_bWidgetMovable        : true;bool m_bWidgetResizable      : true;bool m_bRubberBandOnResize   : true;bool m_bRubberBandOnMove     : true;
};class FramelessHelper : public QObject
{Q_OBJECTpublic:explicit FramelessHelper(QObject *parent = 0);~FramelessHelper();// 激活窗体void activateOn(QWidget *topLevelWidget);// 移除窗体void removeFrom(QWidget *topLevelWidget);// 设置窗体移动void setWidgetMovable(bool movable);// 设置窗体缩放void setWidgetResizable(bool resizable);// 设置橡皮筋移动void setRubberBandOnMove(bool movable);// 设置橡皮筋缩放void setRubberBandOnResize(bool resizable);// 设置边框的宽度void setBorderWidth(uint width);// 设置标题栏高度void setTitleHeight(uint height);bool widgetResizable();bool widgetMovable();bool rubberBandOnMove();bool rubberBandOnResisze();uint borderWidth();uint titleHeight();protected:// 事件过滤,进行移动、缩放等virtual bool eventFilter(QObject *obj, QEvent *event);private:FramelessHelperPrivate *d;
};class LinuxRubberBand : public QRubberBand
{
public:LinuxRubberBand(Shape s, QWidget * p = 0 ): QRubberBand( s, p ){QPalette palette;palette.setBrush( QPalette::WindowText, QBrush(Qt::lightGray) );setPalette(palette);repaint();}protected:virtual void paintEvent( QPaintEvent * ){QStylePainter painter(this);QStyleOptionFocusRect option;option.initFrom(this);QPen pen;pen.setStyle(Qt::DashLine);pen.setWidth(1);pen.setColor(QColor(Qt::red));painter.setPen(pen);painter.drawControl(QStyle::CE_FocusFrame, option);}};/****** CursorPosCalculator* 计算鼠标是否位于左、上、右、下、左上角、左下角、右上角、右下角
*****/
class CursorPosCalculator
{
public:explicit CursorPosCalculator();void reset();void recalculate(const QPoint &globalMousePos, const QRect &frameRect);public:bool m_bOnEdges              : true;bool m_bOnLeftEdge           : true;bool m_bOnRightEdge          : true;bool m_bOnTopEdge            : true;bool m_bOnBottomEdge         : true;bool m_bOnTopLeftEdge        : true;bool m_bOnBottomLeftEdge     : true;bool m_bOnTopRightEdge       : true;bool m_bOnBottomRightEdge    : true;static int m_nBorderWidth;static int m_nTitleHeight;
};/****** WidgetData* 更新鼠标样式、移动窗体、缩放窗体
*****/
class WidgetData
{
public:explicit WidgetData(FramelessHelperPrivate *d, QWidget *pTopLevelWidget);~WidgetData();QWidget* widget();// 处理鼠标事件-划过、厉害、按下、释放、移动void handleWidgetEvent(QEvent *event);// 更新橡皮筋状态void updateRubberBandStatus();private:// 更新鼠标样式void updateCursorShape(const QPoint &gMousePos);// 重置窗体大小void resizeWidget(const QPoint &gMousePos);// 移动窗体void moveWidget(const QPoint &gMousePos);// 处理鼠标按下void handleMousePressEvent(QMouseEvent *event);// 处理鼠标释放void handleMouseReleaseEvent(QMouseEvent *event);// 处理鼠标移动void handleMouseMoveEvent(QMouseEvent *event);// 处理鼠标离开void handleLeaveEvent(QEvent *event);// 处理鼠标进入void handleHoverMoveEvent(QHoverEvent *event);private:FramelessHelperPrivate *d;LinuxRubberBand *m_pRubberBand;QWidget *m_pWidget;QPoint m_ptDragPos;CursorPosCalculator m_pressedMousePos;CursorPosCalculator m_moveMousePos;bool m_bLeftButtonPressed;bool m_bCursorShapeChanged;bool m_bLeftButtonTitlePressed;Qt::WindowFlags m_windowFlags;
};#endif // FRAMELESSHELPER_H
2、实现代码

BaseWindow.cpp 

#include "BaseWindow.h"
#include <QDesktopWidget>
#include <QApplication>
#include <QPainter>
#include <QFile>BaseWindow::BaseWindow(QWidget *parent): QWidget(parent)
{// FramelessWindowHint属性设置窗口去除边框;// WindowMinimizeButtonHint 属性设置在窗口最小化时,点击任务栏窗口可以显示出原窗口;this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);// 设置窗口背景透明;setAttribute(Qt::WA_TranslucentBackground);// 初始化标题栏;initTitleBar();
}BaseWindow::~BaseWindow()
{}void BaseWindow::initTitleBar()
{m_titleBar = new BaseTitleBar(this);m_titleBar->move(0, 0);connect(m_titleBar, SIGNAL(signalButtonMinClicked()), this, SLOT(onButtonMinClicked()));connect(m_titleBar, SIGNAL(signalButtonRestoreClicked()), this, SLOT(onButtonRestoreClicked()));connect(m_titleBar, SIGNAL(signalButtonMaxClicked()), this, SLOT(onButtonMaxClicked()));connect(m_titleBar, SIGNAL(signalButtonCloseClicked()), this, SLOT(onButtonCloseClicked()));}void BaseWindow::paintEvent(QPaintEvent* event)
{//设置背景色;QPainter painter(this);QPainterPath pathBack;pathBack.setFillRule(Qt::WindingFill);pathBack.addRoundedRect(QRect(0, 0, this->width(), this->height()), 3, 3);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);painter.fillPath(pathBack, QBrush(QColor(127, 127, 127)));return QWidget::paintEvent(event);
}void BaseWindow::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 BaseWindow::onButtonMinClicked()
{if (Qt::Tool == (windowFlags() & Qt::Tool)){hide();    //设置了Qt::Tool 如果调用showMinimized()则窗口就销毁了???}else{showMinimized();}
}void BaseWindow::onButtonRestoreClicked()
{QPoint windowPos;QSize windowSize;m_titleBar->getRestoreInfo(windowPos, windowSize);this->setGeometry(QRect(windowPos, windowSize));
}void BaseWindow::onButtonMaxClicked()
{m_titleBar->saveRestoreInfo(this->pos(), QSize(this->width(), this->height()));QRect desktopRect = QApplication::desktop()->availableGeometry();QRect FactRect = QRect(desktopRect.x() - 3, desktopRect.y() - 3, desktopRect.width() + 6, desktopRect.height() + 6);setGeometry(FactRect);
}void BaseWindow::onButtonCloseClicked()
{close();
}

BaseTitleBar.cpp

#include "BaseTitleBar.h"
#include <QHBoxLayout>
#include <QPainter>
#include <QFile>
#include <QMouseEvent>#define BUTTON_HEIGHT 30		// 按钮高度;
#define BUTTON_WIDTH 30			// 按钮宽度;
#define TITLE_HEIGHT 30			// 标题栏高度;BaseTitleBar::BaseTitleBar(QWidget *parent): QWidget(parent), m_colorR(153), m_colorG(153), m_colorB(153), m_isPressed(false), m_buttonType(MIN_MAX_BUTTON)
{// 初始化;initControl();initConnections();loadStyleSheet("MyTitle");
}BaseTitleBar::~BaseTitleBar()
{}// 初始化控件;
void BaseTitleBar::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);m_pTitleContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);this->setFixedHeight(TITLE_HEIGHT);this->setWindowFlags(Qt::FramelessWindowHint);
}// 信号槽的绑定;
void BaseTitleBar::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 BaseTitleBar::setBackgroundColor(int r, int g, int b)
{m_colorR = r;m_colorG = g;m_colorB = b;// 重新绘制(调用paintEvent事件);update();
}// 设置标题栏图标;
void BaseTitleBar::setTitleIcon(QString filePath)
{QPixmap titleIcon(filePath);m_pIcon->setPixmap(titleIcon.scaled(25 , 25));
}// 设置标题内容;
void BaseTitleBar::setTitleContent(QString titleContent)
{m_pTitleContent->setText(titleContent);m_titleContent = titleContent;
}// 设置标题栏长度;
void BaseTitleBar::setTitleWidth(int width)
{this->setFixedWidth(width);
}// 设置标题栏上按钮类型;
// 由于不同窗口标题栏上的按钮都不一样,所以可以自定义标题栏中的按钮;
// 这里提供了四个按钮,分别为最小化、还原、最大化、关闭按钮,如果需要其他按钮可自行添加设置;
void BaseTitleBar::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 BaseTitleBar::setTitleRoll()
{connect(&m_titleRollTimer, SIGNAL(timeout()), this, SLOT(onRollTitle()));m_titleRollTimer.start(200);
}// 保存窗口最大化前窗口的位置以及大小;
void BaseTitleBar::saveRestoreInfo(const QPoint point, const QSize size)
{m_restorePos = point;m_restoreSize = size;
}// 获取窗口最大化前窗口的位置以及大小;
void BaseTitleBar::getRestoreInfo(QPoint& point, QSize& size)
{point = m_restorePos;size = m_restoreSize;
}// 绘制标题栏背景色;
void BaseTitleBar::paintEvent(QPaintEvent *event)
{//设置背景色;QPainter painter(this);QPainterPath pathBack;pathBack.setFillRule(Qt::WindingFill);pathBack.addRoundedRect(QRect(0, 0, this->width(), this->height()), 3, 3);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);painter.fillPath(pathBack, QBrush(QColor(m_colorR, m_colorG, m_colorB)));// 当窗口最大化或者还原后,窗口长度变了,标题栏的长度应当一起改变;if (this->width() != this->parentWidget()->width()){this->setFixedWidth(this->parentWidget()->width());}QWidget::paintEvent(event);
}// 双击响应事件,主要是实现双击标题栏进行最大化和最小化操作;
void BaseTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{// 只有存在最大化、还原按钮时双击才有效;if (m_buttonType == MIN_MAX_BUTTON){// 通过最大化按钮的状态判断当前窗口是处于最大化还是原始大小状态;// 或者通过单独设置变量来表示当前窗口状态;if (m_pButtonMax->isVisible()){onButtonMaxClicked();}else{onButtonRestoreClicked();}}	return QWidget::mouseDoubleClickEvent(event);
}// 以下通过mousePressEvent、mouseMoveEvent、mouseReleaseEvent三个事件实现了鼠标拖动标题栏移动窗口的效果;
void BaseTitleBar::mousePressEvent(QMouseEvent *event)
{return QWidget::mousePressEvent(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();}//	return QWidget::mousePressEvent(event);
}void BaseTitleBar::mouseMoveEvent(QMouseEvent *event)
{if (m_isPressed){QPoint movePoint = event->globalPos() - m_startMovePos;QPoint widgetPos = this->parentWidget()->pos();m_startMovePos = event->globalPos();this->parentWidget()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y());}return QWidget::mouseMoveEvent(event);
}void BaseTitleBar::mouseReleaseEvent(QMouseEvent *event)
{m_isPressed = false;return QWidget::mouseReleaseEvent(event);
}// 加载本地样式文件;
// 可以将样式直接写在文件中,程序运行时直接加载进来;
void BaseTitleBar::loadStyleSheet(const QString &sheetName)
{QFile file(":/TitleBarRc/" + sheetName + ".css");file.open(QFile::ReadOnly);if (file.isOpen()){QString styleSheet = this->styleSheet();styleSheet += QLatin1String(file.readAll());this->setStyleSheet(styleSheet);}
}// 以下为按钮操作响应的槽;
void BaseTitleBar::onButtonMinClicked()
{emit signalButtonMinClicked();
}void BaseTitleBar::onButtonRestoreClicked()
{m_pButtonRestore->setVisible(false);m_pButtonMax->setVisible(true);emit signalButtonRestoreClicked();
}void BaseTitleBar::onButtonMaxClicked()
{m_pButtonMax->setVisible(false);m_pButtonRestore->setVisible(true);emit signalButtonMaxClicked();
}void BaseTitleBar::onButtonCloseClicked()
{emit signalButtonCloseClicked();
}// 该方法主要是让标题栏中的标题显示为滚动的效果;
void BaseTitleBar::onRollTitle()
{static int nPos = 0;QString titleContent = m_titleContent;// 当截取的位置比字符串长时,从头开始;if (nPos > titleContent.length())nPos = 0;m_pTitleContent->setText(titleContent.mid(nPos));nPos++;
}

 framelesshelper.cpp

#include "framelesshelper.h"int CursorPosCalculator::m_nBorderWidth = 5;
int CursorPosCalculator::m_nTitleHeight = 30;
/***** CursorPosCalculator *****/
CursorPosCalculator::CursorPosCalculator()
{reset();
}void CursorPosCalculator::reset()
{m_bOnEdges = false;m_bOnLeftEdge = false;m_bOnRightEdge = false;m_bOnTopEdge = false;m_bOnBottomEdge = false;m_bOnTopLeftEdge = false;m_bOnBottomLeftEdge = false;m_bOnTopRightEdge  = false;m_bOnBottomRightEdge = false;
}void CursorPosCalculator::recalculate(const QPoint &gMousePos, const QRect &frameRect)
{int globalMouseX = gMousePos.x();int globalMouseY = gMousePos.y();int frameX = frameRect.x();int frameY = frameRect.y();int frameWidth = frameRect.width();int frameHeight = frameRect.height();m_bOnLeftEdge = (globalMouseX >= frameX &&globalMouseX <= frameX + m_nBorderWidth );m_bOnRightEdge = (globalMouseX >= frameX + frameWidth - m_nBorderWidth &&globalMouseX <= frameX + frameWidth);m_bOnTopEdge = (globalMouseY >= frameY &&globalMouseY <= frameY + m_nBorderWidth );m_bOnBottomEdge = (globalMouseY >= frameY + frameHeight - m_nBorderWidth &&globalMouseY <= frameY + frameHeight);m_bOnTopLeftEdge = m_bOnTopEdge && m_bOnLeftEdge;m_bOnBottomLeftEdge = m_bOnBottomEdge && m_bOnLeftEdge;m_bOnTopRightEdge = m_bOnTopEdge && m_bOnRightEdge;m_bOnBottomRightEdge = m_bOnBottomEdge && m_bOnRightEdge;m_bOnEdges = m_bOnLeftEdge || m_bOnRightEdge || m_bOnTopEdge || m_bOnBottomEdge;
}/***** WidgetData *****/
WidgetData::WidgetData(FramelessHelperPrivate *_d, QWidget *pTopLevelWidget)
{d = _d;m_pWidget = pTopLevelWidget;m_bLeftButtonPressed = false;m_bCursorShapeChanged = false;m_bLeftButtonTitlePressed = false;m_pRubberBand = NULL;m_windowFlags = m_pWidget->windowFlags();m_pWidget->setMouseTracking(true);m_pWidget->setAttribute(Qt::WA_Hover, true);updateRubberBandStatus();
}WidgetData::~WidgetData()
{m_pWidget->setMouseTracking(false);m_pWidget->setWindowFlags(m_windowFlags);m_pWidget->setAttribute(Qt::WA_Hover, false);delete m_pRubberBand;m_pRubberBand = NULL;
}QWidget* WidgetData::widget()
{return m_pWidget;
}void WidgetData::handleWidgetEvent(QEvent *event)
{switch (event->type()){default:break;case QEvent::MouseButtonPress:handleMousePressEvent(static_cast<QMouseEvent*>(event));break;case QEvent::MouseButtonRelease:handleMouseReleaseEvent(static_cast<QMouseEvent*>(event));break;case QEvent::MouseMove:handleMouseMoveEvent(static_cast<QMouseEvent*>(event));break;case QEvent::Leave:handleLeaveEvent(static_cast<QMouseEvent*>(event));break;case QEvent::HoverMove:handleHoverMoveEvent(static_cast<QHoverEvent*>(event));break;}
}void WidgetData::updateRubberBandStatus()
{if (d->m_bRubberBandOnMove || d->m_bRubberBandOnResize){if (NULL == m_pRubberBand) {m_pRubberBand = new LinuxRubberBand(QRubberBand::Rectangle);}}else{delete m_pRubberBand;m_pRubberBand = NULL;}
}void WidgetData::updateCursorShape(const QPoint &gMousePos)
{if (m_pWidget->isFullScreen() || m_pWidget->isMaximized()){if (m_bCursorShapeChanged){m_pWidget->unsetCursor();}return;}m_moveMousePos.recalculate(gMousePos, m_pWidget->frameGeometry());if(m_moveMousePos.m_bOnTopLeftEdge || m_moveMousePos.m_bOnBottomRightEdge){m_pWidget->setCursor( Qt::SizeFDiagCursor );m_bCursorShapeChanged = true;}else if(m_moveMousePos.m_bOnTopRightEdge || m_moveMousePos.m_bOnBottomLeftEdge){m_pWidget->setCursor( Qt::SizeBDiagCursor );m_bCursorShapeChanged = true;}else if(m_moveMousePos.m_bOnLeftEdge || m_moveMousePos.m_bOnRightEdge){m_pWidget->setCursor( Qt::SizeHorCursor );m_bCursorShapeChanged = true;}else if(m_moveMousePos.m_bOnTopEdge || m_moveMousePos.m_bOnBottomEdge){m_pWidget->setCursor( Qt::SizeVerCursor );m_bCursorShapeChanged = true;}else{if (m_bCursorShapeChanged){m_pWidget->unsetCursor();m_bCursorShapeChanged = false;}}
}void WidgetData::resizeWidget(const QPoint &gMousePos)
{QRect origRect;if (d->m_bRubberBandOnResize)origRect = m_pRubberBand->frameGeometry();elseorigRect = m_pWidget->frameGeometry();int left = origRect.left();int top = origRect.top();int right = origRect.right();int bottom = origRect.bottom();origRect.getCoords(&left, &top, &right, &bottom);//int minWidth = m_pWidget->minimumWidth();//int minHeight = m_pWidget->minimumHeight();int minWidth = 40;int minHeight = 40;if (m_pressedMousePos.m_bOnTopLeftEdge){left = gMousePos.x();top = gMousePos.y();}else if (m_pressedMousePos.m_bOnBottomLeftEdge){left = gMousePos.x();bottom = gMousePos.y();}else if (m_pressedMousePos.m_bOnTopRightEdge){right = gMousePos.x();top = gMousePos.y();}else if (m_pressedMousePos.m_bOnBottomRightEdge){right = gMousePos.x();bottom = gMousePos.y();}else if (m_pressedMousePos.m_bOnLeftEdge){left = gMousePos.x();}else if (m_pressedMousePos.m_bOnRightEdge){right = gMousePos.x();}else if (m_pressedMousePos.m_bOnTopEdge){top = gMousePos.y();}else if (m_pressedMousePos.m_bOnBottomEdge){bottom = gMousePos.y();}QRect newRect(QPoint(left, top), QPoint(right, bottom));if (newRect.isValid()){if (minWidth > newRect.width()){if (left != origRect.left())newRect.setLeft(origRect.left());elsenewRect.setRight(origRect.right());}if (minHeight > newRect.height()){if (top != origRect.top())newRect.setTop(origRect.top());elsenewRect.setBottom(origRect.bottom());}if (d->m_bRubberBandOnResize){m_pRubberBand->setGeometry(newRect);}else{m_pWidget->setGeometry(newRect);}}
}void WidgetData::moveWidget(const QPoint& gMousePos)
{if (d->m_bRubberBandOnMove){m_pRubberBand->move(gMousePos - m_ptDragPos);}else{m_pWidget->move(gMousePos - m_ptDragPos);}
}void WidgetData::handleMousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton){m_bLeftButtonPressed = true;m_bLeftButtonTitlePressed = event->pos().y() < m_moveMousePos.m_nTitleHeight;QRect frameRect = m_pWidget->frameGeometry();QRect moveRect(frameRect.x(), frameRect.y(), frameRect.width(), 30);m_pressedMousePos.recalculate(event->globalPos(), frameRect);m_ptDragPos = event->globalPos() - frameRect.topLeft();if (m_pressedMousePos.m_bOnEdges){if (d->m_bRubberBandOnResize){m_pRubberBand->setGeometry(frameRect);m_pRubberBand->show();}}else if (d->m_bRubberBandOnMove){if (moveRect.contains(event->globalPos())) {m_pRubberBand->setGeometry(frameRect);m_pRubberBand->show();}}}
}void WidgetData::handleMouseReleaseEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton){m_bLeftButtonPressed = false;m_bLeftButtonTitlePressed = false;m_pressedMousePos.reset();if (m_pRubberBand && m_pRubberBand->isVisible()){m_pRubberBand->hide();m_pWidget->setGeometry(m_pRubberBand->geometry());}}
}void WidgetData::handleMouseMoveEvent(QMouseEvent *event)
{if (m_bLeftButtonPressed){if (d->m_bWidgetResizable && m_pressedMousePos.m_bOnEdges){resizeWidget(event->globalPos());}else if (d->m_bWidgetMovable && m_bLeftButtonTitlePressed){moveWidget(event->globalPos());}}else if (d->m_bWidgetResizable){updateCursorShape(event->globalPos());}
}void WidgetData::handleLeaveEvent(QEvent *event)
{Q_UNUSED(event)if (!m_bLeftButtonPressed){m_pWidget->unsetCursor();}
}void WidgetData::handleHoverMoveEvent(QHoverEvent *event)
{if (d->m_bWidgetResizable){updateCursorShape(m_pWidget->mapToGlobal(event->pos()));}
}/*****FramelessHelper*****/
FramelessHelper::FramelessHelper(QObject *parent): QObject(parent),d(new FramelessHelperPrivate())
{d->m_bWidgetMovable = true;d->m_bWidgetResizable = true;d->m_bRubberBandOnResize = false;d->m_bRubberBandOnMove = false;
}FramelessHelper::~FramelessHelper()
{QList<QWidget*> keys = d->m_widgetDataHash.keys();int size = keys.size();for (int i = 0; i < size; ++i) {delete d->m_widgetDataHash.take(keys[i]);}delete d;
}bool FramelessHelper::eventFilter(QObject *obj, QEvent *event)
{switch (event->type()){case QEvent::MouseMove:case QEvent::HoverMove:case QEvent::MouseButtonPress:case QEvent::MouseButtonRelease:case QEvent::Leave:{WidgetData *data = d->m_widgetDataHash.value(static_cast<QWidget*>(obj));if (data){data->handleWidgetEvent(event);return true;}}}return QObject::eventFilter(obj, event);
}void FramelessHelper::activateOn(QWidget *topLevelWidget)
{if (!d->m_widgetDataHash.contains(topLevelWidget)){WidgetData *data = new WidgetData(d, topLevelWidget);d->m_widgetDataHash.insert(topLevelWidget, data);topLevelWidget->installEventFilter(this);}
}void FramelessHelper::removeFrom(QWidget *topLevelWidget)
{WidgetData *data = d->m_widgetDataHash.take(topLevelWidget);if (data){topLevelWidget->removeEventFilter(this);delete data;}
}void FramelessHelper::setRubberBandOnMove(bool movable)
{d->m_bRubberBandOnMove = movable;QList<WidgetData*> list = d->m_widgetDataHash.values();foreach (WidgetData *data, list){data->updateRubberBandStatus();}
}void FramelessHelper::setWidgetMovable(bool movable)
{d->m_bWidgetMovable = movable;
}void FramelessHelper::setWidgetResizable(bool resizable)
{d->m_bWidgetResizable = resizable;
}void FramelessHelper::setRubberBandOnResize(bool resizable)
{d->m_bRubberBandOnResize = resizable;QList<WidgetData*> list = d->m_widgetDataHash.values();foreach (WidgetData *data, list){data->updateRubberBandStatus();}
}void FramelessHelper::setBorderWidth(uint width)
{if (width > 0){CursorPosCalculator::m_nBorderWidth = width;}
}void FramelessHelper::setTitleHeight(uint height)
{if (height > 0){CursorPosCalculator::m_nTitleHeight = height;}
}bool FramelessHelper::widgetMovable()
{return d->m_bWidgetMovable;
}bool FramelessHelper::widgetResizable()
{return d->m_bWidgetResizable;
}bool FramelessHelper::rubberBandOnMove()
{return d->m_bRubberBandOnMove;
}bool FramelessHelper::rubberBandOnResisze()
{return d->m_bRubberBandOnResize;
}uint FramelessHelper::borderWidth()
{return CursorPosCalculator::m_nBorderWidth;
}uint FramelessHelper::titleHeight()
{return CursorPosCalculator::m_nTitleHeight;
}

        通过以上代码,我们可以实现一个具有自定义标题栏、窗口大小调整和可移动、可缩放属性的无边框窗口。这段代码提供了一种简单而有效的方法来实现自定义外观和交互方式的无边框窗口。可以根据自己的需求进行修改和定制,以满足不同应用程序的需要。

五、使用示例

以下是一个简单的示例代码,演示了如何在Qt中使用此控件:

CustomMainWindow.h
#ifndef CUSTOMMAINWINDOW_H
#define CUSTOMMAINWINDOW_H#include <QWidget>#include "BaseWindow.h"
#include "framelesshelper.h"namespace Ui {
class CustomMainWindow;
}class CustomMainWindow : public BaseWindow
{Q_OBJECTpublic:explicit CustomMainWindow(QWidget *parent = 0);~CustomMainWindow();private:void initTitleBar();void framelesshelperInit();Ui::CustomMainWindow *ui;
};#endif // CUSTOMMAINWINDOW_H
CustomMainWindow.cpp
#include "CustomMainWindow.h"
#include "ui_CustomMainWindow.h"CustomMainWindow::CustomMainWindow(QWidget *parent) :BaseWindow(parent),ui(new Ui::CustomMainWindow)
{initTitleBar();framelesshelperInit();ui->setupUi(this);
}CustomMainWindow::~CustomMainWindow()
{delete ui;
}void CustomMainWindow::initTitleBar()
{m_titleBar->setBackgroundColor(71,76,78);m_titleBar->setTitleIcon(":/TitleBarRc/icon.png");m_titleBar->setTitleContent(QStringLiteral("这是一个可以滚动的标题!"));m_titleBar->setTitleRoll();m_titleBar->setButtonType(MIN_MAX_BUTTON);m_titleBar->setTitleWidth(this->width());
}void CustomMainWindow::framelesshelperInit()
{//this指的是要处理的窗体FramelessHelper *pHelper = new FramelessHelper(this);pHelper->activateOn(this);  //激活当前窗体pHelper->setWidgetMovable(true);  //设置窗体可移动pHelper->setWidgetResizable(true);  //设置窗体可缩放pHelper->setRubberBandOnMove(false);  //设置橡皮筋效果-可移动pHelper->setRubberBandOnResize(true);  //设置橡皮筋效果-可缩放
}

        现在,我们可以编译和运行该项目。当窗口显示出来时,您将会看到一个自定义的无边框窗口,包含自定义的标题栏和窗口大小调整按钮。您可以尝试拖动标题栏或点击窗口大小调整按钮来调整窗口的大小和位置。需要注意的是,使用自定义标题栏时需留够空间,不要被窗口内控件遮挡。

        通过以上步骤,我们成功地实现了在QT中创建自定义无边框窗口的目标,包括自定义标题栏和窗口大小调整功能。这使得我们可以根据自己的需要创建独特和个性化的窗口界面。

        谢谢您的阅读,希望本文能为您带来一些帮助和启发。如果您有任何问题或意见,请随时与我联系。祝您度过美好的一天!

六、源代码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/385177.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux离线安装mysql8(单机版)

文章目录 一、检查服务器是否有残留mysql资源&#xff0c;有的话就全删除1.1、查询mysql已安装的相关依赖&#xff1a;1.2、查找含有MySQL的目录 二、安装2.1、上传mysql安装包到文件夹下并解压2.2、移动及重命名2.3、mysql用户2.4、配置mysql所需的my.cnf文件2.5、给my.cnf配置…

推荐系统三十六式学习笔记:工程篇.常见架构24|典型的信息流架构是什么样的

目录 整体框架数据模型1.内容即Activity2.关系即连接 动态发布信息流排序数据管道总结 从今天起&#xff0c;我们不再单独介绍推荐算法的原理&#xff0c;而是开始进入一个新的模块-工程篇。 在工程实践的部分中&#xff0c;我首先介绍的内容是当今最热门的信息流架构。 信息…

脑网络布线成本优化——从Caja守恒原则到最小化成本的探索

脑网络布线成本优化——从Caja守恒原则到最小化成本的探索 Caja守恒原则的核心作用 Caja守恒原则&#xff0c;即大脑组织的布线成本最小化原则&#xff0c;是神经科学中的一个重要概念。它指出&#xff0c;大脑在组织结构上倾向于最小化连接神经元以构成环路或网络所涉及的布…

数据结构之探索“堆”的奥秘

找往期文章包括但不限于本期文章中不懂的知识点&#xff1a; 个人主页&#xff1a;我要学编程(ಥ_ಥ)-CSDN博客 所属专栏&#xff1a;数据结构&#xff08;Java版&#xff09; 目录 堆的概念 堆的创建 时间复杂度分析&#xff1a; 堆的插入与删除 优先级队列 PriorityQ…

Docker安装kkFileView实现在线文件预览

kkFileView为文件文档在线预览解决方案,该项目使用流行的spring boot搭建,易上手和部署,基本支持主流办公文档的在线预览,如doc,docx,xls,xlsx,ppt,pptx,pdf,txt,zip,rar,图片,视频,音频等等 官方文档地址:https://kkview.cn/zh-cn/docs/production.html 一、拉取镜像 do…

go-kratos 学习笔记(6) 数据库gorm使用

数据库是项目的核心&#xff0c;数据库的链接数据是data层的操作&#xff0c;选择了比较简单好用的gorm作为数据库的工具&#xff1b;之前是PHP开发&#xff0c;各种框架都是orm的操作&#xff1b;gorm还是很相似的&#xff0c;使用起来比较顺手 go-kratos官网的实例是ent&…

轻松搭建 VirtualBox + Vagrant + Linux 虚拟机

一、准备工作 首先&#xff0c;我们来了解一下搭建 VirtualBox Vagrant Linux 虚拟机所需的软件准备工作。 VirtualBox 的下载地址&#xff1a;您可以通过访问https://www.virtualbox.org/wiki/Downloads获取适用于您系统的版本。 Vagrant 的下载地址&#xff1a;前往http…

斯坦福UE4 C++课学习补充 14:UMG-优化血量条

文章目录 一、优化执行效率二、简单脉冲动画 一、优化执行效率 绑定事件需要每一帧检查绑定对象是否有变化&#xff0c;势必造成CPU资源的浪费&#xff0c;因此优化执行效率的思路是&#xff1a;UI组件不再自行每帧查询血量&#xff0c;而是让血量自己在发生变化的同时通知UI进…

Linux环境下(DeepinV20+)安装并配置jdk和maven

一、jdk下载 Oracle的JDK开始收费了&#xff0c;如非必要&#xff0c;请勿使用&#xff01;&#xff01;&#xff01; jdk下载地址1&#xff08;推荐&#xff09;https://github.com/graalvm/graalvm-ce-builds/releases jdk下载地址2&#xff08;可选&#xff09;&#xff1a;…

LLM 大语言模型显存消耗估计与计算

LLM 大语言模型显存消耗估计与计算 1. LLM 大语言模型开发流程 在大模型&#xff08;如 LLaMA-7B、GPT-3 等&#xff09;的开发、训练、微调、推理和部署过程中&#xff0c;各个阶段的流程都涉及多个复杂的步骤。以下是详细的流程描述&#xff0c;涵盖训练和微调的区别&#…

SpringCloud+Vue3多对多,多表联查

♥️作者&#xff1a;小宋1021 &#x1f935;‍♂️个人主页&#xff1a;小宋1021主页 ♥️坚持分析平时学习到的项目以及学习到的软件开发知识&#xff0c;和大家一起努力呀&#xff01;&#xff01;&#xff01; &#x1f388;&#x1f388;加油&#xff01; 加油&#xff01…

学习日记:数据类型2

目录 1.转义字符 2.隐式类型转换 2.1 强制类型转换 2.2 不同类型间赋值 3.运算符 表达式 3.1 算术运算符 3.2 算术运算优先级 3.3 赋值运算 3.3.1 不同类型间混合赋值 3.4 逗号运算 4.生成随机数 5. 每日一练 1.转义字符 \n 表示换行 \t …

前端渲染模式

渲染的概念 在Web开发中&#xff0c;渲染&#xff08;Rendering&#xff09;是一个核心概念&#xff0c;指的是将应用程序的数据&#xff08;data&#xff09;与模板&#xff08;template&#xff09;结合&#xff0c;生成最终的HTML页面&#xff0c;这个页面随后会被浏览器解析…

RedHat9 | Ansible 角色

环境版本说明 RedHat9 [Red Hat Enterprise Linux release 9.0]Ansible [core 2.13.3]Python [3.9.10]jinja [3.1.2] 描述角色结构 Playbook可能比较冗长且负载&#xff0c;也可能存在大量的重复代码。而角色&#xff08;roles&#xff09;可以用于层次性结构化的组织playbo…

55. 跳跃游戏【 力扣(LeetCode) 】

一、题目描述 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个下标&#xff0c;如果可以&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 二、测试用…

在vue中优雅地异步引入(懒加载)腾讯地图API

背景 接到一个需求需要在网站首页显示使用腾讯地图展示公司所在地。一开始我直接全局引入了腾讯地图js&#xff0c;结果发现在用户打开登陆页面的时候首页比较缓慢&#xff0c;为了提高用户登陆的加载效率&#xff0c;需要优化为异步引入。 思路 根据官网的示例&#xff0c;…

SQL 注入漏洞详解 - Union 注入

1)漏洞简介 SQL 注入简介 SQL 注入 即是指 Web 应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在 Web 应用程序中事先定义好的查询语句的结尾上添加额外的 SQL 语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,…

【前端 02】新浪新闻项目-初步使用CSS来排版

在今天的博文中&#xff0c;我们将围绕“新浪新闻”项目&#xff0c;深入探讨HTML和CSS在网页制作中的基础应用。通过具体实例&#xff0c;我们将学习如何设置图片、标题、超链接以及文本排版&#xff0c;同时了解CSS的引入方式和选择器优先级&#xff0c;以及视频和音频标签的…

分布式光伏并网AM5SE-IS防孤岛保护装置介绍——安科瑞 叶西平

产品简介 功能&#xff1a; AM5SE-IS防孤岛保护装置主要适用于35kV、10kV及低压380V光伏发电、燃气发电等新能源并网供电系统。当发生孤岛现象时&#xff0c;可以快速切除并网点&#xff0c;使本站与电网侧快速脱离&#xff0c;保证整个电站和相关维护人员的生命安全。 应用…

Hello 算法:动画图解、一键运行的数据结构与算法教程

Hello 算法 《Hello 算法》是一份开源、免费的数据结构与算法入门教程&#xff0c;特别适合新手。全书采用动画图解&#xff0c;内容清晰易懂&#xff0c;学习曲线平滑&#xff0c;引导初学者探索数据结构与算法的知识地图。源代码可以一键运行&#xff0c;帮助读者通过练习提…