Results 1 to 5 of 5

Thread: qmenu and dynamic slot

  1. #1
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qmenu and dynamic slot

    Hello,
    How can I modify this code that will work ? Because line 17 is not working..

    Qt Code:
    1. Object::connect: No such slot qnicklist::invite(strInviteChannel) in qnicklist.cpp:146
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ...
    2. void qnicklist::invite(QString strInviteChannel)
    3. {
    4. QString strNick = this->selectedItems().at(0)->text();
    5. qnicklist::send(QString("INVITE %1 %2").arg(strInviteChannel).arg(strNick));
    6. }
    7. ....
    8.  
    9. void qnicklist::contextMenuEvent(QContextMenuEvent *e)
    10. {
    11. ....
    12. QMenu *minvite = new QMenu("Invite");
    13. for (int i = 0; i < strOpenChannels.count(); i++)
    14. {
    15. QString strInviteChannel = strOpenChannels.at(i);
    16. if (strInviteChannel[0] == '#')
    17. minvite->addAction(strInviteChannel, this, SLOT(invite(strInviteChannel)));
    18. }
    19. ....
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: qmenu and dynamic slot

    Make sure you are using Q_OBJECT in your header file and then the correct syntax for slots is the signature of the slot:
    Qt Code:
    1. minvite->addAction(strInviteChannel, this, SLOT(invite(QString)));
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qmenu and dynamic slot

    Quote Originally Posted by Lykurg View Post
    Make sure you are using Q_OBJECT in your header file and then the correct syntax for slots is the signature of the slot:
    Qt Code:
    1. minvite->addAction(strInviteChannel, this, SLOT(invite(QString)));
    To copy to clipboard, switch view to plain text mode 

    But then I guess you cant achieve what you want, so have a look at QSignalMapper or use sender() in your slot.
    Qt Code:
    1. ...
    2. void qnicklist::invite()
    3. {
    4. QString strInviteChannel = (QString)(QObject::sender()->objectName());
    5. QString strNick = this->selectedItems().at(0)->text();
    6. qnicklist::send(QString("INVITE %1 %2").arg(strInviteChannel).arg(strNick));
    7. }
    8. ....
    9.  
    10. void qnicklist::contextMenuEvent(QContextMenuEvent *e)
    11. {
    12. ....
    13. QMenu *minvite = new QMenu("Invite");
    14. for (int i = 0; i < strOpenChannels.count(); i++)
    15. {
    16. QString strInviteChannel = strOpenChannels.at(i);
    17. if (strInviteChannel[0] == '#')
    18. minvite->addAction(strInviteChannel, this, SLOT(invite()));
    19. }
    20. ....
    21. }
    To copy to clipboard, switch view to plain text mode 
    sender()->objectName() return null ..
    and sender() cannot cast to string...

    So how to do it by sender ? example ?
    Last edited by mero; 14th March 2010 at 02:16.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: qmenu and dynamic slot

    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.

  5. #5
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qmenu and dynamic slot


Similar Threads

  1. QMenu
    By khcbabu in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2008, 14:08
  2. QMenu
    By toki83 in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2008, 11:17
  3. QMenu in QHeaderView
    By munna in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2006, 14:58
  4. QMenu Help pls
    By munna in forum Newbie
    Replies: 2
    Last Post: 19th March 2006, 15:36
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.