Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m 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

1 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...