21

I have to use the output of a function of a type const char* and I need to convert it to QString.

Note: inside that function, these are lines of code to return the const char*

char* ClassA::getData() const{
    return const_cast<char *> (_foo.c_str());
}

where _foo is std::string.

I tried to use the following lines of code but always get empty string (actually not empty but contain only the new lines characters and neglect all other characters).

QString foo1 = QString(temp.getData());
QString foo2 = QString::fromLocal8Bit(temp.getData());
QString foo3 = QString(QLatin1String(temp.getData()));
QString foo4 = QString::fromAscii(temp.getData());
QString foo5 = QString::fromUtf8(temp.getData());
László Papp
  • 51,870
  • 39
  • 111
  • 135
Mahmoud Hassan
  • 598
  • 1
  • 3
  • 15
  • Show more code. This is not sufficient to help. `QString foo5 = QString::fromUtf8(temp.getData());` should work. – László Papp Dec 29 '13 at 11:45
  • 1
    `QString` accepts `const char *`. No need to go out of your way to pretend it's not constant data. – chris Dec 29 '13 at 11:46
  • @LaszloPapp strangely all these versions got the same "empty" string. I think the problem in the `const_cast` in the function that I call. but I have no permission to edit that function to return `std:string` or normal `char *` – Mahmoud Hassan Dec 29 '13 at 11:51
  • In cases like this I save the function output (the result of `getData()`) to a variable and check with the debugger whether the function really returns a valid string. Then proceed with conversion to QString. It has to convert since QString has a suitable constructor (http://qt-project.org/doc/qt-4.8/qstring.html#QString-8). As chris said, try without casting, simply with `const char*`. – Sceptical Jule Dec 29 '13 at 12:07
  • I start thinking that _foo is not set properly in the code around. I could not explain it otherwise. :-) – László Papp Dec 29 '13 at 12:08
  • Drop that `const_cast`! Immediately! – Lightness Races in Orbit Dec 29 '13 at 13:25
  • @LightnessRacesinOrbit I can't change the ClassA implementation. – Mahmoud Hassan Dec 29 '13 at 13:30
  • @MahmoudHassan: If you're in C++11, return `&_foo.front()` then. Functionally the same but doesn't raise eyebrows. – Lightness Races in Orbit Dec 29 '13 at 13:31

1 Answers1

10

The code below should work fine. Your issue is most likely somewhere else. Please do a clean build.

The error will be somewhere else in your more complex code that you have not shared with us. You are probably getting issues with setting _foo incorrectly.

As you noted yourself, you cannot change the interface, but it is better to take a note that in an ideal world, you would not mix std strings with QStrings. You would just use QStrings altogether in your code.

Even if you need to use std or raw char* types for some reason, it is better not to do such a const cast in the code since QString will cope with const strings passed to it.

main.cpp

#include <QString>
#include <QDebug>

class ClassA
{
    public:
        ClassA() { _foo = "Hello World!\n"; }
        ~ClassA() {}

        char* getData() const {
            return const_cast<char *> (_foo.c_str());
        }

    private:
        std::string _foo;
};

int main()
{
    ClassA temp;
    QString myString = QString::fromUtf8(temp.getData());
    qDebug() << "TEST:" << myString;
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Output

TEST: "Hello World!
"
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • in the `getData()` try to return the same way as in the question: `return const_cast (_foo.c_str());` – Mahmoud Hassan Dec 29 '13 at 11:53
  • @MahmoudHassan: I even ran it through qt 4 as well as 5 with `qmake -r && make && ./main`. – László Papp Dec 29 '13 at 11:57
  • @MahmoudHassan: which compiler type and version are you using? – László Papp Dec 29 '13 at 12:01
  • I know it should work I actually can write something like `cerr << temp.getData();` and get the string printed in the terminal but the `qDebug() << QString::fromUtf8(temp.getData());` print just empty lines. I use QT 4.8 and build with gmake. – Mahmoud Hassan Dec 29 '13 at 12:09
  • @MahmoudHassan: `gmake`? Which operating system are you on? Can you also reply to my question about the compiler? So, your problem is probably not the conversion, but qDebug()? Have you tried QTextStream instead of qDebug()? – László Papp Dec 29 '13 at 12:10
  • hmm, I think it is not qDebug problem, as I tried `_textEditor->appendPlainText(QString::fromUtf8(temp.getData()));` and I'm working on "Red Hat Enterprise Linux Workstation release 6.4 (Santiago)" – Mahmoud Hassan Dec 29 '13 at 12:13
  • @MahmoudHassan: which Qt version are you using? Also what does it print out: `char* getData() const { qDebug() << "TEST0:" << _foo; return const_cast (_foo.c_str()); }` – László Papp Dec 29 '13 at 12:14
  • @MahmoudHassan: ok, what did it print out after "TEST0:"? – László Papp Dec 29 '13 at 12:22
  • Actually the problem was the string was not set properly before calling the function, very thanks for the hint :) and the string that was in the terminal was not from the `cerr` line was from another class but because the project is too big and too complicated I got confused. thanks again for the answer and the hint. – Mahmoud Hassan Dec 29 '13 at 12:24
  • @LightnessRacesinOrbit: how would you solve it otherwise if he cannot change the interface, or perhaps you have not read the thread entirely? – László Papp Dec 29 '13 at 13:28
  • @LaszloPapp: At least put a comment on it, or a disclaimer in your answer. Otherwise someone is going to come along one day and copy/paste this code, not realising that it has a "deliberate error" in it. – Lightness Races in Orbit Dec 29 '13 at 13:29
  • @LaszloPapp: Better :P – Lightness Races in Orbit Dec 29 '13 at 13:42