现在的位置: 首页 > 综合 > 正文

Qt自定义窗口事件

2018年01月22日 ⁄ 综合 ⁄ 共 5126字 ⁄ 字号 评论关闭

http://blog.csdn.net/chenjinxian88/article/details/8698410

一、移动主界面

       移动主界面是通过按住鼠标左键进行标题栏拖动最终导致主界面移动;由于还有窗口伸缩功能,因此对于标题栏左部,顶部,右部应该腾出5像素空间给窗口伸缩功能使用,即鼠标移动到这5像素空间之内的话,鼠标形状就会发生改变(暗示可以伸缩窗口);为什么只有标题栏腾出5像素空间,而其他部件(如工具栏、内容区域、状态栏)就不需要了?因为只有标题栏部件重新实现了void
mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);这三个事件;而主窗口也实现了它自己的这三个事件,为了防止界面移动和界面伸缩相冲突,所以留有5像素的空间为窗口伸缩功能使用;下面讲解移动主界面的代码实现:

       总体思路是:鼠标按下时设置按下标识并保存按下点坐标;鼠标移动时,判断是否按下(标识)然后获得移动鼠标点的坐标,根据两者的差值最后移动主界面即可;当然,鼠标释放时肯定要重置按下标识。 

[html] view
plain
copy

  1. //鼠标按下事件  
  2. void TitleBar::mousePressEvent(QMouseEvent *event)  
  3. {  
  4.     if (event->button() == Qt::LeftButton)  
  5.     {  
  6.      if(event->y()<VALUE_DIS||event->x()<VALUE_DIS||rect().width()-event->x()<5)  
  7.         {  
  8.             event->ignore();  
  9.             return;  
  10.         }  
  11.         m_ptPress = event->globalPos();  
  12.         m_bLeftButtonPress = true;  
  13.     }  
  14.     event->ignore();  
  15. }  
  16. //鼠标移动事件  
  17. void TitleBar::mouseMoveEvent(QMouseEvent *event)  
  18. {  
  19.     if(m_bLeftButtonPress)  
  20.     {  
  21.         m_ptMove = event->globalPos();  
  22.         //移动主窗口  
  23.         MainWindow *pMainWindow = (qobject_cast<MainWindow *>(parent()));  
  24.         pMainWindow->move(pMainWindow->pos()+m_ptMove-m_ptPress);  
  25.         //重新设置m_ptPress;  
  26.         m_ptPress = m_ptMove;  
  27.     }  
  28.     event->ignore();  
  29. }  
  30. //鼠标释放事件  
  31. void TitleBar::mouseReleaseEvent(QMouseEvent *event)  
  32. {  
  33.     if (event->button() == Qt::LeftButton)  
  34.     {  
  35.         m_bLeftButtonPress = false;  
  36.     }  
  37.      event->ignore();  
  38. }  

      注意,在事件的末尾要加上event->ignore();语句,因为标题栏是覆盖在主界面部件之上的,所以事件传递是先传递给标题栏,标题栏完成该事件之后,应使用event->ignore();表示继续将事件传递给其父对象(即主界面部件);
二、伸缩主界面 
       界面当然要可以伸缩,即窗口变大变小,这些也是由鼠标事件产生的,也是三个事件处理代码;当鼠标移动到主界面内部周围5像素时,改变鼠标形状;当进行伸缩拖动时,根据拖动方向进行主界面的位置和大小设置即可。

[html] view
plain
copy

  1. View Code   
  2.   
  3. //鼠标按下事件  
  4. void MainWindow::mousePressEvent(QMouseEvent *event)  
  5. {  
  6.     if (event->button() == Qt::LeftButton)  
  7.     {  
  8.         m_ptPressGlobal = event->globalPos();  
  9.         m_bLeftBtnPress = true;  
  10.     }  
  11. }  
  12. //鼠标移动事件  
  13. void MainWindow::mouseMoveEvent(QMouseEvent *event)  
  14. {  
  15.     if(!m_bLeftBtnPress)  
  16.     {  
  17.         m_eDirection = PointValid(event->x(),event->y());  
  18.         SetCursorStyle(m_eDirection);  
  19.     }  
  20.     else  
  21.     {  
  22.         int nXGlobal = event->globalX();  
  23.         int nYGlobal = event->globalY();  
  24.         SetDrayMove(nXGlobal,nYGlobal,m_eDirection);  
  25.         m_ptPressGlobal =QPoint(nXGlobal,nYGlobal);  
  26.     }  
  27. }  
  28. //鼠标释放事件  
  29. void MainWindow::mouseReleaseEvent(QMouseEvent *event)  
  30. {  
  31.     if (event->button() == Qt::LeftButton)  
  32.     {  
  33.         m_bLeftBtnPress = false;  
  34.         m_eDirection = eNone;  
  35.     }  
  36.  }  
  37. //鼠标双击事件  
  38. void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)  
  39. {  
  40.     if (event->button() == Qt::LeftButton && event->y()<= m_pTitleBar->height())  
  41.     {  
  42.         if(!m_bMaxWin)  
  43.         {  
  44.             m_rectRestoreWindow = geometry();  
  45.             setGeometry(qApp->desktop()->availableGeometry());  
  46.         }  
  47.         else  
  48.         {  
  49.             setGeometry(m_rectRestoreWindow);  
  50.         }  
  51.         m_bMaxWin = !m_bMaxWin;  
  52.     }  
  53.  }  


