Page 1 of 2 12 LastLast
Results 1 to 20 of 33

Thread: how to emit signal in a static function ?

  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,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: 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.

  15. #14
    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: how to emit signal in a static function ?

    What do you mean by a "status of a static function"?

  16. #15
    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 ?

    Quote Originally Posted by cxl2253 View Post
    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.
    Ok, lets see what we have here:
    You have an external library which you are using.
    You say the call back function should update a status, but you are not saying who is checking that status - is it the external lib that needs this status information?
    At any rate, your problem is not "how to emit a signal in a tatic function" but how to update a status to some client.
    If you will explain which code needs the status update, we might help you more.
    If you need to use a callback function you wont be able to use signals, but there are other ways as well - depending on the task you have.

  17. #16
    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: how to emit signal in a static function ?

    What high_flyer is saying is that we know a way to do what you want, but we don't want to tell you, because it's not a good thing to use such solutions and we think you may have a design flaw. So instead of continuing with a faulty design that will surely cause you more problems in future we suggest to modify the design to make your application foul proof.

    If you really can't live without callbacks, I suggest to implement an additional layer (proxy) that will handle the callback-Qt transition in both ways. Then you'll have a nice loosely coupled design which should cause you no trouble.

    BTW. In my personal opinion using callbacks is a Bad Thing (C). Qt signals-slots are soooooo much nicer.

  18. #17
    Join Date
    Aug 2006
    Location
    istanbul, turkey
    Posts
    42
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    OK let me explain what cxl2253 is trying to do?

    Assume that you have a C library.
    In the library's documentation you have a callback function about the progress of some event. So when this callback is called then cxl2253 want to emit a signal for passing info to related object.
    Ok, let's make it clear.
    Assume that you want to wrap a class to convert C library to C++ object. As used library's docs says you need to use a callback func, but callbacks are not encouraged in C++ so what else to do?
    Then use a static class member. In this case static funcs cannot contain class wide variables.

    But off course there are tricks to make them available.


    But there is two things to take care of before using these tricks:
    1. If you're using the callbacks within QThread, don't do any GUI thread operations. Do with

    Qt Code:
    1. QApplication::postEvent()
    To copy to clipboard, switch view to plain text mode 

    whatever you do.
    2. If you use emit within the QThread emit is syncronious, this means you're directly reaching the GUI thread so this is undefined behaviour also.

    As a result you had better forget emit in static class functions if It's used in QThread.
    Last edited by hayati; 28th March 2007 at 18:53.

  19. #18
    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: how to emit signal in a static function ?

    Quote Originally Posted by hayati View Post
    OK let me explain what cxl2253 is trying to do?

    Assume that you have a C library.
    In the library's documentation you have a callback function about the progress of some event. So when this callback is called then cxl2253 want to emit a signal for passing info to related object.
    We know that. But it's not mentioned anywhere that you should install callbacks on Qt objects. You should always think twice before mixing two technologies that use completely different approaches.


    Assume that you want to wrap a class to convert C library to C++ object. As used library's docs says you need to use a callback func, but callbacks are not encouraged in C++ so what else to do?
    <flamemode>Use another library?</flamemode>
    Seriously - in such situations don't wrap the functions into classes. If you need callbacks - go ahead, create callback functions that will call regular methods from regular (maybe global?) objects. Then the objects can emit signals properly.

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

    Default

    thanks all . I found that it's impossible to emit the signal in static function .Now I play the trick. I not emit the signal to inform the object which is out of the thread, I directly implement the code that shouble be in SLOT range. Because it is non-static member function of class ,i have to load it into static function.I used the trick mentioned before, used "this" address .
    thanks all again.

    Other more, if want to use QApplication.poseEvent(), what should i do ? in BCB,I can just write like that .
    Qt Code:
    1. /////////////cfrmmain.h///////
    2. enum { NM_STORESCP_CALLBACK = WM_USER + 100 };
    3. void __stdcall MyStoreScpCallback(T_StoreProgress* progress, LPCSTR filename);
    4. class TfrmMain : public TForm
    5. {
    6. BEGIN_MESSAGE_MAP
    7. VCL_MESSAGE_HANDLER(NM_STORESCP_CALLBACK, TMessage, OnStoreSCPCallback);
    8. END_MESSAGE_MAP(TForm);
    9. }
    10. //////////////cfrmmain.cpp////////
    11. void __stdcall MyStoreScpCallback(T_StoreProgress* progress, LPCSTR filename)
    12. {
    13. ///emit the message user defined :in QT ,how to postEvent and get the receive ????
    14. SendMessage(Application->MainForm->Handle, NM_STORESCP_CALLBACK,
    15. (WPARAM)progress, (LPARAM)filename);
    16. }
    17. void __fastcall TfrmMain::OnStoreSCPCallback(TMessage &Message)
    18. {
    19. ///response to the message ,do something accoring to WPARAM LPARAM
    20. }
    21. void __fastcall TfrmMain::FormShow(TObject *Sender)
    22. {
    23. //now callback here
    24. storescp_execute(m_iServerPort, MyStoreScpCallback);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 29th March 2007 at 06:20. Reason: Posts merged

  21. #20
    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: how to emit signal in a static function ?

    You mean you use "this" from a static method?????????????????????????????????? And it actually works?

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.