【发布时间】:2020-11-18 07:01:17
【问题描述】:
现在我使用 QPainterPath 和旋转技术绘制我的自定义小部件,此处列出了代码 sn-p:
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')));
}
但现在我如何处理EVERY SECTOR点击。
【问题讨论】:
-
据我了解,每个部门都只是您正在实施的小部件上的绘图。如果您不想更改当前的实现,我建议您覆盖此小部件
mouseClickEvent并计算用户点击了哪个扇区。为此,您需要某种容器来存储所有扇区及其位置(例如,可能是地图)。当用户点击小部件时,您会在小部件的本地坐标中计算鼠标点击位置,并通过计算出的坐标检索确切的扇区 - 这就是需要重绘的扇区。 -
当然,这似乎是一个非常糟糕的实现,这不是我首先推荐的。如果您将 QGraphicsView 与 QGraphicsItems 一起使用,您将有更好的时间编写应用程序。在这种情况下,您将 QGraphicsView 作为您的小部件的模拟,但每个扇区都是一个单独的 QGraphicsItem 对象,您可以通过简单地更改其笔或画笔轻松地对其进行寻址和重绘。
-
现在我用 QGraphicsItem 重写,但是根本没有调用 mousePressEvent。
-
如果没有看到您的尝试,我无法为您提供帮助