[Qt-interest] Dynamic QMenuBar - passing a value to the slot in connect( )?

Robert Wood robert.wood at apostrophe.co.uk
Thu Mar 24 19:03:51 CET 2011


I have successfully created a QMenuBar on the fly according to the 
number of com ports in my /dev directory. I have then managed to have 
each entry in the menu call a routine. However, I cannot work out how to 
pass the string of the selected item to the routine called. The code is 
as below:


// In the routine that detects the VCPs:
// numberOfEntries is how many VCPs in the /dev directory
// results is  a QStringList of the directory entires
QAction *exitAction[numberOfEntries];

for (i=0;i<numberOfEntries;i++)
	{
	exitAction[i] =  ui->menuSelectPort->addAction(results.at(i));
	connect(exitAction[i], SIGNAL(triggered()), this, SLOT(openPort()));
	}


This works in that any item elected in the menuSelectPort of the 
QMenuBar calls the openPort routine.

I have tried various things such as:

connect(exitAction[i], SIGNAL(triggered()), this, 
SLOT(openPort(results.at(i))));

or even:

connect(exitAction[i], SIGNAL(triggered()), this, SLOT(openPort(i)));

(The latter just to try and prove a point.)

And then having openPort setup so it expects a string or a number passed 
to it, but this just doesn't work.

So how can I make it so my dynamically created menu passes its string 
from the menu item selected by the user to the slot it calls? The 
obvious method doesn't work and in fact as soon as I try and pass 
anything at all to openPort() it doesn't call the openPort routine at all!

Many thanks. :~)



More information about the Qt-interest-old mailing list