[SOLVED]How to implement the mouse click behaviour using arrow keys
-
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 QShortcutQShortcut *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); -
You're mixing theactivated
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 thatactivate
is generally used to open some sort of in-place editor for the item. It's not suited well for what you are doing.QShortcut::activated
withQTreeView::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()); }
-
@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 groupboxconnect(ui->listView_Messages,SIGNAL(clicked(QModelIndex)),this,SLOT(listViewClicked(QModelIndex)));
-
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.
-
@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) -
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. -
@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?? -
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 haveclearGroupbox
ordisplayNone
etc. and instead ofshowPropPanelForIndex
you could haveshowItemInfo
,updateGroupbox
or whatever you call it (even yourlistViewClicked
, although that is a bad name). -
@Chris-Kawa
are you referring to treeview ? But i am referring to listview. -
I'm referring to whatever you need :) Selection model works the same in both.
-
@Chris-Kawa
Yes , it worked for me :)