Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Nobody knows why??? plz help me! a weird problem about mosueEvent__Is this a bug???



  • what I want is very very simple:
    put a QGraphicsView on the MainWindow, when I click it, a QDialog will come out. That's all.

    for this goal, I create a class QgraphicViewDessin who inherits the class QGraphicsView and I rewrite the fonction: mousePressEvent, which will emit a signal.

    in the MainWindow, first I put a QgraphicsView in ui and promote it into QGraphicsViewDessin.
    then, I write a slot and make a connect in the class MainWindow.

    here is all of my codes:
    @
    //GraphicsViewDessin

    class GraphicsViewDessin : public QGraphicsView
    {
    Q_OBJECT

    public:
    GraphicsViewDessin(QWidget *parent = 0);

    protected:
    void mousePressEvent(QMouseEvent *);

    signals:
    void dessin_by_click();
    };
    @

    @
    //cpp
    #include "graphicsviewdessin.h"

    GraphicsViewDessin::GraphicsViewDessin(QWidget *parent) : QGraphicsView(parent)
    {
    }

    void GraphicsViewDessin::mousePressEvent(QMouseEvent *event)
    {
    if(event->button() == Qt::LeftButton)
    {
    emit dessin_by_click();
    }
    QGraphicsView::mousePressEvent(event);
    }
    @

    @
    //mainwindow
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public slots:
    void on_graphicsView_extra_clicked();

    private:
    Ui::MainWindow *ui;
    };
    @

    @
    //cpp
    //the name of the graphicsview on ui is graphicsView_extra
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDialog>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    connect(ui->graphicsView_extra, SIGNAL(dessin_by_click()), this, SLOT(on_graphicsView_extra_clicked()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_graphicsView_extra_clicked()
    {
    QDialog d(this);
    d.exec();
    }
    @

    that's all. u see, it is very very simple, by the way, dont forget to add a graphicsview and promote it on ui.

    now, the very weird problem is:
    when I click on the graphicsview, a dialog comes out, this is correct.
    then I click the cross button to close the dialog. this is correct.
    BUT, now, when I click on the mainwindow ----- no matter where, a dialog will come out AGAIN!!!
    BUT, if we click on the dialog ---- no matter where before it is closed, this problem will be GONE!!!

    for this very very weird problem, I have asked so many people even my professor, NOBODY knows why......

    my version of Qt is 5.0.2.

    up to now I have not done a test with other versions.

    trust me, u can do a test, u will get the same result....(ofc i m not sure for the other versions)

    plz help me!
    this problem has tortured me for three days, plz help me!

    I ve never seen such a problem.

    thank you very much in advance.

    [code tags added, koahnig]



  • welcome to devnet

    Please checkout the forum rules for "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01
    I have added them this time



  • not sure but check this
    @
    void GraphicsViewDessin::mousePressEvent(QMouseEvent *event)
    {
    if(event->button() == Qt::LeftButton)
    {
    emit dessin_by_click();
    }
    QGraphicsView::mousePressEvent(event);
    }
    @
    and this http://qt-project.org/doc/qt-5/qwidget.html#mousePressEvent
    ( QGraphicsView::mousePressEvent(QMouseEvent * event) is just reimplemented QWidget::mousePressEvent(QMouseEvent * event))



  • thanks very much.

    i have seen this from your url:
    If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

    if I am right, this is how my problem happened.

    it is still so so weird for me...

    but at least i got an explanation.

    thanks.

    [quote author="SetBetterPass" date="1389007545"]not sure but check this
    @
    void GraphicsViewDessin::mousePressEvent(QMouseEvent *event)
    {
    if(event->button() == Qt::LeftButton)
    {
    emit dessin_by_click();
    }
    QGraphicsView::mousePressEvent(event);
    }
    @
    and this http://qt-project.org/doc/qt-5/qwidget.html#mousePressEvent
    ( QGraphicsView::mousePressEvent(QMouseEvent * event) is just reimplemented QWidget::mousePressEvent(QMouseEvent * event))[/quote]



  • Hi,

    I checked your code, exactly, and I have not noticed any misbehaviour ... I use Qt 5.2.0 on Win 7 ... imho mouseReleaseEvent() default implementation cannot be an issue



  • thx man

    check out the third reply, that's I copied some explanation from that url.
    I think that is why I met this problem.
    anyway, there is no other reasons i think.

    in fact, my classmate and my prof copied my code and did a test, and they got the same problem. we are all qt 5.0.2 and win7......

    and also, a friend from another forum did a test too. and he didnt get this problem, just like u.

    so this is still weird....

    thx man.
    i think the problem comes from the system or sth else. it should not be my mistake anyway.

    [quote author="NicuPopescu" date="1389017412"]Hi,

    I checked your code, exactly, and I have not noticed any misbehaviour ... I use Qt 5.2.0 on Win 7 ... imho mouseReleaseEvent() default implementation cannot be an issue[/quote]


  • Moderators

    This is just a long shot, but here's my thought.

    You make a connect with default DirectConnection type.
    Then you emit signal before calling the default implementation of the mouse click. This means that a dialog is shown via exec() before the click is properly handled on the graphics view.

    Don't know the Qt internals that well but I would bet that this can mess up some internal state of mouse handling.

    Try to call the base implementation first and then emit the signal or change the connection type to QueuedConnection and see if that helps.



  • thx for ur adivce.

    i understand what u are saying.
    I ve tryed but the problem is still there.

    u see, for some friends, they didnt meet this problem.
    so i believe that this problem comes from the system.

    [quote author="Chris Kawa" date="1389114239"]This is just a long shot, but here's my thought.

    You make a connect with default DirectConnection type.
    Then you emit signal before calling the default implementation of the mouse click. This means that a dialog is shown via exec() before the click is properly handled on the graphics view.

    Don't know the Qt internals that well but I would bet that this can mess up some internal state of mouse handling.

    Try to call the base implementation first and then emit the signal or change the connection type to QueuedConnection and see if that helps.[/quote]


Log in to reply