Results 1 to 4 of 4

Thread: How to emit signal from static method?

  1. #1
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default How to emit signal from static method?

    Hi I have tried to emit signal from static method in this way .


    Qt Code:
    1. MainWidget* pThis;
    2. MainWidget *MainWidget:: _instance = 0;
    3.  
    4. MainWidget::MainWidget(QWidget *parent):QWidget(parent)
    5. {
    6. pThis = this;
    7. }
    8. MainWidget* MainWidget::getInstance()
    9. {
    10. if(!_instance)
    11. _instance = new MainWidget();
    12. return _instance;
    13. }
    14. void MainWidget :: emitMySignal(){
    15. qDebug()<<"emitMySignal";
    16. pThis->emit mySignal ();
    17. }
    18. MainWidget::~MainWidget()
    19. {
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    and the call to static function and signal slot connection is here

    Qt Code:
    1. MainWidget::getInstance()->emitMySignal();
    2. qDebug()<<"after emitCall back....";
    3. QObject::connect(MainWidget::getInstance(),SIGNAL(mySignal()),this,SLOT(connectToSlot()));
    To copy to clipboard, switch view to plain text mode 


    But this is not happening for me .
    Plz suggest

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to emit signal from static method?

    you connected the slot after emitted the signal.

    try with

    Qt Code:
    1. QObject::connect(MainWidget::getInstance(),SIGNAL(mySignal()),this,SLOT(connectToSlot()));
    2. MainWidget::getInstance()->emitMySignal();
    3. qDebug()<<"after emitCall back....";
    To copy to clipboard, switch view to plain text mode 

    what is the static method??
    Last edited by mcosta; 20th July 2011 at 08:48. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to emit signal from static method?

    Qt Code:
    1. pThis = this; //pThis is global, don't do this, you never know what it contains unless, ctor as private
    2.  
    3. void MainWidget :: emitMySignal(){ //I assume this is non static function, if not make it non-static function
    4. qDebug()<<"emitMySignal";
    5. //pThis->emit mySignal ();
    6. emit mySignal(); //this will work
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to emit signal from static method?

    You can't emit signal from static methods, because static methods are not part of an objects instance, they are global, in the scope of the class, which means, you don't have a "sender" object for them.
    You could send an event though.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. Replies: 2
    Last Post: 3rd May 2011, 20:22
  3. emit signal from a slot?
    By ask8y@yahoo.com in forum Qt Programming
    Replies: 9
    Last Post: 11th June 2010, 20:18
  4. how to know which button emit the signal?
    By coder1985 in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 14:26
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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.