本文介紹了QString 到 char* 的轉(zhuǎn)換的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我試圖通過以下方法將 QString 轉(zhuǎn)換為 char* 類型,但它們似乎不起作用.
I was trying to convert a QString to char* type by the following methods, but they don't seem to work.
//QLineEdit *line=new QLineEdit();{just to describe what is line here}
QString temp=line->text();
char *str=(char *)malloc(10);
QByteArray ba=temp.toLatin1();
strcpy(str,ba.data());
您能否詳細(xì)說明這種方法可能存在的缺陷,或者提供替代方法?
Can you elaborate the possible flaw with this method, or give an alternative method?
推薦答案
好吧,Qt常見問題說:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLocal8Bit();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}
所以也許您遇到了其他問題.這到底是怎么回事?
So perhaps you're having other problems. How exactly doesn't this work?
這篇關(guān)于QString 到 char* 的轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!