PDA

View Full Version : method creation



MarkoSan
30th May 2007, 07:48
Hi to all!

I am developing an application that will have a lot of menus. Now, I've prepared following code snippet:



m_pDataSourceAction=new QAction(tr("&Data source ..."), this);
Q_CHECK_PTR(m_pDataSourceAction);
m_pDataSourceAction->setShortcut(tr("Ctrl+D"));
m_pDataSourceAction->setStatusTip(tr("Selects data source"));
connect(m_pDataSourceAction, SIGNAL(triggered()), this, SLOT(dataSourceSelection()));


How do I transform this code snipper into an flexible method - method with paramateres ActionText, ShortcutCombo, StatusTipText, SourceSignal, TargetSlot. Now, method should return pointer to QAction, I am aware of that. But how do I declare signal and slot as a parameter of method?

marcel
30th May 2007, 08:13
First of all, I don't think you need to pass the signal. Probably you will always use triggered.

But anyway, you can do this:


QAction* MyClass::createAction( const QString& text, const QString &shortcut, const char * signal, const char* slot, const QString statusTip )
{
QAction* pAction=new QAction(tr( text ), this);
Q_CHECK_PTR(pAction);
pAction->setShortcut(tr( shortcut ));
pAction->setStatusTip(tr( statusTip ));
connect(pAction, signal , this, slot );
return pAction;
}
When you call createAction, for signal you pass something like:
SIGNAL( triggered() );
and for slot:
SLOT( dataSourceSelection() ).



createAction( "Data source", "CTRL+D", SIGNAL( triggered() ), SLOT( dataSourceSelection() ), "status tip..." );
It will work.

By the way, I think you should have posted at least in the Newbie section.

Regards

MarkoSan
30th May 2007, 08:52
Ok, sorry! But i just did not know how to integrate signal and slot as parameters ... :o

marcel
30th May 2007, 08:56
Hey, no problem for me.
I'm sure Wysota will move it :).

MarkoSan
30th May 2007, 09:02
Ok, should I pm him to move it?

wysota
30th May 2007, 09:12
I'm sure Wysota will move it :).
Why me? I'm not the only one who can moderate threads here, you know...


Ok, should I pm him to move it?
No need for that. I just got up and started my cruise around the forum :)

marcel
30th May 2007, 09:47
Why me? I'm not the only one who can moderate threads here, you know...


Well, you and Jacek.
As for the other moderators, I haven't seen them around that much.