#include <QPoint>
#include <QMouseEvent>
#include <QTimer>
class CDemo : public QDialog
{
Q_OBJECT
public:
CDemo(QWidget *parent = Q_NULLPTR);
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent* event);//鼠標(biāo)釋放事件
private:
Ui::CDemoClass ui;
private:
bool m_isLeftPressed = false; //判斷是否是左鍵點(diǎn)擊
QPoint m_clickedPositon; //獲取鼠標(biāo)左鍵按下時(shí)光標(biāo)在全局(屏幕而非窗口)的位置
int m_distance=20;
};
#include "CDemo.h"
#include <QPushButton>
#include <QDebug>
CDemo::CDemo(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint); //隱藏菜單欄
setMouseTracking(true);//設(shè)置鼠標(biāo)追蹤
}
void CDemo::mouseMoveEvent(QMouseEvent *event)
{
Q_UNUSED(event);
if (this->isFullScreen()) return;//全屏?xí)r不處理
QPoint t_pos(this->geometry().width(), this->geometry().height());
m_distance = QLineF(t_pos, event->pos()).length();
if ( m_distance < 10){
setCursor(Qt::SizeFDiagCursor);
}else{
setCursor(Qt::ArrowCursor);
}
if (m_isLeftPressed)//是否左擊
{
QPoint t_pos = event->globalPos();//當(dāng)前鼠標(biāo)在桌面上的位置
t_pos = t_pos - m_clickedPositon;//減去點(diǎn)擊時(shí)的點(diǎn),得到新的點(diǎn)(x,y)是右下角的移動(dòng)距離
QRect t_lastWidget = this->geometry();//窗口的幾何位置
t_lastWidget.setBottomRight(t_lastWidget.bottomRight() + t_pos);//改變窗口右下角的位置
//設(shè)置最小大小
if (t_lastWidget.size().width()<200 || t_lastWidget.size().height() < 200)
{
return;
}
this->setGeometry(t_lastWidget);//更新窗口的集合位置
m_clickedPositon = event->globalPos();//更新位置
}
}
void CDemo::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event);
if (event->button() == Qt::LeftButton && m_distance < 10)
{
this->m_isLeftPressed = true;
QPoint t_pos = event->globalPos();
m_clickedPositon = t_pos;//記錄點(diǎn)擊時(shí)的點(diǎn)的坐標(biāo)
}
}
void CDemo::mouseReleaseEvent(QMouseEvent* event)
{
Q_UNUSED(event);
if (m_isLeftPressed)
m_isLeftPressed = false;
setCursor(Qt::ArrowCursor);
}
重寫鼠標(biāo)移動(dòng)事件就可以了,如果想實(shí)現(xiàn)其它位置的縮放,可以自己根據(jù)鼠標(biāo)的位置來實(shí)現(xiàn),思路都是一樣的文章來源地址http://www.zghlxwxcb.cn/news/detail-537060.html
文章來源:http://www.zghlxwxcb.cn/news/detail-537060.html
到了這里,關(guān)于Qt隱藏標(biāo)題欄,鼠標(biāo)實(shí)現(xiàn)窗口右下角放縮窗口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!