Home > database >  Qt - The mousePressEvent problem is not working
Qt - The mousePressEvent problem is not working

Time:06-13

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);

CodePudding user response:

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.

CodePudding user response:

   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);

CodePudding user response:

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.

  •  Tags:  
  • c qt
  • Related