简体   繁体   English

Qt 5:const char * QString强制转换

[英]Qt 5: const char * QString cast

In Qt 4 it is possible to automatically cast a QString to a "const char *", eg I could pass a QString to a function that expected a "const char *". 在Qt 4中,可以自动将QString转换为“const char *”,例如,我可以将QString传递给期望“const char *”的函数。

void myFunction(const char *parameter);

QString myString;
myFunction(myString); //works in QT4

In Qt 5 I would however get an "error C2440: 'type cast' : cannot convert from 'QString' to 'const char *'" (that is the Visual C++ 2008 compiler, other compilers would throw something similar). 在Qt 5中,我会得到一个“错误C2440:'type cast':无法从'QString'转换为'const char *'”(即Visual C ++ 2008编译器,其他编译器会抛出类似的东西)。 If I understand the documentation correctly, it is because the Qt 3 compatibility layer is not included anymore in QT5. 如果我正确理解文档,那是因为QT5中不再包含Qt 3兼容层。

Of course, I could change the function call from 当然,我可以改变函数调用

myFunction(myString); 

to

myFunction(myString.toLatin1().data());

However, I have a huge code base that compiles fine with Qt 4 and I would really like to have my old code compile with Qt 5 without modifying it. 但是,我有一个庞大的代码库,可以用Qt 4编译好,我真的想让我的旧代码用Qt 5编译而不修改它。 Is there any way to achieve this? 有没有办法实现这个目标?

You could create a macro or inline function for your purpose to minimize the changes, but since that would also require a grep alike operation, there is not much difference. 你可以为你的目的创建一个宏或内联函数来最小化更改,但由于这也需要一个类似于grep的操作,因此没有太大的区别。

#define QString2charp(myString) myString.toLatin1().data()

or 要么

inline char* QString2charp (const QString &myString)
{
    return myString.toLatin1().data();
}

and then: 然后:

myFunction(QString2charp(myString));

BUT

of course, in an ideal world, it would be nice if your "myFunction" could get an overload expecting a QString argument. 当然,在一个理想的世界中,如果你的“myFunction”可以获得一个期望QString参数的重载会很好。

void myFunction(const char *parameter);
void myFunction(QString parameter);

and then the overload would be implemented like this: 然后重载将实现如下:

void myFunction(const QString &parameter)
{
    myFunction(myString.toLatin1().data());
}

of course, this would require the constructor being explicit so that no implicit conversion can happen, otherwise the compiler will complain about ambiguity in presence of both when trying to pass const char* , but if you always use QString, it should just work with Qt 5. 当然,这需要构造函数是显式的,这样才不会发生隐式转换,否则编译器会在尝试传递const char*时抱怨存在歧义,但是如果你总是使用QString,它应该只适用于Qt 5。

This is somewhat equal to rewriting the original function to a different signature expecting QString though because unfortunately the corresponding constructor is not explicit. 这有点等于将原始函数重写为期望QString的不同签名,因为遗憾的是相应的构造函数不是显式的。 I imagine that was not changed in Qt 5 for compatibility as it would have broken too much code. 我想在Qt 5中没有改变兼容性,因为它会破坏太多的代码。

As you can see, you do not need to change anything in your code this way, other than adding a one-liner overload. 正如您所看到的,除了添加单行重载之外,您不需要以这种方式更改代码中的任何内容。 It is neat, isn't it? 它很整洁,不是吗?

You could either 你可以

  • Change the signature of your function to MyFunction( const QString & parameter ) (of course that works only if you don't need the one that takes char * any more) 将函数的签名更改为MyFunction( const QString & parameter ) (当然,只有当您不再需要使用char *才能使用它)
  • Overload MyFunction : 重载MyFunction

     void myFunction(const char *parameter); //this is your normal function void myFunction( const QString & parameter ) { char * c_str = nullptr; <convert parameter to char * in any way that suits you> myFunction( c_str ); } 
  • If the above is impossible for some reason, the best approach is probably to write a similar wrapper: 如果出于某种原因上述情况是不可能的,那么最好的方法可能就是写一个类似的包装器:

     void MyFunctionWrapper( const QString & parameter ) { <same as above> } 

It is no longer possible to cast QString to const char* in >= Qt 5, because QString is UTF-16 and no longer has an entry for caching the result of converting to ASCII. 不再可能将QString转换为> = Qt 5中的const char* ,因为QStringUTF-16并且不再有用于缓存转换为ASCII的结果的条目。

The way to achieve your aim is 实现目标的方法是
a) Add overloads to the functions expecting const char* which can handle QString s, a)为期望const char*的函数添加重载,它可以处理QString
b) Add the explicit conversion code to the call-site, or b)将显式转换代码添加到呼叫站点,或
c) Go to some char -based string, like std::string . C)去一些char为基础的弦一样std::string

None of those fulfill your aim of nearly no changes though. 这些都没有达到你几乎没有变化的目标。

I found this on QT forum 我在QT论坛上找到了这个

const QByteArray byteArray = mytext = textBox.text().toUtf8();
const char *mytext = byteArray.constData();

This resolved my issue 这解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM