Results 1 to 20 of 33

Thread: how to emit signal in a static function ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default how to emit signal in a static function ?

    i defined a signal and want to emit it in a static function. as you know ,the refrenced signal should be static, but signal function cannot be static in qt, how should i do ? how to convert this signal function to static ? Anybody can tell me how to use static_cast to solve the problem?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    try implementing a function , say emitMySignal(), from where you emit the signal.
    From the static function call this funtion, emitMySignal(), which emits the signal ...

    Hope this works

  3. #3
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default Re: how to emit signal in a static function ?

    My code just like that:
    1 class mythreadublic QThread
    2 {
    3 static void mycallbackfunc();
    4 void myfunc()
    5 private singlas:
    6 void mysignal();
    7 }
    8 void mythread::myfunc()
    9 {
    10 somefuntion(mycallbackfunc)
    11 }
    12 void mythread::mycallbackfunc()
    13 {
    14 emit mysignal();
    15 }

    what should i do ? thanks

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    just trying to understand wat u want to do...

    why do u want to emit signal from a static function ?? what purpose do u want to achieve ??

  5. #5
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default Re: how to emit signal in a static function ?

    because i use third party function, it used the callback .now I want to emit the signal in the callback funtion to inform the qt as indiating the progess. How to solve the problem?can you help me ?

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    so why do u have to make the callback function as static ??

    can u show some code ??

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: how to emit signal in a static function ?

    What sense would it make to emit a signal without having an object?

  8. #8
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    so why do u have to make the callback function as static ??
    Callbacks functions has to be static member functions
    Read this to know why http://www.newty.de/fpt/callback.html#intro


    Now for the part of emitting a signal from the static function. I believe it makes no sense to have the signal emitted from a static function. Since a static function is not bound to an oobject how will you ever connect to that signal.

    you just cannot write
    Qt Code:
    1. connect( MyClass, SIGNAL( sinalEmittedFromStaticFunction() ), someObject, SLOT( whatEver() );
    To copy to clipboard, switch view to plain text mode 


    More over if you go through the Qt docs, you will find that the signals cannot be static.

    But you can always post/send Custom Events from the static function.
    We can't solve problems by using the same kind of thinking we used when we created them

  9. #9
    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 in a static function ?

    I fail to see the need for a call back in a Qt application.
    In Qt you can use a signal/slot connection or events.
    I am sure that if you explain to us what it is you want to do we can show you how to do this with siganl and slots.

  10. #10
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default Re: how to emit signal in a static function ?

    yes,you are all right. now I found a solution way. USED a static variable to record this address,then call non-static member function of the class.JUST LIKE THAT.
    //mythread.h
    1 static unsigned long classAddress;
    2 class myclass: public QThread
    3 {
    Q_Object
    4 private:
    5 static void myfuntion(); // callback function
    6 protected:
    7 void run();
    8 static void emitMsg( unsigned long user_value,char * filename)
    9 {
    10 myclass* pthis = (myclass*)user_value; // get this address
    11 pthis->emit storescpProgressInfo(filename) ;
    12 }
    13 signals:
    14 void storescpProgressInfo(char * filename);
    15 }

    //mythread.cpp
    16 void myclass::myfuntion(char * filename)
    17 {
    18 classAddress =(unsigned long)this ;
    19 emitMsg(classAddress,filename);
    20 }

    //main.cpp
    21 frmmainWindow::frmmainWindow()
    22 {
    23 myclass my;
    24 connect(&my,SIGNAL(storescpProgressInfo(char *)),this,SLOT(myslot(char *)));
    25 }

    the myclass can emit the signal,
    but frmmainwindow cannot receive the SIGNAL and not load the myslot function, what's wrong ? can you help ?

  11. The following user says thank you to cxl2253 for this useful post:


  12. #11
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    Can you explain what exactly you are trying to achieve ? I just could not figure it out.
    more over please use the code tags like shown below
    [code]
    (put your code here)
    [/ code]
    We can't solve problems by using the same kind of thinking we used when we created them

  13. #12
    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 in a static function ?

    Qt Code:
    1. static void emitMsg( unsigned long user_value,char * filename)
    2. {
    3. myclass* pthis = (myclass*)user_value; // get this address
    4. pthis->emit storescpProgressInfo(filename) ;
    5. }
    To copy to clipboard, switch view to plain text mode 
    This makes NO SENSE in the context you made it!
    Such call back is good, when you have the need to allow some external code to execute code it knows nothing about.
    Then, such code will offer a call back signature that it knows, in which you can wrap your code for it to execute.
    But in your case you only emit a signal, which means the other code is in the context of your class, which means it must have a pointer to your class, otherwise the signal will have no effect.
    If you have pointer to your class, you don't need the call back!
    what's wrong ?
    What wrong is, that you are not listening to what we are saying to you, and you are not explainig what it is you are trying to do.
    We can't help you if you don't supply us with details about your problem.
    can you help ?
    We try to help, but you need to help us help you.

  14. #13
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default Re: how to emit signal in a static function ?

    I am sorry.My expression is poor.my problem is that i want to emit signal in the static function which will be used as a param in a callback function. why i must use the callback? because i use a third party libarary ,it used the callback, i have to do it. why i want to emit signal in the static function? I want to let the static function to inform its status and other can response alternatively.
    Now the problem is that the emited signal cannot be recieved. tommorrow i will give the detail code, thanks all.
    Last edited by cxl2253; 28th March 2007 at 14:38.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. From extends QTreeWidgetItem emit signal?
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2006, 14:54
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  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.