qt浮动窗口
提要
鼠標進入窗口后,窗口放大,且出現標題欄,鼠標移開,窗口標題欄隱藏,窗口還原。對于放大后超出顯示區域的窗口,將窗口進行移動,使其在顯示區域內。對于可以看清的窗口,只浮動顯示標題欄,不進行放大,默認認為窗口在700x700時可以看清。
示例
只展示關鍵代碼。
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();//最大值是否小于浮動窗口的區域700x700,小于出現浮動窗口,大于不需要出現浮動窗口if(max < FLOATWIN_RANGE){max = FLOATWIN_RANGE;//最長的邊為700int 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){rectWin.moveLeft(SCENE_W - rectWin.width());//setX會改變矩形的寬}if(rectWin.y() + rectWin.height() > SCENE_H){rectWin.moveTop(SCENE_H - 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<<"矩形恢復之前的寬:"<<m_oldSizePos.width()<<"高:"<<m_oldSizePos.height()<<"x:"<<m_oldSizePos.x()<<"y:"<<m_oldSizePos.y();m_isFloatWin = false;} }其中變量m_isFloatWin 初始化為false,其中enterEvent和leaveEvent為鼠標進入事件和鼠標離開事件。FLOATWIN_RANGE為定義的宏700,出現的浮動窗口的范圍在700x700內,這樣就不用考慮過大的窗口,如何出現浮動窗口,以及顯示區域定義的長寬大于700后,不用考慮左上角坐標出現在顯示區域外的情況,也屬于一種偷懶行為吧,但適用于我目前的項目,視情況而定了可酌情增刪代碼。
總結
- 上一篇: java读取excel某个单元格的值_[
- 下一篇: qt场景中视图QGraphicsView