Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4068534
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:23:13+00:00 2026-05-20T16:23:13+00:00

I need to display MDI windows containing images in my application. I wanted to

  • 0

I need to display MDI windows containing images in my application. I wanted to be able to drag-scroll the images using the right mouse button, zoom them using the mouse wheel, and also create polygon-shaped area-of-interest masks over them. To this end, I created a custom QGraphicsView-derived class (ImageView) which reimplements some mouse events. I also have a QGraphicsPixmapItem-derived class (ImageItem) for implementing hover events which update a cursor pixel-position indicator in the application’s interface. Here’s the outline (doesn’t yet implement the mask polygons):

class ImageView : public QGraphicsView
{
    Q_OBJECT
public:
    ImageView(QWidget *parent) : QGraphicsView(parent), _pan(false), _panStartX(0), _panStartY(0), 
        _scale(1.2), _scene(NULL), _imgItem(NULL)
    {
        _scene = new QGraphicsScene(this); 
        _scene->setBackgroundBrush(Qt::lightGray);
        setScene(_scene);
        _imgItem = new ImageItem(this);
        _scene->addItem(_imgItem);
    }

    void showImage(const QString &path)
    {
        _imgItem->loadImage(path);
        setSceneRect(0, 0, _imgItem->imageWidth(), _imgItem->imageHeight()); 
    }

    void startAoi(AoiType type)
    {
        _imgItem->setAcceptHoverEvents(true);
        _imgItem->setCursor(Qt::CrossCursor);
        // explicit mouse tracking enable isn't necessary
    }

    void stopAoi()
    {
        _imgItem->unsetCursor();
        _imgItem->setAcceptHoverEvents(false);
    }

public slots:
    void zoomIn() { scale(_scale, _scale); }
    void zoomOut() { scale(1/_scale, 1/_scale); }

protected:
    void ImageView::mousePressEvent(QMouseEvent *event)
    {
        // enter pan mode; set flag, change cursor and store start position
        if (event->button() == Qt::RightButton)
        {
            _pan = true;
            _panStartX = event->x();
            _panStartY = event->y();
            setCursor(Qt::OpenHandCursor);
            // accept the event and skip the default implementation?
            event->accept();
            return;
        }

        // should do event accept here?
        event->ignore();
    }

    void ImageView::mouseReleaseEvent(QMouseEvent *event)
    {
        // leave pan mode on right button release; clear flag and restore cursor
        if (_pan && event->button() == Qt::RightButton)
        {
            _pan = false;
            unsetCursor();
            event->accept();
            return;
        }

        // in the future, left clicks will add vertices to a mask polygon
        event->ignore() // ?
    }

    void ImageView::mouseMoveEvent(QMouseEvent *event)
    {
        // pan-mode move; scroll image by appropriate amount
        if (_pan)
        {
            scrollBy(_panStartX - event->x(), _panStartY - event->y());
            _panStartX = event->x();
            _panStartY = event->y();
            event->accept();
            return;
        }

        // generic mouse move, hover events won't occur otherwise.
        QGraphicsView::mouseMoveEvent(event);
        // need to accept or ignore afterwards?
    }

    void ImageView::wheelEvent(QWheelEvent *event)
    {
        // disallow zooming while panning
        if (_pan)
        {
            event->ignore();
            return;
        }

        // handle mouse wheel zoom
        // perform scaling
        if (event->delta() > 0) zoomIn();
        else zoomOut();
        event->accept();
        return;
    }


    bool _pan;
    int _panStartX, _panStartY;
    const qreal _scale;
    QGraphicsScene *_scene;
    ImageItem *_imgItem;
};

class ImageItem : public QGraphicsPixmapItem
{
public:
    ImageItem(ImageView *view) : QGraphicsPixmapItem()
    {
    }

    void loadImage(const QString &path)
    {
        _pixmap.load(path);
        setPixmap(_pixmap);
    }

protected:
    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event)
    {
        // update label with position in GUI here
    }

    QPixmap _pixmap;
};

My first problem is that everything works fine before I activate startAoi() in the view (connected to a button in the GUI which the user presses). Before that, panning works fine, with the mouse cursor changing into an open hand. After I activate the AOI mode, the cursor changes into a cross while over the image, and pressing and dragging the right button pans the view, but doesn’t change the cursor, as if the image item’s cursor takes precedence over the view’s cursor. After I deactivate the AOI mode, the cross cursor disappears, hover events are no longer triggered, but this time while pan still works, I’m stuck with the plain arrow cursor while dragging.

EDIT: Basically, it looks like for a QGraphicsItem, unsetCursor() doesn’t remove the cursor per se, but rather changes it to the standard arrow, which is persistent and overrides the parent view’s cursor.

The other problem is I’m not sure I have the whole mouse processing laid out correctly. For example, I don’t know if I should process the panning and zooming in the mouse events overrides of the view, the scene, or the image item. I figured since because they are global to the whole view (which will contain other objects in the future), this should belong in the top item – the view. Also, I’m not sure when I should call accept() and ignore() on the events, what this actually does, and when I should call the parent classes’ implementations of mouse events. Any insight will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:23:14+00:00Added an answer on May 20, 2026 at 4:23 pm

    I have created a similar image manipulation application some months ago: image drag, zoom-in/out + different custom tools to draw (add) specific items on a QGraphicsView.
    I used only the setCursor() function – not the unsetCursor().

    I created a timer and I set the cursor-shape inside the timer-event function according to a custom “Tool-Type” flag stored in my GraphicsView subclass and taking into account the current state of keyboard & mouse.
    I think this is more convenient as:

    a) It allows you to set the cursor-shape even if the user does not move the mouse or click on a specific object – e.g. if a “zoom tool” is selected you can set the cursor to a (+) [zoom-in cursor]. If the user holds-down the Ctrl-Key you can set the cursor to a (-) [zoom-out cursor]

    b) There is no need to store any cursor states: You just set the correct cursor-shape according to currently selected tool and the mouse/keyboard state.

    c) It’s the easiest way to make your cursor-shape to quickly respond to all keyboard/mouse/tool-change events

    I hope this timer idea can help you to overcome all cursor-shape related problems.
    A timer with a 30 msec interval it will be just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use winforms MDI window and display child windows on it. I need to
In the QT application we develop we need to display several 'Viewer windows' (to
I need display an image on right of some paragraph without using float. So
i am developing one application with map view i need display the weather depends
I need to display image and one button on fancybox popup but I can't
I need to display array of images in a single view which have various
I need to display a custom scrollbar. I would like to avoid using a
Need to display an element ( div ) ontop of webpage. During scroll the
I need to display new tweets from Twitter using Jquery.
I need to display thumbnails of images in a given directory. I use TFileStream

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.