Nominate our 2022 Qt Champions!

Mouse tracking on QGraphicsView

  • Hello!

    I have a class derivated of QGraphicsView. I had to enable the mouse tracking property because I have to get the mouse position coordinates. Now, the problem is that when I move the mouse on the QGraphicsView, I can move the image inside "without clicking it".

    How could I disable this first click?

  • @ivanicy

    Try setting to false,l setflag()

    setFlag(QGraphicsItem::ItemIsMovable, false);

  • Thank you very much for answer me but this fix doesn't work. The problem is: at first, when I move the mouse, the image moves too, but, when I click the image, the movement stops, and I want to stop the movement always.

  • Qt Champions 2017

    If image moves means u must be setting image coordinates with mouse move coordinates. Can u paste the sample code ?

  • @dheerendra This is my code section:

        /* MODO POR DEFECTO (ZOOM) ****************************************************************************** */
        if (graphicMode == ZOOM_MODE) {
        //if (sceneMode == NoMode) {
            QPointF pos = this->mapToScene(event->pos());
            int _posX = pos.x();
            int _posY = pos.y();
            if (this->hasMouseTracking() && !item->pixmap().isNull()) {
                if (_posX >= 0 && _posX <= this->imageData.cols && _posY >= 0 && _posY <= this->imageData.rows)
                    if (analysisMode)
                        emit sendPos(this->mapToScene(event->pos()), this->imageTemp.ptr<double>(_posY)[_posX]);/************/
                    else emit sendPos(this->mapToScene(event->pos()), asignaTemperatura(C1*(double)this->imageData.ptr<short>(event->pos().y())[event->pos().x()] + C0, 1, tabla_L_T1));
                else {
                    QPoint badPos(-1,-1);
                    emit sendPos(badPos, -1);
            if (_pan)
                horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - _panStartX));
                verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - _panStartY));
                _panStartX = event->x();
                _panStartY = event->y();

    Thank you very much!

  • Qt Champions 2017

    Let me rephrase your question. You have QGraphicsView & inside the You have image with GraphicsItem. Based on the mouse click you would like to move the image. Since you have enabled mouse tracking, without clicking also image is moving. Is that the correct problem statement ?
    If yes, you would like do something like, without first click image should not move. Is that correct ?

  • @dheerendra Yes, exactly, this is my problem. Thank you for translate my question haha.

Log in to reply