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

Detect if mouse is pressed on scene



  • Hello,
    i want to call a function when a scene is clicked twice, but only when an item is clicked twice not the whole scene this is what i have tried :

    void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
    {
        if ( event->button() == Qt::LeftButton )
        {
            if(zoneTitre1->isVisible()){
                changeText();
            }
        }
    }
    

    the function changeText is working fine and it s being called every time i press the mouse twice in the whole window but i need it to be in only one item of the scene .
    I know it may be an easy one but i am totally new to qt creator
    Thanks



  • If you want to follow this route, you should reimplement mouseDoubleClickEvent in the widget you want to be double clicked, rather than the whole MainWindow (I suppose it is a subclass of QMainWindow).
    You can then emit a custom signal doubleClicked that, in MainWindow constructor, you connect to your changeText slot.
    If you want more specific help, you should post the relevant part of the code, i.e. the full definition of the class (minus the irrelevant methods) and implementations of constructor and relevant methods.



  • Thanks for the reply @Astrinus . well i have tried to do what you have told me but it did not work ( may be what i have done is incorrect ) so the important parts of my code which i believe have relation with this are :

    • the item on the scene i am trying to change it when i click on it has his own class , it is kind of long to post it all here ( the class is called TitreClasse ) so what i have done is adding this code to it :
    void TitreClasse::mouseDoubleClickEvent(QMouseEvent *event)
    {
        if ( event->button() == Qt::LeftButton )
        {
                setPlainText("SOME TEXT"); // setPlainText is a function to change text
        }
    }
    

    i am not sure which code to post specifically. Thanks for the help



  • Let's see if I have understood your problem correctly:

    • You have a TitreClasse that is a (possibly indirectly) subclass of QWidget (is it a QPlainTextEdit subclass?)
    • You want that, on double click on the TitreClasse widget, its text changes

    The code you posted (except for not calling the base class implementation, a thing you should do) seems fine. Maybe the culprit is in another part of the code that you have not posted?



    • it is a QGraphicsTextItem subclass.
    • Yes on double clicking this item i want it to change text or call the slot "changeText()".

    What i have done is :

    • in TitreClasse.h : void mouseDoubleClickEvent(QMouseEvent *);
    • in TitreClasse.cpp : the code above

    And that s all . so basically i don t think any part of the code may interfer in doing the double clicking thing .


  • Lifetime Qt Champion

    Hi
    @chrisLucr said in Detect if mouse is pressed on scene:

    What i have done is :

    • in TitreClasse.h : void mouseDoubleClickEvent(QMouseEvent *);

    The method signature is wrong, see here.

    To avoid that kind of error, you should add the override keyword at the end of the method declaration.

    Don't forget to call the base class implementation otherwise you will lose what QGraphicsTextItem does (unless that's what you want).



  • Thanks @SGaist / @Astrinus works perfectly rn.
    Marked as Solved
    Thanks again


Log in to reply