1

I'm a bit stucked with creating drop-down button based on QAction placed in QToolBar
I have an XML file with following data:

<cfg>
<fields>
  <group name="First fields">
    <field>filed1</field>
    <field>filed2</field>
    <field>filed3</field>
  </group>
  <group name="Second fields">
    <field>filed4</field>
    <field>filed5</field>
    <field>filed6</field>
      ... etc ...
  </group>
</fields>
<button name="MyButton1" />
<button name="MyButton2 />
      ... etc ...
</cfg>  

NB: I don't know how much field groups will be, as well as I don't know how much button will be.

So, first of all, I parse following xml-file and extract needed data.

For each button I create its own QAction and add it to the existing toolbar.
Later, I create QMenu for each button and fill it with QActions for each group and field. I have QAction for First fields, field1, field2, etc...
Then, for each button I use setMenu method and add created menus there.
If I launch my app I can see my buttons in toolbar with drop-down menus and they looks like I expected.

The problem here is that I need to add some functionality to this buttons.

I'd like to allow user to check them (I use setCheckable for all QActions) and uncheck. When user checks actions with group name all fields related to this group become checked and so on.

As I don't know how much buttons and fields I will have on startup it become difficult to work with signals and slots.
It forces me to use QMap to store all addresses of created QAction, when the signals from this fields being emited, then using find method to look for actions that emited it and only them perform needed action.

The question is: is there a better way to achieve my goal or I should continue using this approach?

potashin
  • 44,205
  • 11
  • 83
  • 107
tema
  • 1,115
  • 14
  • 33

1 Answers1

2

If you don't know how many items are going to be created, I advice you use a QSignalMapper (documentation here).

First you reference each item with a variable in the QSignalMapper, then you connect the signal you want (coming from your item) to the signal mapper. Finally you connect the QSignalMapper to a single slot, which will receive the reference variable. Several signal mappers can be used if you need to connect different kinds of signals.

You will still certainly have to store each item somewhere, but I think this is the way to go.

Davy
  • 429
  • 2
  • 17