[英]not able to return QString from c++ function to PERL function by using SWIG
I have c++ code (Includes Qt also), i want to call those functions by using Perl. 我有c ++代码(也包括Qt),我想使用Perl调用这些函数。 Which we can do by using SWIG, so I have implemented interfaces and did all the stuff need to use them in Perl script.
我们可以通过使用SWIG来做到这一点,所以我已经实现了接口,并做了所有需要在Perl脚本中使用它们的内容。
I have a function in c++ which returns a QString value, 我在c ++中有一个函数返回QString值,
QString get_string()
{
return QString("mystring");
}
I have written one more class which will be used in perl script where i have a function which calls this get_string() function and returns const char*. 我还写了一个将在perl脚本中使用的类,其中我有一个函数调用此get_string()函数并返回const char *。
const char* get_const_string()
{
QString str = get_string();
**//here I print str and str .toLocal8Bit().constData()
//both are printing the text which i shoud get here**
return str.toLocal8Bit().constData();
//here I have tried diff combinations also, as
// return str.toStdString().c_str();
}
The problem is, in get_const_string() function, I could get the string I wanted, but when I call this function in my perl script, I am getting undefine value ie null string 问题是,在get_const_string()函数中,我可以获得所需的字符串,但是当我在perl脚本中调用此函数时,我得到的值不确定,即空字符串
. 。
Any idea, what is the problem here ?? 任何想法,这是什么问题?
I am using perl5, Qt4.8.4 我正在使用perl5,Qt4.8.4
Thanks in advance. 提前致谢。
if you cant use a QString
return value, maybe you can use std::string
. 如果您不能使用
QString
返回值,则可以使用std::string
。
if both fail and you do not have limitations, you could do some dirty trick: 如果两者都失败并且您没有限制,则可以采取一些肮脏的把戏:
QString get_string()
{
static QByteArray arr;
QString str = getString();
arr = str.toLocal8Bit();
return arr.constData();
}
note that the arr
variable will not be free'd untill your app is running 请注意,直到您的应用程序运行后,
arr
变量才会被释放
edit: found a possible solution to just use std::string
... string arguments are not recognized by SWIG 编辑:找到了仅使用
std::string
... SWIG无法识别字符串参数的可能解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.