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 The problem is how to callback because I do not know the number of the But the problem is that I can not connect the parameters to the What is another strategy? then And 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. 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: You connect each signal in the Signal Map () slot: Then you can tell the signer mapper what the sender has the index: Then you connect the outbound map (int) signal to your slot QAction * variable and add them to
QMenu . Therefore, the number of objects is dynamic and is not known at the time of compilation.
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)
menuItemClicked (int) slot.
Update - it has not been found yet
class MainWindow {Private slots: void help document (int);
zero main menu: :: helpDocumentation {// some logging}
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)); }
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}
QSignalMapper * signalMapper = new QSignalMapper (this);
connect (action, signal (trigger), signalmap, slot ( Map ())); Connect (second action, signal (trigger), signalmapper, slot (map)); Connect (yet another action, signal (trigger)), signalmapper, slot (map));
signalMapper-> setmapping (action, 0); SignalMapper- & gt; Setmapping (second action, 1); SignalMapper- & gt; Setmapping (yet another action, 2);
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
Post a Comment