PDA

View Full Version : qmenu and dynamic slot



mero
13th March 2010, 02:41
Hello,
How can I modify this code that will work ? Because line 17 is not working..



Object::connect: No such slot qnicklist::invite(strInviteChannel) in qnicklist.cpp:146




...
void qnicklist::invite(QString strInviteChannel)
{
QString strNick = this->selectedItems().at(0)->text();
qnicklist::send(QString("INVITE %1 %2").arg(strInviteChannel).arg(strNick));
}
....

void qnicklist::contextMenuEvent(QContextMenuEvent *e)
{
....
QMenu *minvite = new QMenu("Invite");
for (int i = 0; i < strOpenChannels.count(); i++)
{
QString strInviteChannel = strOpenChannels.at(i);
if (strInviteChannel[0] == '#')
minvite->addAction(strInviteChannel, this, SLOT(invite(strInviteChannel)));
}
....
}

Lykurg
13th March 2010, 07:46
Make sure you are using Q_OBJECT in your header file and then the correct syntax for slots is the signature of the slot:
minvite->addAction(strInviteChannel, this, SLOT(invite(QString)));

But then I guess you cant achieve what you want, so have a look at QSignalMapper or use sender() in your slot.

mero
14th March 2010, 03:11
Make sure you are using Q_OBJECT in your header file and then the correct syntax for slots is the signature of the slot:
minvite->addAction(strInviteChannel, this, SLOT(invite(QString)));

But then I guess you cant achieve what you want, so have a look at QSignalMapper or use sender() in your slot.



...
void qnicklist::invite()
{
QString strInviteChannel = (QString)(QObject::sender()->objectName());
QString strNick = this->selectedItems().at(0)->text();
qnicklist::send(QString("INVITE %1 %2").arg(strInviteChannel).arg(strNick));
}
....

void qnicklist::contextMenuEvent(QContextMenuEvent *e)
{
....
QMenu *minvite = new QMenu("Invite");
for (int i = 0; i < strOpenChannels.count(); i++)
{
QString strInviteChannel = strOpenChannels.at(i);
if (strInviteChannel[0] == '#')
minvite->addAction(strInviteChannel, this, SLOT(invite()));
}
....
}

sender()->objectName() return null ..
and sender() cannot cast to string...

So how to do it by sender ? example ?

Lykurg
14th March 2010, 07:31
Well QSignalMapper would be the better way and of course sender (which returns a QObject) can not converted to a string. How could it. And how should the program know which string you mean?????

So the idea was to put user data to your actions and then cast the sender() to an action and read the data back.

mero
14th March 2010, 14:51
I found solution at: http://doc.trolltech.com/4.6/mainwindows-recentfiles.html
;)