1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > QT实现可移动和改变尺寸的无边框窗口

QT实现可移动和改变尺寸的无边框窗口

时间:2024-04-11 09:44:22

相关推荐

QT实现可移动和改变尺寸的无边框窗口

独角兽企业重金招聘Python工程师标准>>>

实现QT无边框窗口的拖动,尺寸改变。需要的时候从该类继承,派生类需要定义为QWidget。

头文件:

#ifndef SIZEABLEWIDGET_H#define SIZEABLEWIDGET_H#include <QWidget>class SizeableWidget : public QWidget{Q_OBJECTpublic:SizeableWidget(QWidget *parent = 0);//允许或禁止移动bool isMoveable()const;void setMoveable(bool moveable);//允许或禁止改变大小bool isSizeable()const;void setSizeable(bool sizeable);private:void mousePressEvent (QMouseEvent* event);void mouseReleaseEvent(QMouseEvent* event);void mouseMoveEvent(QMouseEvent* event);int calcPosition(const QPoint& pt);void setCursorType(int value);private:bool m_bMoveable;bool m_bSizeable;bool m_bLeftMouseButtonPressed;intm_lastPosition;QPoint m_ptLast;Qt::CursorShape m_currentCursor;};#endif // SIZEABLEWIDGET_H

源文件:

#include "sizeablewidget.h"#include <QPainter>#include <QMouseEvent>namespace{const int g_padding = 4;int helperCalcPosition(int pos, int range){return (pos < g_padding) ? 0 : ((pos > (range - g_padding)) ? 2 : 1);}}SizeableWidget::SizeableWidget(QWidget *parent) :QWidget(parent) ,m_bMoveable(true) ,m_bSizeable(true) ,m_bLeftMouseButtonPressed(false) ,m_lastPosition(11) ,m_currentCursor(Qt::ArrowCursor){setCursor(m_currentCursor);setMinimumSize(QSize(32, 32));}bool SizeableWidget::isMoveable()const{return m_bMoveable;}void SizeableWidget::setMoveable(bool moveable){m_bMoveable = moveable;}bool SizeableWidget::isSizeable()const{return m_bSizeable;}void SizeableWidget::setSizeable(bool sizeable){m_bSizeable = sizeable;}int SizeableWidget::calcPosition(const QPoint& pt){int row = helperCalcPosition(pt.y(), rect().height());int col = helperCalcPosition(pt.x(), rect().width());return row * 10 + col;}void SizeableWidget::mousePressEvent(QMouseEvent* event){m_bLeftMouseButtonPressed = true;m_ptLast = event->globalPos();m_lastPosition = calcPosition(event->pos());QWidget::mousePressEvent(event);}void SizeableWidget::mouseReleaseEvent(QMouseEvent* event){m_bLeftMouseButtonPressed = false;QWidget::mouseReleaseEvent(event);}void SizeableWidget::mouseMoveEvent(QMouseEvent* event){if(m_bLeftMouseButtonPressed){QPoint ptNew = event->globalPos();ptNew -= m_ptLast;if(11 == m_lastPosition) //拖动{if (m_bMoveable){ptNew += pos();move(ptNew);}}else //调整大小{if (m_bSizeable){QRect rectWindow = geometry();switch(m_lastPosition){case 00:rectWindow.setTopLeft(rectWindow.topLeft() + ptNew);break;case 02:rectWindow.setTopRight(rectWindow.topRight() + ptNew);break;case 20:rectWindow.setBottomLeft(rectWindow.bottomLeft() + ptNew);break;case 22:rectWindow.setBottomRight(rectWindow.bottomRight() + ptNew);break;case 10:rectWindow.setLeft(rectWindow.left() + ptNew.x());break;case 12:rectWindow.setRight(rectWindow.right() + ptNew.x());break;case 01:rectWindow.setTop(rectWindow.top() + ptNew.y());break;case 21:rectWindow.setBottom(rectWindow.bottom() + ptNew.y());break;default:Q_ASSERT(0);}setGeometry(rectWindow);}}m_ptLast = event->globalPos();}else{if (m_bSizeable)setCursorType(calcPosition(event->pos()));}QWidget::mouseMoveEvent(event);}void SizeableWidget::setCursorType(int value){Qt::CursorShape cursor;switch(value){case 00:case 22:cursor = Qt::SizeFDiagCursor;break;case 02:case 20:cursor = Qt::SizeBDiagCursor;break;case 10:case 12:cursor = Qt::SizeHorCursor;break;case 01:case 21:cursor = Qt::SizeVerCursor;break;case 11:cursor = Qt::ArrowCursor;break;default:Q_ASSERT(0);break;}if(cursor != m_currentCursor){m_currentCursor = cursor;setCursor(m_currentCursor);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。