Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
628 views
in Technique[技术] by (31.9m points)

c++ - Why does QGraphicsScene ignore the left mouse button release event if the mousePressEvent method is not reimplemented in the widget?


I need make scene contains another scenes.
Schema: MainWindow->MainView->MainScene->LittleWidget->LittleView->LittleScene
class LittleScene:public QGraphicsScene
{
public:
    LittleScene(QObject* parent=nullptr):QGraphicsScene(parent){};

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override{}

    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
    {
        //just to catch a function call
        auto wi = new QWidget(this);
        wi->show();
    }
};

class LittleWidget:public QWidget
{
    LittleScene *scene;
    QGraphicsView *view;
public:
    LittleWidget(QObject* parent):
        scene(new LittleScene(parent)),
        view(new QGraphicsView(scene))
    {
        auto lay=new QGridLayout(this);
        lay->addWidget(view);
        this->setLayout(lay);
        this->resize(100,50);
    }

//protected:    //#1
//    void mousePressEvent(QMouseEvent *event) override{}
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    auto scene = new QGraphicsScene;
    ui->graphicsView->setScene(scene);
    auto wid = scene->addWidget(new LittleWidget(scene),Qt::Window);
}

If compile this code mouseReleaseEvent() has never been invoked for left button (with others there is no any problems).

If uncomment #1 - works perfectly.

I've tried with no parent-child relationship - the same behavior.

What a hell? (Maybe it's already fixed bug - I'm using 5.10.1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
...