QString转化为wchar_t*类型
方法1 分配内存
void mesServiceClient::allocate(wchar_t*& target, const QString& value){
if(value==nullptr){
target=nullptr;
return;
}
QString temp = value;
size_t tempLength = temp.size() + 1;
target = new wchar_t[tempLength];
wcscpy_s(target, tempLength, temp.toStdWString().c_str());
}
void mesServiceClient::recycle(wchar_t*& target){
delete[] target;
target=nullptr;
}
方法2 值传递,用于调用
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
QByteArray narrowBytes1 = FrockName.toUtf8();
std::wstring wideStr1 = converter.from_bytes(narrowBytes1.constData());
wchart* aa= const_cast<wchar_t*>(wideStr1.c_str());
wchar_t*转换为QString
QString::fromWCharArray(wchar_t* xx)
wchar_t*给wchar_t*
size_t length = wcslen(webList->checkClass);
checkList->EquipmentCheck[i]->checkClass = new wchar_t[length + 1];
wcscpy_s(checkList->EquipmentCheck[i]->checkClass, length + 1, webList->checkClass);
long转换为LONG64*
void mesServiceClient::allocate(LONG64*& target, const long& value){
target= new LONG64;
*target = value;
}
void mesServiceClient::recycle(LONG64*& target){
delete target;
target=nullptr;
}
LONG64*转long
前面加*
QDateTime转换为time_t*
void mesServiceClient::allocate(time_t*& target, const QDateTime& value){
target = new time_t;
*target = value.toTime_t();
}
void mesServiceClient::recycle2(time_t*& target){
delete target;
target=nullptr;
}
time_t*转QDateTime
time_t* aa;
QDateTime::fromSecsSinceEpoch(*aa);
int转换为int *
void mesServiceClient::allocate(int *& target, const int& value){
target = new int;
*target = value;
}
void mesServiceClient::recycle(int *& target){
delete target;
target=nullptr;
}
int *转换为int
前面加*
bool转换为bool*
void mesServiceClient::allocate(bool *& target, const bool& value){
target = new bool;
*target = value;
}
void mesServiceClient::recycle(bool *& target){
delete target;
target=nullptr;
}
bool*转换为bool
前面加*
qreal转换为std::wstring *
void mesServiceClient::allocate(std::wstring *& target, const qreal& value){
target = new std::wstring();
if (value - static_cast<long long>(value) == 0) {
// 是整数,按照整数方式转换
*target = std::to_wstring(static_cast<long long>(value));
} else {
// 不是整数,按照浮点数方式转换
*target = std::to_wstring(value);
}
}
void mesServiceClient::recycle(std::wstring *& target){
delete target;
target=nullptr;
}
std::wstring *转换为qreal
std::wstring *aa
QString::fromStdWString(*aa).toDouble()
short转换为short *
void mesServiceClient::allocate(short *& target, const short& value){
target = new short;
*target = value;
}
void mesServiceClient::recycle(short *& target){
delete target;
target=nullptr;
}
short *转换为short
前面加*
QString转换为char*
//分配
QString value ;
QByteArray valueByteArray = value.toUtf8();
int valueByteArrayLength = valueByteArray.length();
char *item=new char[valueByteArrayLength+1];
strcpy_s(item, valueByteArrayLength+1, valueByteArray.data());
//回收
delete[] item;
item=nullptr;
char*转换为QString
char* item;
QString Value=QString::fromUtf8(item);
标签:类型转换,Qt,void,value,wstring,QString,mesServiceClient,合集,target
From: https://blog.csdn.net/m0_58053246/article/details/144490493