简体   繁体   中英

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: 这是我的主要设计

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 ?

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM