-1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
WKstraw
  • 67
  • 1
  • 13

1 Answers1

2

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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Thomas
  • 4,980
  • 2
  • 15
  • 30
  • Must have missed that... I tried both ways, but now I get another error : `conversion from 'QByteArray' to 'QByteArray*' is ambiguous byt = usernameOfNewUser.toLocal8Bit(); ^`. They both give me the same error. Do you happen to know where that comes from? – WKstraw Jul 11 '18 at 17:00
  • I managed to solve the second error (I just forgot adding the pointer's asterix `*`) Thank you for your answer. – WKstraw Jul 11 '18 at 17:08
  • `QByteArray toLocal8Bit() const`, so either make byt a QByteArray instead of pointer or write `*byt = usernameOfNewUser.toLocal8Bit()` – Thomas Jul 11 '18 at 17:08