Qt Python中的C++代码,PyQt4

问题描述:

我需要使用动态库,写在Python代码中的Qt/C++上。Qt Python中的C++代码,PyQt4

我发现这个工具http://pyqt.sourceforge.net/Docs/sip4/using.html#ref-simple-c-example 而我有使用它的问题。

所以,我有这个文件的C++库

hello.h

// Define the interface to the hello library. 
#include <qlabel.h> 
#include <qwidget.h> 
#include <qstring.h> 

class Hello : public QLabel { 
    // This is needed by the Qt Meta-Object Compiler. 
    Q_OBJECT 

public: 
    Hello(QWidget *parent); 

private: 
    // Prevent instances from being copied. 
    Hello(const Hello &); 
    Hello &operator=(const Hello &); 
}; 

HELLO.CPP

#include "hello.h" 
#include "stdio.h" 

Hello::Hello(QWidget *parent = 0):QLabel(parent) 
{ 
    printf("First Qt example function"); 
} 

Hello::Hello(const Hello &) 
{ 

} 

Hello &Hello::operator=(const Hello &) 
{ 
    return *this; 
} 

proj.pro

QT  += core gui 

TARGET = hello 

TEMPLATE = lib 

SOURCES += hello.cpp 
HEADERS += hello.h 

我编译与qmake- QT4 然后使用使,并得到了theesw文件

-rwxr-xr-x 1 alex alex 630459 Dec 15 00:03 hello.so 
lrwxrwxrwx 1 alex alex  17 Dec 15 00:01 libhello.so -> libhello.so.1.0.0 
lrwxrwxrwx 1 alex alex  17 Dec 15 00:01 libhello.so.1 -> libhello.so.1.0.0 
lrwxrwxrwx 1 alex alex  17 Dec 15 00:01 libhello.so.1.0 -> libhello.so.1.0.0 
-rwxr-xr-x 1 alex alex 21295 Dec 15 00:01 libhello.so.1.0.0 

然后,IA有文件SIP配置

configure.py

import os 
import sipconfig 
from PyQt4 import pyqtconfig 

# The name of the SIP build file generated by SIP and used by the build 
# system. 
build_file = "hello.sbf" 

# Get the PyQt configuration information. 
config = pyqtconfig.Configuration() 

# Get the extra SIP flags needed by the imported PyQt modules. Note that 
# this normally only includes those flags (-x and -t) that relate to SIP's 
# versioning system. 
pyqt_sip_flags = config.pyqt_sip_flags 

# Run SIP to generate the code. Note that we tell SIP where to find the qt 
# module's specification files using the -I flag. 
os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "-I", config.pyqt_sip_dir, pyqt_sip_flags, "hello.sip"])) 

# We are going to install the SIP specification file for this module and 
# its configuration module. 
installs = [] 

installs.append(["hello.sip", os.path.join(config.default_sip_dir, "hello")]) 

installs.append(["helloconfig.py", config.default_mod_dir]) 

# Create the Makefile. The QtGuiModuleMakefile class provided by the 
# pyqtconfig module takes care of all the extra preprocessor, compiler and 
# linker flags needed by the Qt library. 
makefile = pyqtconfig.QtGuiModuleMakefile(
    configuration=config, 
    build_file=build_file, 
    installs=installs 
) 

# Add the library we are wrapping. The name doesn't include any platform 
# specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the 
# ".dll" extension on Windows). 
makefile.extra_libs = ["hello"] 

# Generate the Makefile itself. 
makefile.generate() 

# Now we create the configuration module. This is done by merging a Python 
# dictionary (whose values are normally determined dynamically) with a 
# (static) template. 
content = { 
    # Publish where the SIP specifications for this module will be 
    # installed. 
    "hello_sip_dir": config.default_sip_dir, 

    # Publish the set of SIP flags needed by this module. As these are the 
    # same flags needed by the qt module we could leave it out, but this 
    # allows us to change the flags at a later date without breaking 
    # scripts that import the configuration module. 
    "hello_sip_flags": pyqt_sip_flags 
} 

# This creates the helloconfig.py module from the helloconfig.py.in 
# template and the dictionary. 
sipconfig.create_config_module("helloconfig.py", "helloconfig.py.in", content) 

hello.sip

// Define the SIP wrapper to the hello library. 

%Module hello 

%Import QtGui/QtGuimod.sip 

%If (Qt_4_2_0 -) 

class Hello : public QLabel { 

%TypeHeaderCode 
#include <hello.h> 
%End 

public: 
    Hello(QWidget *parent /TransferThis/ = 0); 

private: 
    Hello(const Hello &); 
}; 


%End 

helloconfig.py.in

from PyQt4 import pyqtconfig 

# These are installation specific values created when Hello was configured. 
# The following line will be replaced when this template is used to create 
# the final configuration module. 
# @[email protected] 

class Configuration(pyqtconfig.Configuration): 
    """The class that represents Hello configuration values. 
    """ 
    def __init__(self, sub_cfg=None): 
     """Initialise an instance of the class. 

     sub_cfg is the list of sub-class configurations. It should be None 
     when called normally. 
     """ 
     # This is all standard code to be copied verbatim except for the 
     # name of the module containing the super-class. 
     if sub_cfg: 
      cfg = sub_cfg 
     else: 
      cfg = [] 

     cfg.append(_pkg_config) 

     pyqtconfig.Configuration.__init__(self, cfg) 

class HelloModuleMakefile(pyqtconfig.QtGuiModuleMakefile): 
    """The Makefile class for modules that %Import hello. 
    """ 
    def finalise(self): 
     """Finalise the macros. 
     """ 
     # Make sure our C++ library is linked. 
     self.extra_libs.append("hello") 

     # Let the super-class do what it needs to. 
     pyqtconfig.QtGuiModuleMakefile.finalise(self) 

然后我就与

$ python ./configure.py 

配置然后我让使用make

/usr/bin/ld: cannot find -lhello 
collect2: error: ld returned 1 exit status 
make: *** [hello.so] Error 1 

错误,但这个简单的问题,与加解决 - L.

,我已经在我的目录中新的.so - hello.so

然后,我用简单的Python脚本测试结果。

import hello 
h = hello.Hello() 
print h 

而且我得到了一个错误

Ever/ QtExample $ python ./pythontest.py Traceback (most recent call last): File "./pythontest.py", line 1, in <module> 
    import hello ImportError: libhello.so.1: cannot open shared object file: No such file or directory 

哪里是我的错?我做错了什么?

+0

您还没有将libhello.so.1文件放到系统库路径中。所以你需要在制作时指定-L。显然你也需要在运行时指定库路径,而你还没有这样做。尝试将LIBPATH或LD_LIBRARY_PATH环境变量设置为包含libhello.so.1文件的目录。也许有一种python特定的方式来添加库路径。另见http://stackoverflow.com/questions/1099981/why-cant-python-find-shared-objects-that-are-in-directories-in-sys-path –

您是否运行make install?这应该已经将libhello.so添加到/ usr/lib中。

如果这仅仅是一个测试库,你不想安装的话,你应该在同一个文件夹libhello.so你已经招呼模块或添加libhello.so的文件夹路径到您的LD_LIBRARY_PATH环境变量。

+0

非常感谢。 LD_LIBRARY_PATH工作! – user1113159