最近在写一个QT程序。需要把QString 转换成char * 指针。结果遇到程序崩溃的严重错误。在大量的帮助调试下,发现是QString转换成char * 的时候,一定要定义一个QBateArray的变量。不能连写 。
以下是官方回复:
How can I convert a QString to char* and vice versa ?(trolltech)
Answer:
In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array. See the documentation:
See the following example for a demonstration:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLatin1();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}
Note that it is necessary to store the bytearray before you call data() on it, a call like the following
const char *c_str2 = str2.toLatin1().data();
will make the application crash as the QByteArray has not been stored and hence no longer exists.
To convert a char* to a QString you can use the QString constructor that takes a QLatin1String, e.g:
QString string = QString(QLatin1String(c_str2)) ;
差点给QT弄的精神崩溃。
分享到:
相关推荐
当QString内容包含汉字时,转换char*等会发生失败。此接口解决了该问题。使用后char*与unsigned char*的qDebug()输出与QString输出结果相同。 注意,该函数返回unsigned char*使用完毕后记得delete。
一、QString赋值给char* 例如: QString qstr; char *str = qstr.toLatin1().data();//这样便把qstr中的值传给了str 二 、char [] 赋值给string char str[32]; string str2(str);//这样便把str中的值传给...
能够有效的将QString转换为TCHAR*,编译运行通过
在Qt下写串口字符串读写方面的程序,里面需要处理一些QString和字符数组等方面的字符处理。 QString: QString类提供Unicode字符串。 QString存储一个16位QChars字符串,其中每个QChar对应一个Unicode 4.0字符。...
• Internationalization • Preparing for localization • Localization • Localization process • Qt Linquist tool • Testing • QTestLib
QT将表格中的数据读取出来(QString),在使用QString 转换成对应的ASCII值(char)用于再在数组中传输 :例如:“A” 转换成 int或者char
QString和string的互相转换; QString to Char*; char* to QString; char to number; number to QString; uint8_t to QString; BYTE* to char[] to QString; string to BYTE [] ; BYTE[] to string //上个...
将 char Buff[]数组中的数据转换成QString数据,例如:将0x41转换“A”,就是将Char转成ASCII所对应的字母
QString详解
void WriteLog(int loglevel, QString strlogstr, QString strfilename, int iline, QString strfunname) { QString g_logdllpath(g_runPath + "/dependences/LoggingModeDLL.dll"); HINSTANCE hDll = LoadLibrary...
[这个是需要是想输出变量值,QString需要转成char*类型] 例如:log_debug("strXXX: %s", strXXX.toStdString().c_str()); 然后就会输入到txt文本当中 windows,linux均可使用,可以定位到报错地方
可以获取textEdit上的QString...结果转换为QString,并且每两位数据都需要增加空格展示; 输入清除功能; 程序内容说明详情: https://blog.csdn.net/qq_28178271/article/details/128651784?spm=1001.2014.3001.5502
在VS2010中调试QString等Qt类型的时候不能显示值是不是很坑爹,这个就是为了解决此问题的工具文件,该文件包含了VS中的MFC支持,和Qt类型的支持,还增加包含了网上没有的QDateTime等的支持,将下载的文件放到 ...
VS调试QT类中的QString无法查看到变量值时,进行相关的配置即可实现变量值的查看: 下载**qt5.natvis**放置到路径: > Microsoft\VS2017\Enterprise\Common7\Packages\Debugger\Visualizers
2.首先来两个int类型的数据(或double型): 4.将int型(double型)转换为QByteArray型: 5.QString与QByteArray之
QString str; const char** engines = NULL; chessGame = game; redIsMan = true; blackIsMan = true; blackOnTop = true; /* Initialize the combobox for search engines */ engines = game->...
C2001:常量中有换行符 C2146:语法错误;缺少";"(在标识符“QString”的前面) 针对上面俩错误想的歪招。 详情可以看: https://blog.csdn.net/weixin_44635546/article/details/125677035
类似于QString的js类,支持中文的字符串操作,如字符的查找,比较,截取等
确定能用的Qstrings.pas,请用7ZIP解压。