改變鼠標形狀,在繪制坐標系的時候有用到,特此記下:
1 this->setMouseTracking(true); //設置為不按下鼠標鍵觸發moveEvent 2 void mouseMoveEvent(QMouseEvent* event) 3 { 4 QPoint mousepos = event()->pos(); 5 6 //在坐標(0 ~ width,0 ~ height)范圍內改變鼠標形狀 7 if(mousepos.rx() > 0 8 && mousepos.rx() < width 9 && mousepos.ry() > 0 10 && mousepos.ry() < height) 11 { 12 this->setCursor(Qt::CrossCursor); 13 } 14 else 15 { 16 this->setCursor(Qt::ArrowCursor); //范圍之外變回原來形狀 17 } 18 }
鼠標形狀對應的枚舉值:
Qt::ArrowCursor
Qt::UpArrowCursor
Qt::CrossCursor
Qt::IBeamCursor
Qt::WaitCursor
Qt::BusyCursor
Qt::ForbiddenCursor
Qt::PointingHandCursor
Qt::WhatsThisCursor
Qt::SizeVerCursor
Qt::SizeHorCursor
Qt::SizeBDiagCursor
Qt::SizeFDiagCursor
Qt::SizeAllCursor
Qt::SplitVCursor
Qt::SplitHCursor
Qt::OpenHandCursor
Qt::ClosedHandCursor
Qt::BlankCursor
還可以自定義鼠標形狀:
1 Qcurosr *myCursor=new QCursor(QPixmap(":/images/custom.png"),-1,-1); //-1,-1表示熱點位於圖片中心 2 this->setCursor(*myCursor);