Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detect if mouse is pressed on scene
Qt 6.11 is out! See what's new in the release blog

Detect if mouse is pressed on scene

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chrisLucr
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Astrinus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        chrisLucr
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Astrinus
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          1
          • C Offline
            C Offline
            chrisLucr
            wrote on last edited by
            #5
            • 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 .

            SGaistS 1 Reply Last reply
            0
            • C chrisLucr
              • 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 .

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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).

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • C Offline
                C Offline
                chrisLucr
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved