QT-鼠标库不起作用的问题

发布于 2025-02-06 20:13:41 字数 1447 浏览 2 评论 0 原文

我通过构造函数创建了一个多边形,并将其添加到了我调用构造函数并完成所有操作的场景中,但是现在我必须通过按箭头上的鼠标来发出信号,我添加了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);

I created a polygon through the constructor and added it to the scene where I call the constructor and do all that, but now I have to make a signal by pressing the mouse on the arrow, I added mousePressEvent, but when I click I don't get the message I put into qDebug or any functionality, which might be a problem, if someone can offer a solution, thank you.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2025-02-13 20:13:41

如果我们看不到 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 the mousePressEvent was declared as public, protected, signal etc.

小瓶盖 2025-02-13 20:13:41

基于显示的代码在这里,问题是您永远不会呼叫 QT 图形框架对图形项目的形状/大小一无所知,何时在其上进行鼠标操作。

尝试将构造函数更改为...

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);
    setPolygon(pol1);
}

您也可以将 pol1 在构造函数上进行本地化,并将其作为类成员删除。同样无需 paint 覆盖 - qgraphics -polygonitem :: Paint 实现为您做到这一点。

Based on the code shown here, the problem is that you never call QGraphicsPolygonItem::setPolygon. Hence the Qt 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...

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);
    setPolygon(pol1);
}

You can probably also make pol1 local to the constructor and remove it as a class member. Also no need for the paint override -- the QGraphicsPolygonItem::paint implementation does that for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文