문제

In Qt 4 it is possible to automatically cast a QString to a "const char *", e.g. I could pass a QString to a function that expected a "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). If I understand the documentation correctly, it is because the Qt 3 compatibility layer is not included anymore in QT5.

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. 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.

#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.

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.

This is somewhat equal to rewriting the original function to a different signature expecting QString though because unfortunately the corresponding constructor is not explicit. I imagine that was not changed in Qt 5 for compatibility as it would have broken too much code.

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)
  • Overload 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.

The way to achieve your aim is
a) Add overloads to the functions expecting const char* which can handle QStrings,
b) Add the explicit conversion code to the call-site, or
c) Go to some char-based string, like std::string.

None of those fulfill your aim of nearly no changes though.

I found this on QT forum

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

This resolved my issue

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top