Search code examples
c++qtvtkdicom

How to make perform many functions in mousepressevent


I want to make some functions on a dicom serie (with qt and vtk) , and I want to make some connections between the qt window and the mouse.

This is my primary design: this is my primary design

For example, if I click on zoombutton, then I click on my image with the left button of the mouse, I want that the image will be zoomed, I know that we must use the function mousePressEvent but I have seen that we must use this name for any connection with the mouse, or I want to do 4 or 5 functions like this one, each one for one pushbutton. How can I do this ?


Solution

  • As you suggested correctly, you should use mousePressEvent to capture a mouse press action. To perform the correct action on a mouse press (zoom, pan, ...), you should remember the last pressed button and call the appropriate method accordingly. This can be implemented as follows:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow ()
        {
            connect(ui->panButton, &QPushButton::clicked, this, &MainWindow::onPan)
            connect(ui->zoomButton, &QPushButton::clicked, this, &MainWindow::onZoom)
            ...
        }
    
    protected slots:
        enum Action {None, Pan, Zoom, ...};
        void onPan () {currentAction = Pan;}
        void onZoom () {currentAction = Zoom;}
    
    protected:
        void mousePressEvent(QMouseEvent *event)
        {
            switch(currentAction)
            {
            case Pan:
                // perform Pan operation
                break;
            case Zoom:
                // perform Zoom operation
                break;
            }
        }
    
    protected:
        Action currentAction;
    };