Thanks for reply.But the problem it's not this point....<br><br>Let's see.<br>hello.h:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">#include<QtCore><br>
</blockquote><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">class Hello : public QObject{<br> Q_OBJECT<br>public:<br> Hello(QObject *parent = 0);<br>
static int test(){<br> static int i=0;<br> qDebug()<<i++;<br> return 0;<br> }<br>};<br></blockquote><br>hello.cpp<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
#include "hello.h"<br><br>const int kkk=Hello::test();<br><br>Hello::Hello (QObject *parent)<br>{<br>}<br></blockquote><br>test.py<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
from hello import Hello<br>Hello.test()<br></blockquote><br>Here is the result:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">$ python -v test.py <br>
# installing zipimport hook<br>import zipimport # builtin<br>....<br>......stuff ouput.....<br>....<br>import encodings.utf_8 # precompiled from /usr/lib/python2.7/encodings/utf_8.pyc<br>Python 2.7.3 (default, Aug 1 2012, 05:14:39) <br>
[GCC 4.6.3] on linux2<br>Type "help", "copyright", "credits" or "license" for more information.<br>dlopen("/home/feng/fun/lab/pyqt/hello.so", 2); <----------Here loal the hello.so<br>
0 <---------- const int kkk=Hello::test(); Now the i in test() such be 1<br>dlopen("/usr/lib/python2.7/dist-packages/sip.so", 2);<br># /usr/lib/python2.7/atexit.pyc matches /usr/lib/python2.7/atexit.py<br>
import atexit # precompiled from /usr/lib/python2.7/atexit.pyc<br>import sip # dynamically loaded from /usr/lib/python2.7/dist-packages/sip.so<br>import PyQt4 # directory /usr/lib/python2.7/dist-packages/PyQt4<br># /usr/lib/python2.7/dist-packages/PyQt4/__init__.pyc matches /usr/lib/python2.7/dist-packages/PyQt4/__init__.py<br>
import PyQt4 # precompiled from /usr/lib/python2.7/dist-packages/PyQt4/__init__.pyc<br>dlopen("/usr/lib/python2.7/dist-packages/PyQt4/QtCore.so", 2);<br>import PyQt4.QtCore # dynamically loaded from /usr/lib/python2.7/dist-packages/PyQt4/QtCore.so<br>
import hello # dynamically loaded from /home/feng/fun/lab/pyqt/hello.so<br>0 <------------ the python <i>Hello.test()</i> ,the i in test() is still 0<br></blockquote><br>Let's print the <i>Hello::test()</i><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
static int test(){<br> static int i=0;<br> printf("%p\n",&Hello::test);<br> return 0;<br> }<br></blockquote><div><br>run again:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
$ python test.py <br>0x7f8fc467bfe0 <----the <i>Hello::test() is </i>difference<br>0x7f8fc488053f<br></blockquote><div><br>When the pothon load the module<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
dlopen("/home/feng/fun/lab/pyqt/hello.so", 2); <br>......<br>import hello # dynamically loaded from /home/feng/fun/lab/pyqt/hello.so <----the <i>Hello::test() is </i>difference</blockquote><div> <br>I think it's cause by twice load the Hello.<br>
<br>Thank you<br></div></div></div><br><br><br><br><br><div class="gmail_quote">On 24 October 2012 19:56, <span dir="ltr"><<a href="mailto:Mathias.Born@gmx.de" target="_blank">Mathias.Born@gmx.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>On 24.10.2012, 00:11:13 D.Y Feng wrote:<br>
> I meet a problem in my code:<br>
<br>
> hello.h:<br>
<br>
> #include<QtCore><br>
<br>
> class Hello : public QObject{<br>
> Q_OBJECT<br>
> public:<br>
> Hello(QObject *parent = 0);<br>
> static int test(){<br>
> static int i=0;<br>
> qDebug()<<i++;<br>
> return 0;<br>
> }<br>
> };<br>
<br>
> hello.cpp:<br>
> #include "hello.h"<br>
<br>
> const int i=Hello::test();<br>
<br>
> Hello::Hello (QObject *parent)<br>
> {<br>
> }<br>
<br>
> hello.sip<br>
<br>
> %Module hello<br>
<br>
> %Import QtCore/QtCoremod.sip<br>
<br>
<br>
> class Hello : public QObject {<br>
<br>
> %TypeHeaderCode<br>
> #include "hello.h"<br>
> %End<br>
<br>
> public:<br>
> Hello(QObject *parent = 0);<br>
> static int test();<br>
> };<br>
<br>
> I use the const int i=Hello::test(); to invoke test() before main<br>
> function.It's uesful in factory pattern when you want to auto register driver.<br>
<br>
> the python test code<br>
> import hello<br>
> hello.Hello.test1()<br>
<br>
> It seems static int i in test() changed after import hello<br>
<br>
</div></div>Now, I haven't tried that myself yet. On the other hand, I don't see any<br>
problem.<br>
<div><br>
> Breakpoint 1, Hello::test1 () at hello.h:8<br>
> 8 static int test1(){<br>
> (gdb) p i<br>
> $1 = 0<br>
> (gdb) p &i<br>
> $2 = (const int *) 0x7ffff60300d0<br>
<br>
</div>This displays the address of your global variable "const int i".<br>
<div><br>
> (gdb) c<br>
> Continuing.<br>
> 0<br>
<br>
> Breakpoint 1, Hello::test1 () at main.h:8<br>
> 8 static int test1(){<br>
> (gdb) p &i<br>
> $3 = (const int *) 0x7ffff60300d0<br>
<br>
</div>Again, address of your global variable "const int i".<br>
<div><br>
> (gdb) n<br>
> 10 qDebug()<<i++;<br>
> (gdb) p &i<br>
> $4 = (int *) 0x7ffff62355c0 <---What happen here?<br>
<br>
</div>This shows the address of your local static variable "static int i",<br>
which ought to be different.<br>
The names of these objects, which are both "i", come into existence at<br>
different times. While this is perfectly legal C++, it obviously<br>
obfuscates your program. Just naming them differently should already<br>
help a lot.<br>
<br>
In summary, I don't think this is an issue with PyQt. But let me quote<br>
Bjarne Stroustrup: "C makes it easy to shoot yourself in the foot; C++<br>
makes it harder, but when you do it blows your whole leg off."<br>
<br>
Best Regards,<br>
Mathias Born<br>
<br>
<br>
</blockquote></div><br><br clear="all"><br>-- <br> <br><br>DY.Feng(叶毅锋)<br>yyfeng88625@twitter<br>Department of Applied Mathematics<br>Guangzhou University,China<br><a href="mailto:dyfeng@stu.gzhu.edu.cn" target="_blank">dyfeng@stu.gzhu.edu.cn</a><br>
<br>