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?
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 :)
Re: how to emit signal in a static function ?
My code just like that:
1 class mythread:public 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
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 ??
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 ?
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 ??
Re: how to emit signal in a static function ?
What sense would it make to emit a signal without having an object?
Re: how to emit signal in a static function ?
Quote:
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
Code:
connect( MyClass, SIGNAL( sinalEmittedFromStaticFunction() ), someObject, SLOT( whatEver() );
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.
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.
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 ?
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]
Re: how to emit signal in a static function ?
Code:
static void emitMsg( unsigned long user_value,char * filename)
{
myclass* pthis = (myclass*)user_value; // get this address
pthis->emit storescpProgressInfo(filename) ;
}
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 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.
We try to help, but you need to help us help you.
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.
Re: how to emit signal in a static function ?
What do you mean by a "status of a static function"?
Re: how to emit signal in a static function ?
Quote:
Originally Posted by
cxl2253
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.
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.
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
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.
Re: how to emit signal in a static function ?
Quote:
Originally Posted by
hayati
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.
Quote:
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.
Re: how to emit signal in a static function ?
You mean you use "this" from a static method?????????????????????????????????? And it actually works?