c++ - 无法使用 SWIG 将 QString 从 C++ 函数返回到 PERL 函数

标签 c++ qt swig perl

我有 C++ 代码(也包括 Qt),我想使用 Perl 调用这些函数。 我们可以通过使用 SWIG 来做到这一点,所以我已经实现了接口(interface)并完成了在 Perl 脚本中使用它们所需的所有工作。

我在 C++ 中有一个函数,它返回一个 QString 值,

QString get_string()
{
  return QString("mystring");
}

我又写了一个将在 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();
}

问题是,在 get_const_string() 函数中,我可以获得我想要的字符串,但是当我在我的 perl 脚本中调用这个函数时,我得到了未定义的值,即 null string

.

知道吗,这里有什么问题??

我正在使用 perl5、Qt4.8.4

提前致谢。

最佳答案

如果您不能使用QString 返回值,也许您可​​以使用std::string

如果两者都失败了并且你没有限制,你可以做一些卑鄙的把戏:

QString get_string()
{
  static QByteArray arr;
  QString str = getString();
  arr = str.toLocal8Bit();
  return arr.constData();
}

请注意,在您的应用程序运行之前,arr 变量不会被释放

编辑:找到了一个可能的解决方案,只使用 std::string ... string arguments are not recognized by SWIG

关于c++ - 无法使用 SWIG 将 QString 从 C++ 函数返回到 PERL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490915/

相关文章:

c++ - visual studio 2015 中的错误在 2012 年消失

c++ - Rapidjson中Move的使用

c++ - QCompleter 用于 QTableWidgetItem 自动完成

c++ - SFINAE 检查失败

c++ - c++中下列表达式的含义是什么

qt - 使 QLabel 的像素图透明

qt - 如何在Qt中显示一个窗口并在关闭后立即将其删除?

c++ - 如何在 Perl 中使用指向 SWIG 中的 char 的指针?

c# - 为什么包含枚举的 C++ 方法会在 SWIG/C# 中导致 AccessViolationExceptions?

c++ - 包含传递给 lua 的 std::string 的结构