【问题标题】:How to handle the bounding rect clicking of QPainter如何处理 QPainter 的边界矩形点击
【发布时间】: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。
  • 如果没有看到您的尝试,我无法为您提供帮助

标签: c++ qt


【解决方案1】:

最好的方法是像这样覆盖类中 QWidget 的 mousePressEvent。

void mousePressEvent(QMouseEvent *event) override;

然后你调用任何你想从QMouseEvent 文档中获取位置的函数,但我会推荐 event->pos() 因为它返回相对于你调用它的小部件的位置。您可以在运行时计算每个扇区的位置,也可以将它们存储在 QList 或 QMap 中。然后比较位置,找出鼠标在哪个扇区。

【讨论】:

    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 2014-09-22
    • 2012-07-10
    • 1970-01-01
    • 2011-08-25
    • 2019-01-19
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多