Results 1 to 8 of 8

Thread: QAction

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default QAction

    I need to force act to be fileSaveAction; but don't work; I put fileSaveas action inside popupMenu (mouse menu); so when I click on filesaveas on popupmenu SLOT starts but the sender() must be fileSaveAs...thanks
    Qt Code:
    1. mainform::fileSaveAs()
    2. QAction* act = (QAction*) sender();
    3. if (act != this->fileSaveAction && act != this->fileSaveAsAction)
    4. act = fileSaveAction;
    5. if ((fileSaveAsAction == act)) doSomething //here problem
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAction

    Wouldn't the handling be easier, and more importantly, much more clear if you had separate slots for different saving actions? Anyway, if you really want something like this, put the actions in a group and connect the group's selected(QAction*) to that slot and you'll get the according source action as a parameter.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QAction

    mmm my problem is this: when the SLOT below is called, I'd like a way to know, inside filesaveAs, who's the caller (ie myPopupmenu)
    Qt Code:
    1. myPopupmenu->insertItem(QPixmap::fromMimeSource("icone215.png"),
    2. "&Save As", w, SLOT(fileSaveAs()), CTRL+Key_A );
    To copy to clipboard, switch view to plain text mode 
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAction

    But what exactly is the problem? Sender() should return the object emitting the signal... What exactly are you trying to achieve?

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QAction

    Quote Originally Posted by mickey
    I need to force act to be fileSaveAction; but don't work; I put fileSaveas action inside popupMenu (mouse menu); so when I click on filesaveas on popupmenu SLOT starts but the sender() must be fileSaveAs...thanks
    Qt Code:
    1. mainform::fileSaveAs()
    2. QAction* act = (QAction*) sender();
    3. if ((fileSaveAsAction == act)) doSomething //here problem
    To copy to clipboard, switch view to plain text mode 
    the problem is if I invoke saveAs SLOT with "mypopupMouseMenu" it starts but act is not == fileSaveAsAction; this is the problem;
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAction

    What exactly do you mean by "invoke"? If you call a slot yourself (and not trigger through signal emition), sender() won't return a valid value. Could you show us the connect statement responsible for the signal-slot connection?

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QAction

    Quote Originally Posted by mickey
    mmm my problem is this: when the SLOT below is called, I'd like a way to know, inside filesaveAs, who's the caller (ie myPopupmenu)
    Qt Code:
    1. myPopupmenu->insertItem(QPixmap::fromMimeSource("icone215.png"),
    2. "&Save As", w, SLOT(fileSaveAs()), CTRL+Key_A );
    To copy to clipboard, switch view to plain text mode 
    with this SLOTfileSaveAs() is called; but I need to know, inside this SLOT, who's the sender...How do I capture the sender() in this case? thanks
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAction

    The sender is an anonymous action here, which is created by the menu. If you want to identify it, you have to create a QAction object yourself and add it to the menu using QAction::addTo(). Only then you'll be able to identify the action. But tell me, why do you want to identify the action at all? If you want to have a bunch of if blocks calling different functions in the slot, maybe it's simpler to have separate slots for each of them? Or if they contain hierarchical actions, call them directly one from the other like so:

    Qt Code:
    1. menu->insertItem("Save", this, SLOT(save()));
    2. menu->insertItem("Save As", this, SLOT(saveAs()));
    3. //...
    4.  
    5. void X::save(){
    6. //...
    7. }
    8.  
    9. void X::saveAs(){
    10. QString fname = QFileDialog::getSaveFileName();
    11. if(!fname.isEmpty()){
    12. m_filename = fname;
    13. save(); // call the "lower level" slot
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QAction group
    By mickey in forum Newbie
    Replies: 1
    Last Post: 12th July 2006, 19:15
  2. Replies: 9
    Last Post: 31st March 2006, 13:07
  3. Mouseover signal on QAction
    By manhds in forum Qt Programming
    Replies: 14
    Last Post: 21st March 2006, 03:24
  4. Replies: 1
    Last Post: 18th March 2006, 16:37
  5. Replies: 2
    Last Post: 23rd February 2006, 16:38

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.