Qt类型转换合集

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*

void mesServiceClient::allocate(char *& target, const QString& value){
    if(value==nullptr){
        target=nullptr;
        return;
    }
    QByteArray valueByteArray = value.toUtf8();
    int valueByteArrayLength = valueByteArray.length()+1;
    target=new char[valueByteArrayLength+1];
    strcpy_s(target, valueByteArrayLength, valueByteArray.data());
}
void mesServiceClient::recycle(char *& target){
    delete[] target;
    target=nullptr;
}

char*转换为QString

char* item;
QString Value=QString::fromUtf8(item);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值