Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何处理QPaint的边界矩形单击_C++_Qt - Fatal编程技术网

C++ 如何处理QPaint的边界矩形单击

C++ 如何处理QPaint的边界矩形单击,c++,qt,C++,Qt,现在,我使用QPainterPath和rotate技术绘制自定义小部件,下面列出了代码片段: QRectF flowerBanRect(-radius + (-width / 2), -radius + (-margin) + (-height), width, height); QPainterPath flowerPath; flowerPath.moveTo(-radius + (-width / 4), -diameter + (-margin)); flowerPath.lineTo(

现在,我使用QPainterPath和rotate技术绘制自定义小部件,下面列出了代码片段:

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')));
}
QRectF flowerBanRect(-radius+-width/2),-radius+-margin+-height,width,height);
QPainterPath-flowerPath;
花径移动到(-radius+(-width/4),-diameter+(-margin));
花径。线到(-半径+宽度/4,-直径+(-边缘));
花径线到(-radius+width/2,-直径+(-margin)+(height));
花径线到(-radius+-width/2),-diameter+-margin+-height);
QPainterPath diffPath=花卉路径-头路径;
油漆工->退刀(QBrush(getReagentSeatColor(0));
painter->fillPath(diffPath,QBrush(getReagentSeatColor(0));
painter->drawText(flowerBanRect,Qt::AlignHCenter | Qt::AlignTop,QString(“01”);
画师->保存();
浮动度=360.0/最大托盘;
对于(int i=0;i旋转(度);
画家->挫折(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'));
}

但是现在我如何处理每个扇区的点击。

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

void mousePressEvent(QMouseEvent *event) override;

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

据我所知,每个扇区都只是您正在实现的小部件上的一个图形。如果您不想更改当前的实现,我建议您覆盖此小部件
mouseClickEvent
,并计算用户单击的扇区。为此,您需要某种容器来存储所有扇区及其位置(例如,可能是地图)。当用户单击小部件时,您计算小部件局部坐标中的鼠标单击位置,并通过计算的坐标检索准确的扇区-这是需要重新绘制的扇区。当然,这看起来是一个非常糟糕的实现,而这并不是我首先推荐的。如果您将QGraphicsView与QGraphicsSitems结合使用,那么您编写应用程序的时间会更好。在这种情况下,您将使用QGraphicsView模拟您的小部件,但每个扇区都将是一个单独的QGraphicsItem对象,您只需更改其笔或画笔即可轻松寻址和重画。现在我使用QGraphicsSitem重写,但MousePresseEvent根本不被调用。如果不看到您所做的尝试,我将无法帮助您