Application menu with many rows and columns.
I have strange problem. I'm trying to implement menu (of buttons) with 4 rows and 4 columns, which looks basically like this:
| 1 2 3 4 |
| 5 6 7 8 |
| 9 A B C |
| D E F 0 |
I leyd (sorry, I forgot past form of lay...) down buttons with QGridLayout, but it doesn't work in desired way. Problem is that, when I have focus on object no. 7 and I press "down" I wold like to jump to row below.
I know that I can place those buttons in somekind of 2d table and write my own focus-movement logic, but maybe someone knows some other solution.
AFAIK, a QGridLayout does not implement focus movement using the arrow keys for you, so you'll have to do it yourself. Should not be too difficult, though. You can create your own QGridLayout subclass, do it in the QWidget subclass that uses your grid layout, or use an event filter.
I think I would use this approach:
Create a QObject-derived class that takes in it's constructor a QGridLayout pointer (and the usual QObject parent pointer). In the constructor:
- iterate over the layout to get the widgets
- install an eventfilter on all of these widgets
- reimplement the eventFilter method, and respond to the events for the arrow keys by shifting focus
- use QLayout::indexOf(), QGridLayout::getItemPosition and QGridLayout::itemAtPosition to find out
where the current item is located, and where to move next.
This way, you can add this functionality to any grid layout by just instantiating an instance of this self-contained class.
Thanks gentelmen for Your answers. I will use one of your suggestions to solve this problem. I also will try to post some code here, that others won't have to ask again.