Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

incompréhension avec QGraphicsSceneMouseEvent



  • Bonjour,
    Dans cet exemple, selon que le texte soit affiché ou pas, le comportement de la souris change.
    Si vous pouviez me donner l'explication afin que je puisse corriger.
    Merci d'avance.
    Voici mon code:```

    le h:

    #ifndef FENETRE_H
    #define FENETRE_H
    
    #include <QMouseEvent>
    #include <QGraphicsView>
    #include <QLabel>
    
    class Fenetre : public QGraphicsView
    {
        Q_OBJECT
    public:
        Fenetre(QWidget *parent=0);
    
       bool eventFilter(QObject *watched, QEvent *event);
    
    
    private:
        QGraphicsScene *scene;
    
    
        QGraphicsLineItem *ligne;
    QGraphicsLineItem *ligne0;
        QGraphicsTextItem *text;
    
         int debutX;
        int debutY;
    
        int finX;
        int finY;
    };
    #endif // FENETREE_H
    

    le cpp:

    #include "Fenetre.h"
    
    #include <QEvent>
    #include <QGraphicsPixmapItem>
    #include <QGraphicsSceneMouseEvent>
    
    #include <QDebug>
    
    Fenetre::Fenetre(QWidget *parent):QGraphicsView(parent)
    {
        scene = new QGraphicsScene(this);
    
        scene->setSceneRect(0,0,1000,1000);
    
    setScene(scene);
    
    
    QPen redPen;
    redPen.setColor(Qt::red);
    redPen.setWidth(3);
    
    ligne0 = scene->addLine(20,20,400,400);
    ligne0->setPen(redPen);
    
    QString test = "BLABLABLA";
    text = new QGraphicsTextItem();
    
    text = scene->addText(test, QFont ("Arial",10));
    scene->isActive();
    text->setPos(100,100);
    
    scene->installEventFilter(this);
    }
    
    bool Fenetre::eventFilter(QObject *watched, QEvent *event)
    {    
           if (watched == scene){
    
            QGraphicsSceneMouseEvent *mouseSceneEvent;
            if(event->type() == QEvent::GraphicsSceneMousePress){
    
                mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
              debutX  = mouseSceneEvent->lastScenePos().x();
             debutY  = mouseSceneEvent->lastScenePos().y();      
    
            }
    
          else if (event->type() == QEvent::GraphicsSceneMouseMove) {
    
                mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                finX = mouseSceneEvent->scenePos().x();
                finY = mouseSceneEvent->scenePos().y();
    
                 QPen bluePen;
                    bluePen.setColor(Qt::blue);
                    bluePen.setWidth(3);
    
                     ligne = scene->addLine(debutX,debutY,finX,finY);
                    ligne->setPen(bluePen);
    
            }
    
       else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
    
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
          qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
    
       }
        }
       return QGraphicsView::eventFilter(watched, event);
    
    
    }
    
    
    
    

  • Lifetime Qt Champion

    Bonjour,

    Quelles sont les différences que vous observez ?



  • Bonjour,
    Les lignes tracées avec la souris ne sont plus régulières mais erratiques.

    Merci.


  • Lifetime Qt Champion

    Quelle version de Qt ?
    Sur quel OS ?


Log in to reply