Drag in QGraphicsView



  • Hello!

    I have a problem with my QGraphicsView object. I put an image inside it but the image is bigger than the QGraphicsView so, when I move the mouse over the image, the image moves too. I don't want this effect. It is like when I drag the image but without pressing it.

    How can I fix it?

    Thank you very much!


  • Lifetime Qt Champion

    Hi,

    Do you mean when you move the item which contains that big image ?

    Can you share the code you use ?



  • Yes, when I run the application, it has a QGraphicsView with an image. The problem is that I can move the image without doing drag, if I move the mouse over the image, I can move it, but this situation only should happen when I do drag on the image. I put the mouseTracking on because I need the image's coordinates.

    I would share my code, but it is very long.

    Thank you very much!!


  • Lifetime Qt Champion

    Can you show your code ?



  • @SGaist Ok, here is my code.

    This is the constructor of my QGraphicsView class:

    imageView::imageView(QWidget *parent)
    {
        /* Inicialización de elementos gráficos **************************************** */
        scene = new QGraphicsScene();
        this->setScene(scene);
        //this->resize(788, 1044);
    
        item = new QGraphicsPixmapItem();
    
        scene->addItem(item);
        this->setMouseTracking(true);
        this->scene->setItemIndexMethod(QGraphicsScene::NoIndex);
        connect(scene, &QGraphicsScene::selectionChanged, this, &imageView::applySelection);
        //item->setFlag(QGraphicsPixmapItem::ItemIsMovable, false);
        //this->show();
    
        this->setCacheMode(CacheBackground);
        this->setViewportUpdateMode(BoundingRectViewportUpdate);
        this->setRenderHint(QPainter::Antialiasing);
        this->setTransformationAnchor(AnchorUnderMouse);
    }
    

    And this is my declaration code:

    In *.h

    QSharedPointer<imageView> thermalImage;
    

    In *.cpp

    thermalImage.reset(new imageView(this));
    // Al connects...
    ui->imageLayout->addWidget(thermalImage.data());
    

    Thank you.


  • Lifetime Qt Champion

    Can you provide a complete minimal sample ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.