QT-鼠标库不起作用的问题
我通过构造函数创建了一个多边形,并将其添加到了我调用构造函数并完成所有操作的场景中,但是现在我必须通过按箭头上的鼠标来发出信号,我添加了Mousepressevent,但是当我单击时,我会不会发出信号。 t将我输入QDebug或任何功能的信息,这可能是一个问题,如果有人可以提供解决方案,谢谢。
tranzicija.h
class tranzicija:public QObject,public QGraphicsPolygonItem
{
Q_OBJECT
public:
tranzicija();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
QPolygonF pol1;
void pomjeriproces();
};
tranzicija.cpp
tranzicija::tranzicija()
{
pol1 << QPoint(0,45) << QPoint(60,45) << QPoint(60,35) << QPoint(100, 50)
<< QPoint(60,65) << QPoint(60,55) << QPoint(0,55) << QPoint(0,45);
}
void tranzicija::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "The button was pressed!";
}
void tranzicija::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush pen(Qt::green);
painter->setBrush(pen);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawPolygon(pol1);
}
dialog.cpp
//Poligon start => ready
tranzicija *tr = new tranzicija();
tr->setRotation(45);
tr->setPos(170,125);
tr->setScale(0.8);
scene->addItem(tr);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们看不到
tranzicija
类的标头和源文件的相关部分,很难为您提供帮助。例如,很高兴知道MousePressEvent
是否被称为公共,受保护,信号等。It would be really hard to help you if we don't see the relevant parts of the header and source files of your
tranzicija
class. For example it would be great to know whether themousePressEvent
was declared as public, protected, signal etc.基于显示的代码在这里,问题是您永远不会呼叫 QT 图形框架对图形项目的形状/大小一无所知,何时在其上进行鼠标操作。
尝试将构造函数更改为...
您也可以将
pol1
在构造函数上进行本地化,并将其作为类成员删除。同样无需paint
覆盖 -qgraphics -polygonitem :: Paint
实现为您做到这一点。Based on the code shown here, the problem is that you never call
QGraphicsPolygonItem::setPolygon
. Hence theQt
graphics framework knows nothing about the shape/size of your graphics item and can't know when mouse actions take place on it.Try changing your constructor to...
You can probably also make
pol1
local to the constructor and remove it as a class member. Also no need for thepaint
override -- theQGraphicsPolygonItem::paint
implementation does that for you.