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. Qtreeview MouseEvent
Forum Updated to NodeBB v4.3 + New Features

Qtreeview MouseEvent

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 2 Posters 9.3k Views 2 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.
  • S Offline
    S Offline
    Shiv
    wrote on last edited by Shiv
    #1

    Dear All
    I have implemented a QTreeview with QAbstractItemModel now I want to emit a signal whenever mouse is clicked with LeftButton on the Items of the QTreeview.I have tried with 'clicked' and 'selectionChanged' signal but the problem is it responds for RightButton too since I have to use a context menu for RightClick . Is there any way to emit a signal from QTreeview other than QEvent eventfilter or QMouseEvent.
    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      Im not 100% sure what is not working except right click should not
      give "selectionChanged" for your use case ?

      If you already have a QTreeview child with a
      mousePressEvent(QMouseEvent *event)
      implemented you could just make your own signal?

      treeview..
      public signals:
      void LeftClicked( any param u want );

      then in mousePressEvent
      if (event->button() == Qt::LeftButton) {
      emit LeftClicked( the actual paramters);
      }

      Then from outside u just use your new signal.

      Note: for a signal, you do not implement anything in the cpp. its
      just a definition in the class. No body.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shiv
        wrote on last edited by
        #3

        Hi @mrjj Thanks for your kind reply
        Implementing through QMouseEvent is not an issue . I thought if any signal like 'clicked' is already available I can start using it directly.

        Thank you

        mrjjM 1 Reply Last reply
        1
        • S Shiv

          Hi @mrjj Thanks for your kind reply
          Implementing through QMouseEvent is not an issue . I thought if any signal like 'clicked' is already available I can start using it directly.

          Thank you

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Shiv
          well yes, you could also emit clicked.

          do you use
          http://doc.qt.io/qt-5/qcontextmenuevent.html#details

          to make the context menu ?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shiv
            wrote on last edited by
            #5

            @mrjj
            No I am not using QContextMenuEvent just implementing with ActionsContextMenu policy and adding actions to the treeview.In my case if I use 'clicked' signal implementing context menu is difficult .

            1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Ok.
              But catching MousePress and check
              if left button then emit
              new signal for left selection
              and if not left button,
              then simply call base class MousePress
              Is not acceptable for you?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Shiv
                wrote on last edited by
                #7

                @mrjj
                I will try and let you know

                Thanks

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Shiv
                  wrote on last edited by
                  #8

                  Hi @mrjj
                  I have tried to implement mouse press event with event filters using below code

                  ui.usertreeView->installEventFilter( this );  //event filter for qtreeview
                  
                  //event handler
                  bool MainWindow::eventFilter( QObject * watched, QEvent * event )
                  {
                     QTreeView *view = qobject_cast<QTreeView *>(watched);
                  
                     QMouseEvent *k = (QMouseEvent *)event;
                     
                     if(watched==view)
                     {
                     	if((k->button())== Qt::LeftButton)
                     	{
                     		qDebug()<<"treeview event";
                     	}
                     }
                  
                     return QObject::eventFilter(watched, event);
                  }
                  

                  but no event is getting generated when I click on the treeview.

                  If the same code is used to capture for a QLabel the event is getting generated.What may be the reason?

                  Thanks

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    No, that sounds odd. Should also work for
                    treeview.
                    Is there a reason to use a eventfilter versus
                    just implement the MousePress function ?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Shiv
                      wrote on last edited by
                      #10

                      Hi @mrjj

                      Its not working for treeview .There are lot of widgets in my application to segregate the mouse press event i have used event filter.But i am unable to understand why the event filter is not working for treeview if possible can you check at your end .

                      Thanks

                      mrjjM 1 Reply Last reply
                      0
                      • S Shiv

                        Hi @mrjj

                        Its not working for treeview .There are lot of widgets in my application to segregate the mouse press event i have used event filter.But i am unable to understand why the event filter is not working for treeview if possible can you check at your end .

                        Thanks

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Shiv
                        Hi
                        Why not just override
                        mousePressEvent(QMouseEvent *event)
                        for treeview?
                        I dont understand why you try with eventfilter even i completely agree
                        all events should also be seen there.
                        I will try eventfilter on tree and see if I get a hint.

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          hi
                          The Tree have a viewport that get these events
                          so
                          ui->listView->viewport()->installEventFilter(this);

                          and

                          bool MainWindow::eventFilter( QObject* watched, QEvent* event ) {
                            qDebug() << "treeview event !1";
                            QTreeView* view = qobject_cast<QTreeView*>(watched);
                          
                            QMouseEvent* k = static_cast<QMouseEvent*>(event);
                          
                            if(k) {
                              if((k->button()) == Qt::LeftButton) {
                                qDebug() << "treeview button down 3 ";
                              }
                            }
                          

                          worked for me.

                          return QObject::eventFilter(watched, event);
                          }

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Shiv
                            wrote on last edited by
                            #13

                            Hi @mjrr
                            Thanks for your valuable time and code.
                            Its working fine with this code.The event is working fine in QT5.4 but I am currently doing this project in Qt 4.8.4 with VS2008 there some spurious events are getting generated without pressing the mouse.I am unable find the cause for this peculiar behavior. Any solution/idea will be helpful for me to move forward.

                            Thanks

                            mrjjM 1 Reply Last reply
                            0
                            • S Shiv

                              Hi @mjrr
                              Thanks for your valuable time and code.
                              Its working fine with this code.The event is working fine in QT5.4 but I am currently doing this project in Qt 4.8.4 with VS2008 there some spurious events are getting generated without pressing the mouse.I am unable find the cause for this peculiar behavior. Any solution/idea will be helpful for me to move forward.

                              Thanks

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Shiv
                              Hi, ok that is good to hear.
                              Very hard to guess at when its other Qt and compiler :)

                              You can use my event debug function
                              http://paste.ofcode.org/xKREBjyuFwX6FEzTr4rRAP

                              and call it with
                              qDebug() << "EVENT: " << event->type() << " = " << EventDesc(event->type() );
                              in the event filter to see if u can guess what is happening.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                Shiv
                                wrote on last edited by
                                #15

                                @mjrr
                                I will try and let you know.
                                Thanks

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Shiv
                                  wrote on last edited by
                                  #16

                                  Hi @mjrr
                                  I have tried with your EventDesc function the mouse press event automatically getting generating and it happens randomly I know its difficult to replicate this issue at your end i am posting some of the debug message kindly have look.The BTN variable is the mouseevent k->button() value

                                  //BTN k->button() value
                                  EVENT:  127  =   mouse cursor enters a hover widget 
                                  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  2 
                                  EVENT:  12  =   paint widget 
                                  BTN  2347008 
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  2 
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  //BTN  1  // mouse event without mouse action
                                  //treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  //BTN  1 
                                  //treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  //BTN  1 
                                  //treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  1 
                                  treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  //BTN  1 
                                  //treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  //BTN  1 
                                  //treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  1 
                                  treeview button down 3  
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  2 
                                  EVENT:  110  =  Tooltip 
                                  BTN  0 
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  1 
                                  treeview button down 3  
                                  EVENT:  110  =  Tooltip 
                                  BTN  0 
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  BTN  0 
                                  EVENT:  129  =   mouse cursor move inside a hover widget 
                                  

                                  Thank you

                                  1 Reply Last reply
                                  0
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #17

                                    hi
                                    what about

                                    
                                    bool MainWindow::eventFilter( QObject* watched, QEvent* event ) {
                                      qDebug() << "treeview event !1";
                                      QTreeView* view = qobject_cast<QTreeView*>(watched);
                                    
                                      QMouseEvent* k = static_cast<QMouseEvent*>(event);
                                    
                                      if(k && k->type() == QEvent::MouseButtonPress) {
                                        if((k->button()) == Qt::LeftButton) {
                                          qDebug() << "treeview button down 3 ";
                                        }
                                      }
                                      return QObject::eventFilter(watched, event);
                                    }
                                    
                                    
                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      Shiv
                                      wrote on last edited by
                                      #18

                                      Hi
                                      Great its working thanks a lot..
                                      Thank you

                                      mrjjM 1 Reply Last reply
                                      0
                                      • S Shiv

                                        Hi
                                        Great its working thanks a lot..
                                        Thank you

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        super :)
                                        Error was that we use
                                        <QMouseEvent*>
                                        and Hover is also such type , i think.
                                        No idea why k->button() seems to be set for those but
                                        with extra check it works then :)

                                        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