生活随笔
收集整理的這篇文章主要介紹了
Qt实现浮动窗口
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
提要
在窗口上懸浮鼠標(biāo),此時(shí)窗口標(biāo)題欄顯示,窗口變大,鼠標(biāo)移開后,窗口恢復(fù)原來的大小,標(biāo)題欄隱藏。
示例
直接上代碼,只包含只要的代碼部分。
在自定義的窗口中重寫鼠標(biāo)進(jìn)入事件enterEvent和鼠標(biāo)離開事件leaveEvent,在其內(nèi)部實(shí)現(xiàn)窗口的變大,恢復(fù)。
在構(gòu)造函數(shù)中將標(biāo)題欄隱藏。
ui->titleWidget為自定義標(biāo)題欄。
ui
->titleWidget
->hide();
QRect m_oldSizePos
;
bool m_isFloatWin
;
#define BASE_W 1920
#define BASE_H 1080
#define FLOATWIN_RANGE 800
void CustomWidget::enterEvent(QEvent
*event
)
{ui
->titleWidget
->show();m_oldSizePos
= geometry();QRect rectWin
= geometry();outPut
<<"矩形的寬:"<<rectWin
.width()<<"高:"<<rectWin
.height()<<"x:"<<rectWin
.x()<<"y:"<<rectWin
.y();int max
= rectWin
.width() > rectWin
.height() ? rectWin
.width():rectWin
.height();if(max
< FLOATWIN_RANGE
){max
= FLOATWIN_RANGE
;int otherLen
;if(rectWin
.width() >= rectWin
.height()){otherLen
= (float)FLOATWIN_RANGE
/ rectWin
.width() * rectWin
.height();rectWin
.setWidth(max
);rectWin
.setHeight(otherLen
);}else{otherLen
= (float)FLOATWIN_RANGE
/ rectWin
.height() * rectWin
.width();rectWin
.setWidth(otherLen
);rectWin
.setHeight(max
);}if(rectWin
.x() + rectWin
.width() > SCENE_W
/ m_percentW
){rectWin
.moveLeft(SCENE_W
/ m_percentW
- rectWin
.width());}if(rectWin
.y() + rectWin
.height() > SCENE_H
/ m_percentH
){rectWin
.moveTop(SCENE_H
/m_percentH
- rectWin
.height());}setGeometry(rectWin
);m_isFloatWin
= true;outPut
<<"矩形放大后寬:"<<rectWin
.width()<<"高:"<<rectWin
.height()<<"x:"<<rectWin
.x()<<"y:"<<rectWin
.y();}
}void CustomWidget::leaveEvent(QEvent
*event
)
{ui
->titleWidget
->hide();if(m_isFloatWin
){setGeometry(m_oldSizePos
);outPut
<<"矩形恢復(fù)之前的寬:"<<m_oldSizePos
.width()<<"高:"<<m_oldSizePos
.height()<<"x:"<<m_oldSizePos
.x()<<"y:"<<m_oldSizePos
.y();m_isFloatWin
= false;}
}
以上可以將原來的窗口在鼠標(biāo)懸浮時(shí)放大并顯示標(biāo)題欄,鼠標(biāo)移開窗口,窗口恢復(fù)之前的大小。
總結(jié)
以上是生活随笔為你收集整理的Qt实现浮动窗口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。