简体   繁体   中英

How to Pass QMainWindow mouse clicked position to QGraphicsView

I have a display window like this:

在此处输入图片说明

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

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

If you only want to change the border color on mouse hover, you wouldn't need such complicated programming. Qt supports style sheets, just like CSS.

In this case, it's enough to attach the following stylesheet to your MainWindow .

QGraphicsView:hover {
    border-style: solid;
    border-width: 2px;
    border-color: green;
}

在此处输入图片说明

There's two ways to get this done:

  • Using the Designer: First select the MainWindow and then in its properties panel click on the styleSheet and copy and paste the style sheet.

  • Using code: Use setStyleSheet(...) method of QMainWindow and pass the style sheet as a string.

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