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 focus issue
Qt 6.11 is out! See what's new in the release blog

QTreeView focus issue

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 5.7k 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.
  • c.savurC Offline
    c.savurC Offline
    c.savur
    wrote on last edited by
    #1

    Hello,

    I want to use QTreeView keyboard binding functionality. For example, hold shift and press up/down to select multiple items in the tree.

    To able use this functionality. I have to press tab to set the focus for QTreeView. It is annoying. I want to press shift+up/down, after I clicked with the mouse. Each time, I don't want to press tab.

    How can I do this? I tried to use setFocus() but it did not work.

    Thank you.

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

      Hi,

      Did you already setup the selection mode on your QTreeView ?

      How are you setting up that QTreeView ?

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

      c.savurC 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Did you already setup the selection mode on your QTreeView ?

        How are you setting up that QTreeView ?

        c.savurC Offline
        c.savurC Offline
        c.savur
        wrote on last edited by
        #3

        @SGaist

        yes, I set the tree to

            setSelectionBehavior(QAbstractItemView::SelectRows);
            setSelectionMode(QAbstractItemView::ExtendedSelection);
        

        I inherited QTreeView class, so do you think this could be a reason?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you reimplement anything ? How do you initialize it ?

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

          c.savurC 1 Reply Last reply
          0
          • SGaistS SGaist

            Did you reimplement anything ? How do you initialize it ?

            c.savurC Offline
            c.savurC Offline
            c.savur
            wrote on last edited by
            #5

            @SGaist

            Following virtual method were reimplemented.

                void keyPressEvent(QKeyEvent *event);
                void mousePressEvent(QMouseEvent *event);
                void dragMoveEvent(QDragMoveEvent *event);
                void dropEvent(QDropEvent *event);
                void contextMenuEvent(QContextMenuEvent *event);
            

            the way, I initialize it is I added a QTreeView to UI file and promote to my new derived class.

            Thank you for your help.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              mousePressEvent ? Do you call the base class implementation in your code ?

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

              c.savurC 1 Reply Last reply
              0
              • SGaistS SGaist

                mousePressEvent ? Do you call the base class implementation in your code ?

                c.savurC Offline
                c.savurC Offline
                c.savur
                wrote on last edited by
                #7

                @SGaist

                Yes, I did.

                QTreeView::mousePressEvent(event);
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Also in the other reimplemented functions ?

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

                  c.savurC 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Also in the other reimplemented functions ?

                    c.savurC Offline
                    c.savurC Offline
                    c.savur
                    wrote on last edited by
                    #9

                    @SGaist

                    Yes, except

                    void dropEvent(QDropEvent *event);
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Can you show your implementation of mousePressEvent ?

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

                      c.savurC 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Can you show your implementation of mousePressEvent ?

                        c.savurC Offline
                        c.savurC Offline
                        c.savur
                        wrote on last edited by
                        #11

                        @SGaist

                        here is the code, I deleted some part of the code.

                        void TreeView::mousePressEvent(QMouseEvent *event)
                        {
                            //![1] Control
                            if(event->modifiers() == Qt::ControlModifier) {
                                QModelIndex index = indexAt(event->pos());
                                if(index.isValid()) {
                                    index = model()->index(index.row(), 0, index.parent());
                                    QString name = index.data(Qt::UserRole).toString();
                        
                                    if(name == "xxx") {
                                        event->ignore();
                                        return;
                                    }
                        
                                    if(name == "yyy") {
                                        event->ignore();
                                        return;
                                    }
                                }
                            }
                            //![1]
                        
                            QTreeView::mousePressEvent(event);
                        }
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          And the keyPressEvent ?

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

                          c.savurC 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            And the keyPressEvent ?

                            c.savurC Offline
                            c.savurC Offline
                            c.savur
                            wrote on last edited by
                            #13

                            @SGaist

                            Sory, I was not able to access my computer. Here is the keypressevent

                            void TreeView::keyPressEvent(QKeyEvent *event)
                            {
                                if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_C) {
                                    copyAction();
                                } else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_V) {
                                    pasteAction();
                                }
                            
                                QTreeView::keyPressEvent(event);
                            }
                            
                            1 Reply Last reply
                            0
                            • c.savurC Offline
                              c.savurC Offline
                              c.savur
                              wrote on last edited by
                              #14

                              Hello,

                              I have found my mistake, during mouse pressed event I was calling some method that changes the focus some other widget. That's why It was losing focus.

                              Solution.

                              I reimplement, mouse release event and I set the focus to the QTreeView again.

                              Thank you for your help!

                              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