1

I have a QString that I would like to convert into a char* not a QChar* because I'll be passing it into a method that takes a char*, however I can't convert it without it getting a const char*. For example I have tried:

QString name = "name";
QByteArray byteArray = name.toUtf8();
myMailboxName = byteArray.constData();

and

QString name = "name";
QByteArray byteArray = name.toUtf8();
myMailboxName = byteArray.data();

where myMailboxName is a private char* in my class. However I get an error because it is returning a const char* and can't assign it to a char*. How can I fix this?

Amre
  • 1,630
  • 8
  • 29
  • 41
  • dup? http://stackoverflow.com/questions/5505221/converting-qstring-to-char – billz Nov 15 '12 at 22:11
  • Don't. You're being given a pointer to data that you may not modify, so do not assign it to a `char*`. Assign it to a `char const*`. Why are you storing pointers long-term? – Lightness Races in Orbit Nov 15 '12 at 22:12
  • @billz: Not exactly a dup. It seems that Amre wants a pointer to mutable characters, not a `const char*` – David Rodríguez - dribeas Nov 15 '12 at 22:13
  • what does dup? mean. I cant use a const char* becuase I have a class client which looks like this: `class Client { public: Client(); void init(QString name); void sendMessage(QString mess); private: char *myMailboxName, buf[MSG_SIZE]; struct mq_attr attr; mqd_t mq_ownBox, mq_centralBox; };` I don't know the value of myMailboxName beofre the program starts and if I just created a const char* it would go out of scope after the function im calling it from ends. – Amre Nov 15 '12 at 22:15
  • @Amre dup = duplication, or same question – billz Nov 15 '12 at 22:18

6 Answers6

3

This is because data() returns the address of the buffer in the bytearray, you can read it, but obviously you should not write it. You have your own buffer outside the bytearray. If you want the data, you should copy the buffer of bytearray into the myMailBoName.

use memcpy function

dzada
  • 5,344
  • 5
  • 29
  • 37
2

try this code snippet

const char *myMailboxName = name.toLatin1().data();
sig_seg_v
  • 570
  • 4
  • 15
shivaranjani
  • 75
  • 2
  • 6
  • Welcome to getting your first 10 rep points. Please check out how to use Markdown formatting - this would allow the code in your answers to be syntax highlighted and displayed in a mono-space font. Next to the answer/question textfield is a '?' icon which provides the basics. – marko Nov 28 '12 at 13:33
  • Note: this is likely going to crash the program as the QByteArray returned by toLatin1() goes away at the end of the line. The pointer returned by data() points to nothing. – jomamaxx Jan 23 '21 at 04:30
1

Use strdup. It does the allocation and the copy at the same time. Just remember to free it once you're done.

Mike
  • 693
  • 3
  • 13
1

You can really use strdup (stackoverflow question about it), as Mike recommends, but also you can do that:

// copy QString to char*
QString filename = "C:\dev\file.xml";
char* cstr;
string fname = filename.toStdString();
cstr = new char [fname.size()+1];
strcpy( cstr, fname.c_str() );

Got there: stackoverflow similar question.

Community
  • 1
  • 1
NG_
  • 6,895
  • 7
  • 45
  • 67
0

I use something like this wrapper:

template<typename T, typename Y>
void CharPasser(std::function<T(char *)> funcToExecute,  const Y& str)
{
    char* c = 0;
    c = qstrdup(c, str.c_str());
    funcToExecute(c);
    delete[] c;
}

int SomeFunc(char* ){}

then use it like:

CharPasser<int, std::string>(SomeFunc, QString("test").tostdString())

At least this saves a bit of typing... :)

Zeks
  • 2,265
  • 20
  • 32
0

consider following example
QString str = "BlahBlah;"

try this
char* mychar = strdup(qPrintable(str));

or this
char* mychr = str.toStdString().c_str();

or this
char* mychar = strdup(str.ascii());

Every syntax worked for me ;)

Ani
  • 2,848
  • 2
  • 24
  • 34