其中设置鼠标形状的代码如下:

[html] view
plain
copy

  1. View Code   
  2.   
  3. //设置鼠标样式  
  4. void MainWindow::SetCursorStyle(enum_Direction direction)  
  5. {  
  6.     //设置上下左右以及右上、右下、左上、坐下的鼠标形状  
  7.     switch(direction)  
  8.     {  
  9.     case eTop:  
  10.     case eBottom:  
  11.         setCursor(Qt::SizeVerCursor);  
  12.         break;  
  13.     case eRight:  
  14.     case eLeft:  
  15.         setCursor(Qt::SizeHorCursor);  
  16.         break;  
  17.     case eTopRight:  
  18.     case eBottomLeft:  
  19.         setCursor(Qt::SizeBDiagCursor);  
  20.         break;  
  21.     case eRightBottom:  
  22.     case eLeftTop:  
  23.         setCursor(Qt::SizeFDiagCursor);  
  24.         break;  
  25.     default:  
  26.         setCursor(Qt::ArrowCursor);  
  27.         break;  
  28.     }  
  29. }  


[html] view
plain
copy

  1. View Code   
  2.   
  3. //设置鼠标拖动的窗口位置信息  
  4. void MainWindow::SetDrayMove(int nXGlobal,int nYGlobal,enum_Direction direction)  
  5. {  
  6.     //计算偏差  
  7.     int ndX = nXGlobal - m_ptPressGlobal.x();  
  8.     int ndY = nYGlobal - m_ptPressGlobal.y();  
  9.     //获得主窗口位置信息  
  10.     QRect rectWindow = geometry();  
  11.     //判别方向  
  12.     if(direction & eTop)  
  13.     {  
  14.         rectWindow.setTop(rectWindow.top()+ndY);  
  15.     }  
  16.     if(direction & eRight)  
  17.     {  
  18.         rectWindow.setRight(rectWindow.right()+ndX);  
  19.     }  
  20.     if(direction & eBottom)  
  21.     {  
  22.         rectWindow.setBottom(rectWindow.bottom()+ndY);  
  23.     }  
  24.     if(direction & eLeft)  
  25.     {  
  26.         rectWindow.setLeft(rectWindow.left()+ndX);  
  27.     }  
  28.     if(rectWindow.width()< minimumWidth() || rectWindow.height()<minimumHeight())  
  29.     {  
  30.         return;  
  31.     }  
  32.     //重新设置窗口位置为新位置信息  
  33.     setGeometry(rectWindow);  
  34. }  

三、双击最大化和还原
       这个功能处理起来很简单,只要重新实现void mouseDoubleClickEvent(QMouseEvent *event)事件即可并且限制有效范围,即在不超过标题栏高度的像素空间范围内双击才有效:

[html] view
plain
copy

  1. View Code   
  2.   
  3. //鼠标双击事件  
  4. void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)  
  5. {  
  6.     if (event->button() == Qt::LeftButton && event->y()<= m_pTitleBar->height())  
  7.     {  
  8.         if(!m_bMaxWin)  
  9.         {  
  10.             m_rectRestoreWindow = geometry();  
  11.             setGeometry(qApp->desktop()->availableGeometry());  
  12.         }  
  13.         else  
  14.         {  
  15.             setGeometry(m_rectRestoreWindow);  
  16.         }  
  17.         m_bMaxWin = !m_bMaxWin;  
  18.     }  
  19. }  

抱歉!评论已关闭.