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. [SOLVED]How to implement the mouse click behaviour using arrow keys
QtWS25 Last Chance

[SOLVED]How to implement the mouse click behaviour using arrow keys

Scheduled Pinned Locked Moved General and Desktop
arrowkeys
11 Posts 2 Posters 4.8k Views
  • 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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #1

    I have a listview where messages are displayed . If i click(using mouse) any of the message , its message property is displayed ( like message name ,type etc..) . I want to implement the same using arrow keys . if i press down arrow then its message property is displayed . How can i do it ?
    currently if i press down arrow key then no changes is seen.
    I have tried using QShortcut

    QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_Down), this);
    QObject::connect(shortcut, SIGNAL(activated(QModelIndex)), ui->listView_Messages, SLOT(listViewClicked(QModelIndex)));
    

    should i try QKeyEvent ?
    QKeyEvent event(QEvent::KeyPress, Qt::Key_Down , Qt::NoModifier);

    --Alles ist gut.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      activated signal is emitted when you, well, activate the item. Selecting it with keyboard arrows does not activate it. For that you would have to hit enter. Note that activate is generally used to open some sort of in-place editor for the item. It's not suited well for what you are doing. You're mixing the QShortcut::activated with QTreeView::activated. The one from shortcut does not have a QModeIndex parameter.

      So not an answer to your question directly, but it sounds like you should be displaying the properties for selected item, not when a mouse is clicked. To do that connect to the selectionChanged signal of the selection model:

      connect(treeView->selectionModel(), &QItemSelectionModel::selectionChanged, someClass, &SomeClass::displayProperties);
      
      //and then:
      void SomeClass::displayProperties(const QItemSelection & selection) {
         if(selection.indexes().isEmpty())
             hidePropPanel();
         else
            showPropPanelForIndex(selection.indexes().front());
      }
      
      1 Reply Last reply
      0
      • RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by Ratzz
        #3

        @Chris-Kawa
        thanks for the reply .
        I feel my question is not clearly written.
        In my listview many messages are displayed (say ,10) when i click on message 1 , its property is displayed in the Groupbox . the listview and groupbox are in the stacked widget .
        I want to implement the same with the arrow keys . When i press the down arrow key it is focused to message2 . so i want its property to be displayed in the group box.
        I have already connected listView to the slot which displays property in groupbox

        connect(ui->listView_Messages,SIGNAL(clicked(QModelIndex)),this,SLOT(listViewClicked(QModelIndex)));

        --Alles ist gut.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          when i click on message 1 , its property is displayed in the Groupbox . the listview and groupbox are in the stacked widget .

          Stacked widget displays one widget at a time, so the listview is hidden when you click an item? It doesn't sound like something an arrow key should do.

          Anyway, to do that you can subclass the treeview and reimplement mousePressEvent (or install event filter on it). There you would check for the arrow key and use the currentIndex from selection model to identify the item.

          1 Reply Last reply
          0
          • RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by
            #5

            @Chris-Kawa
            In one stacked widget listview and groupbox are placed side by side .
            So clicking messages in listview displays its property in groupbox (groupbox has line edit,label ,combobox etc)

            --Alles ist gut.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Ok, so the stacked widget is not important here at all.
              In that case you should go with the selection example I posted in the first response. Just replace the show/hide functions I used to fill in your groupbox.

              1 Reply Last reply
              0
              • RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by Ratzz
                #7

                @Chris-Kawa
                connect(ui->listView_Messages,SIGNAL(clicked(QModelIndex)),this,SLOT(listViewClicked(QModelIndex)));
                on the mouse click on message1 displays the message property.
                I just have to make it using the down/up arrow keys to displays the message property.
                The SomeClass::displayProperties you mentioned just hides and displays ryt??

                --Alles ist gut.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You are focusing on the wrong thing. Arrow key should not "simulate" mouse click. In general there might not be mouse present at all. What you want to do is update the information in the groupbox to reflect the state of currently selected item in the list.

                  You named your slot "listViewClicked", but that is a bad name as it doesn't describe what it does and suggests that it only works with mouse clicks, which is not true. It should be called something like "showItemInfo" or "setDataFromIndex" or something else that says what it does, not what it reacts to. It shouldn't care what triggered it and its name shouldn't suggest that.

                  So i suggest to change your slot name to something better and then use the code I provided. Connect it to selection change, not mouse click, because click is only one of many possible triggers. the selection will change when user clicks an item with mouse, navigates to it with arrows or tab, someone uses tablet stylus or even when you select something manually from code.

                  In my example I used show/hide methods, but you can call them whatever you want and make them do whatever you want, for example instead of hidePropPanel you could have clearGroupbox or displayNone etc. and instead of showPropPanelForIndex you could have showItemInfo, updateGroupbox or whatever you call it (even your listViewClicked, although that is a bad name).

                  1 Reply Last reply
                  0
                  • RatzzR Offline
                    RatzzR Offline
                    Ratzz
                    wrote on last edited by
                    #9

                    @Chris-Kawa
                    are you referring to treeview ? But i am referring to listview.

                    --Alles ist gut.

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      I'm referring to whatever you need :) Selection model works the same in both.

                      1 Reply Last reply
                      0
                      • RatzzR Offline
                        RatzzR Offline
                        Ratzz
                        wrote on last edited by
                        #11

                        @Chris-Kawa
                        Yes , it worked for me :)

                        --Alles ist gut.

                        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