qt - Dynamically generated menu items -- figuring out which one was clicked -


I am parsing an XML document that contains a list of menu bar items. In other words, if I have 5 elements in the XML tree then I will create 5 QAction * variable and add them to QMenu . Therefore, the number of objects is dynamic and is not known at the time of compilation.

The problem is how to callback because I do not know the number of the QAction * variable, so I can not create a callback for each one:

 < Code> connect (action, signal (clicked), this, slot (item1Clicked ())); Connect (second action, signal (clicked)), this slot (item2licked ()); Connect (yet another action, signal (clicked)), this slot (item 3 (amazed)); I thought about creating a callback:  
  zero menu item (int index)   

But the problem is that I can not connect the parameters to the menuItemClicked (int) slot.

What is another strategy?

Update - it has not been found yet
  class MainWindow {Private slots: void help document (int);   

then

  zero main menu: :: helpDocumentation {// some logging}   

And

  zero main window :: main wando () {QDomElement rootElement = doc.documentElement (); QDomNodeList nodes = doc.elementsByTagName ("document"); QMenu * documentationMenu = helpMenu-> addMenu ("documentation"); QSignalMapper * signalMapper = new QSignalMapper (this); For (int i = 0; i & lt; nodes.length (); ++ i) {QDomNode node = nodes.item (i); QDomElement element = node. Element (); If (Element.isNull ()) {// item is an element QString fileAttr = element.attribute ("file"); Caststring name is Atrr = Element. Attribute ("name"); Documentation. Push_back (qMakePair (file unbreakable, named ett)); QECTION * ACTION = New QECTION (Name Entry, Help Menu); Connect (action, signal (clicked), signalmapper, slot (map)); SignalMapper- & gt; Setmapping (Action, I); DocumentationMenu- & gt; AddAction (action); }} Connect (signalmapper, signal (mapped), this, slot (helpdesign)); }   

The item is added to the menu bar but the helpDocumentation (int) slot is not called. Use the sender () function just inside the slot, it will tell you which keynation is indicated by

Is triggered.

  QObject * s = sender (); If (s == action) {// something else here} if (s == another action) {// some here some more} and if (s == yet other action) {// do something here}   

If you want exactly one index parameter, then look for QSignalMapper. It does exactly what you want: By adding multiple parameter signals in a single slot, adding a parameter as integer adds a unique integer index for each connected signal.

You create the first signal mapper:

  QSignalMapper * signalMapper = new QSignalMapper (this);   

You connect each signal in the Signal Map () slot:

  connect (action, signal (trigger), signalmap, slot ( Map ())); Connect (second action, signal (trigger), signalmapper, slot (map)); Connect (yet another action, signal (trigger)), signalmapper, slot (map));   

Then you can tell the signer mapper what the sender has the index:

  signalMapper-> setmapping (action, 0); SignalMapper- & gt; Setmapping (second action, 1); SignalMapper- & gt; Setmapping (yet another action, 2);   

Then you connect the outbound map (int) signal to your slot

  connect (signalmapper, signal (map), this, Slot (menuItemClicked (int)); By the way, SignalPaper is a very simple class that uses the sender () and the index looks in a hash table, adding the QObject * senders to the integer.   

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -