-
UID:202628
-
- 注册时间2020-05-17
- 最后登录2025-01-07
- 在线时间456小时
-
- 发帖399
- 搜Ta的帖子
- 精华0
- 金钱5945
- 威望440
- 贡献值0
- 好评度360
-
访问TA的空间加好友用道具
|
如题,在QWidget里重写QPainter后,画了一个双环圆,假设外圆半径为R1,内圆半径为R2,如何实现鼠标移动到外圆和内圆之间的区域,即R1-R2之间的区域时,这个区域就会改变颜色呢?具体代码如下:
- void CWidget::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event);
- int m_radius = 61
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing, true);
- int arcHeight = 16;//30;
- painter.translate(width()/2, this->height()*44/100);
- QColor color("#95DE64"); Q_UNUSED(color);
- this->drawCircle(&painter, m_radius, 0, 360, arcHeight, qRgb(149, 222, 100));
- }
- void CWidget::drawCircle(QPainter *painter, int radius, int startAngle, int angleLength, int arcHeight, QRgb color)
- {
- painter->setBrush(QColor(color));
- // << 1(左移1位)相当于radius*2 即:150*2=300
- //QRectF(-150, -150, 300, 300)
- QRectF rect(-radius, -radius, radius << 1, radius << 1);
- QPainterPath path;
- path.arcTo(rect, startAngle, angleLength);
- // QRectF(-120, -120, 240, 240)
- QPainterPath subPath;
- subPath.addEllipse(rect.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));
- // path为扇形 subPath为椭圆
- path -= subPath;
- painter->setPen(Qt::NoPen);
- painter->drawPath(path);
- }
- void CWidget::mousePressEvent(QMouseEvent *event)
- {
- //鼠标按压时,外圆和内圆区域R1-R2改变颜色
- }
- void CWidget::mouseMoveEvent(QMouseEvent *event)
- {
- //鼠标移动时,外圆和内圆区域R1-R2改变颜色
- }
- void CWidget::mouseReleaseEvent(QMouseEvent *event)
- {
- //鼠标释放时,外圆和内圆区域R1-R2恢复原来的颜色
- }
请问各位大佬们,怎么才能判断鼠标移动到外圆和内圆R1-R2区域,并实现当鼠标在R1-R2区域内的时候,改变这个颜色,不在的时候,就恢复原来的颜色?
|