本文介紹了QString 到 char* 的轉換的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我試圖通過以下方法將 QString 轉換為 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());
您能否詳細說明這種方法可能存在的缺陷,或者提供替代方法?
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?
這篇關于QString 到 char* 的轉換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!