Search code examples
c++qtmouseeventqgraphicsview

How to Pass QMainWindow mouse clicked position to QGraphicsView


I have a display window like this:

enter image description here

Above display widgets are QGraphicsView widgets (they are in a QGridLayout) and what I want to achieve is that:

when user click in MainWindow, I want to seize that clicked position and decide which QGraphicsView widget contains that position and set the border of that selected QGraphicsView widget to green color. And only one QGraphicView widget can be selected at a time.

Can anyone give me some ideas?

Thanks


Solution

  • You can use installEventFilter for your QGraphicsViews and detect mouse press events on them. So, you can define current view and make border for it as you want. Small example:

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QGraphicsView>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public:
        bool eventFilter(QObject* watched, QEvent* event) override;
    
    
    private:
        Ui::MainWindow *ui;
        QGraphicsView* view1_;
        QGraphicsView* view2_;
        QGraphicsView* selectedView_;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGridLayout>
    #include <QMessageBox>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
            view1_(nullptr),
            view2_(nullptr),
            selectedView_(nullptr)
    {
        ui->setupUi(this);
            QGridLayout* grid = new QGridLayout(this->centralWidget());
            view1_ = new QGraphicsView(this);
            view2_ = new QGraphicsView(this);
            grid->addWidget(view1_, 0, 0);
            grid->addWidget(view2_, 0, 1);
            view1_->viewport()->installEventFilter(this);
            view2_->viewport()->installEventFilter(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    bool MainWindow::eventFilter(QObject* watched, QEvent* event)
    {
        qDebug() << event->type();
        if (event->type() == QEvent::MouseButtonPress)
        {
            if (watched == view1_->viewport()){
                selectedView_ = view1_;
                QMessageBox::information(this, "!", "First");
                return false;
            }
            else if (watched == view2_->viewport()){
                selectedView_ = view2_; 
                QMessageBox::information(this, "!", "Second");
                return false;
            }
        }
        return QMainWindow::eventFilter(watched, event);
    }