简体   繁体   English

如何处理 QPainter 的边界矩形点击

[英]How to handle the bounding rect clicking of QPainter

在此处输入图片说明

Now I draw my custom widget by using QPainterPath and rotate technique, code snippet is listed here:现在我使用 QPainterPath 和旋转技术绘制我的自定义小部件,这里列出了代码片段:

QRectF flowerBanRect(-radius + (-width / 2), -radius + (-margin) + (-height), width, height);
QPainterPath flowerPath;
flowerPath.moveTo(-radius + (-width / 4), -diameter + (-margin));
flowerPath.lineTo(-radius + width / 4, -diameter + (-margin));
flowerPath.lineTo(-radius + width / 2, -diameter + (-margin) + (-height));
flowerPath.lineTo(-radius + (-width / 2), -diameter + (-margin) + (-height));

QPainterPath diffPath = flowerPath - headPath;

painter->setBrush(QBrush(getReagentSeatColor(0)));
painter->fillPath(diffPath, QBrush(getReagentSeatColor(0)));
painter->drawText(flowerBanRect, Qt::AlignHCenter | Qt::AlignTop, QString("01"));
painter->save();

float degree = 360.0 / MAX_TRAY;
for (int i = 0; i < MAX_TRAY - 1; ++i) {
    painter->rotate(degree);
    painter->setBrush(QBrush(getReagentSeatColor(i + 1)));
    painter->fillPath(diffPath, QBrush(getReagentSeatColor(i + 1)));
    painter->drawText(flowerBanRect, Qt::AlignHCenter | Qt::AlignTop, QString("%1").arg(i + 2, 2, 10, QChar('0')));
}

But now how I handle the EVERY SECTOR clicking.但是现在我如何处理每个部门的点击。

The best approach would be to override the mousePressEvent of the QWidget in your class like this.最好的方法是像这样覆盖类中 QWidget 的 mousePressEvent 。

void mousePressEvent(QMouseEvent *event) override;

Then you call whatever function you want to get the position from the QMouseEvent doc, but I would recommend event->pos() because it returns the position relative to the widget you are calling it from.然后你调用任何你想要从QMouseEvent文档中获取位置的函数,但我会推荐event- >pos() 因为它返回相对于你调用它的小部件的位置。 You can either calculate the positions of each sector at run time, or you can store them in a QList or QMap.您可以在运行时计算每个扇区的位置,也可以将它们存储在 QList 或 QMap 中。 Then compare the positions and figure out which sector the mouse is in.然后比较位置并找出鼠标所在的扇区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM