首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

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

后端 71.75KB 37 需要积分: 1
立即下载

资源介绍:

本示例介绍了如何使用Qt框架创建一个无边框窗口,并提供了详细的源码,包括窗口样式设置、移动区域控制和窗口大小调整功能。用于设置窗口为无边框窗口。可为窗口添加自定义标题栏、边框和系统菜单按钮。
#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(event)); break; case QEvent::MouseButtonRelease: handleMouseReleaseEvent(static_cast(event)); break; case QEvent::MouseMove: handleMouseMoveEvent(static_cast(event)); break; case QEvent::Leave: handleLeaveEvent(static_cast(event)); break; case QEvent::HoverMove: handleHoverMoveEvent(static_cast(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(); else origRect = 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()); else newRect.setRight(origRect.right()); } if (minHeight > newRect.height()) { if (top != origRect.top()) newRect.setTop(origRect.top()); else newRect.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) {

资源文件列表:

qt-custom-window.zip 大约有20个文件
  1. qt-custom-window/
  2. qt-custom-window/BaseTitleBar.cpp 7.78KB
  3. qt-custom-window/BaseTitleBar.h 2.42KB
  4. qt-custom-window/BaseWindow.cpp 2.5KB
  5. qt-custom-window/BaseWindow.h 544B
  6. qt-custom-window/CustomMainWindow.cpp 1.12KB
  7. qt-custom-window/CustomMainWindow.h 446B
  8. qt-custom-window/CustomMainWindow.ui 618B
  9. qt-custom-window/CustomWindow.pro 396B
  10. qt-custom-window/framelesshelper.cpp 11.82KB
  11. qt-custom-window/framelesshelper.h 4.27KB
  12. qt-custom-window/main.cpp 184B
  13. qt-custom-window/TitleBarRc/
  14. qt-custom-window/TitleBarRc/close.png 2.7KB
  15. qt-custom-window/TitleBarRc/icon.png 52.04KB
  16. qt-custom-window/TitleBarRc/max.png 1.31KB
  17. qt-custom-window/TitleBarRc/min.png 1.36KB
  18. qt-custom-window/TitleBarRc/MyTitle.css 1.3KB
  19. qt-custom-window/TitleBarRc/restore.png 1.34KB
  20. qt-custom-window/titlebarrc.qrc 302B
0评论
提交 加载更多评论
其他资源 TB4117海康威视摄像头软件代码
很多人用TB4117改手持摄像头,也有产品设计用来做防疫产品的。
lnux netwkdfjklajdfklja lkjsldkfjasl kjfl sfakjlksdafjlsk
lnux netwkdfjklajdfklja lkjsldkfjasl kjfl sfakjlksdafjlsk
uniapp项目源码由咸虾米课程,可以用来开源自用共享
uniapp项目,点个关注后下载谢谢
基于SpringBoo防止接口重复提交代码示例
基于SpringBoo防止接口重复提交代码示例
串口modbusRTU通讯学习程序
学习DCS电脑组态跟PLC modbus RTU 通讯的练习软件
图片转PDF及PDF转TXT证件防伪水印工具
图片转PDF及PDF转TXT证件防伪水印工具,实现方式仅供学习参考。
注册中心nacos文件夹
注册中心nacos文件夹
QT环形进度条(圆形进度条)
在Qt自带的控件中,只有垂直进度条、水平进度条两种。在平时做页面开发时,有些时候会用到环形进度条,比如说:下载某个文件的下载进度。本示例使用QT实现一个自定义环形进度条。