Search code examples
c++qtqstringqbytearray

No matching function for call to 'QString::fromLatin1(QByteArray*&)'


I have a class A that calls class B after a signal is emitted. When the user closes B, I am trying to transfer a QString value from B to A. To do so, I first convert the QString to a QByteArray, then I am exchanging the QByteArray between classes. Finally, I am converting the QByteArray back into a QString.

However, during that second conversion, I get this error:

no matching function for call to 'QString::fromLatin1(QByteArray*&)`

Below is my code.

classB.h (is where the first QByteArray is implemented):

public :
    QByteArray *byt = new QByteArray;

classB.cpp:

void classB::foo(QString userame, QString password)
{
    //Some other code
    QString usernameOfNewUser;
    usernameOfNewUser = userame;
    byt = usernameOfNewUser.toLocal8Bit();
    qWarning(byt->data());
}

classA.h (where that second QByteArray is implemented):

private:
    QByteArray *newUserArray = new QByteArray;

classA.cpp (where the problem is located):

classB *cUdsfqkjb =new classB();
cUdsfqkjb->show();
if(!cUdsfqkjb->isVisible())
{
    newUserArray = cUdsfqkjb->byt;
    QString newUser = QString::fromLatin1(newUserArray);

The error is located on the last line.


Solution

  • The fromLatin1() method has the following signature:

    QString fromLatin1(const char * str, int size = -1)
    

    So you will need to pass the QByteArray's data to the method like this:

    QString newUser = QString::fromLatin1(newUserArray->constData(), newUserArray->count());
    

    In Qt5, there is also this overload:

    QString fromLatin1(const QByteArray &str)
    

    So you can use this instead:

    QString newUser = QString::fromLatin1(*newUserArray);