QByteArray length() count() size()的区别

以前没注意length()  count() size()这几个有什么区别,都是随意用,想到哪个就用哪个,但总是疑问到底他们有没有区别。

今天就去探个究竟,于是查qt的帮助文档,终于找到答案,三个确实没有区别,都是和size一样的

以下均摘抄自qt帮助文档,希望可以给初学者提供帮助。

int QByteArray::length() const

Same as size().

 

int QByteArray::count() const

This is an overloaded function.

Same as size().

 

int QByteArray::size() const

Returns the number of bytes in this byte array.

The last byte in the byte array is at position size() - 1. In addition, QByteArray ensures that the byte at position size() is always '\0', so that you can use the return value of data() and constData() as arguments to functions that expect '\0'-terminated strings. If the QByteArray object was created from a raw data that didn't include the trailing null-termination character then QByteArray doesn't add it automaticall unless the deep copy is created.

Example:

  QByteArray ba("Hello");
  int n = ba.size();          // n == 5
  ba.data()[0];               // returns 'H'
  ba.data()[4];               // returns 'o'
  ba.data()[5];               // returns '\0'
See also isEmpty() and resize().

 

希望可以帮到你们,有帮到你们的点个赞吧,你们的支持是我继续写博客的动力!

jsonexe::jsonexe() { path = QDir::currentPath() + "/../user.json"; qDebug() << path; file.setFileName(path); } jsonexe::~jsonexe() { } QByteArray jsonexe::getJson(QStringList sL) { QJsonObject userobj,user; if(file.open(QIODevice::ReadOnly | QIODevice::Text)){ jdoc = QJsonDocument::fromJson(file.readAll()); users = jdoc.array(); int size = users.size(); for (int i=0;i<size;i++) { user = users.at(i).toObject(); if(user["username"] == sL[0] && user["password"] == sL[1]){ userobj = user; break; } } }else{ qDebug() << "open file failed"; } file.close(); jdoc.setObject(userobj); return jdoc.toJson(); } bool jsonexe::judgeJson(QString username) { if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QJsonDocument jdoc = QJsonDocument::fromJson(file.readAll()); QJsonArray users = jdoc.array(); int size = users.size(); for (int i=0;i<size;i++) { QJsonObject user = users.at(i).toObject(); qDebug() << user["username"]; if(user["username"] == username){ return false; } } } file.close(); return true; } bool jsonexe::editJson(QStringList sL) { qDebug() << "EditJson" << sL; // 获取已存在的users if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug() << "File opened failed"; return false; } jdoc = QJsonDocument::fromJson(file.readAll()); users = jdoc.array(); file.close(); // 验证信息,更改密码 QJsonArray newUsers; for (int i=0;i<users.size();i++) { QJsonObject userobj = users.at(i).toObject(); if(userobj["username"] == sL[0]){ if(sL.length() == 2){ QString acount = userobj["acount"].toString(); float total = acount.toFloat() + sL[1].toFloat(); userobj["acount"] = QString::number(total); qDebug() << "acount:" << userobj["acount"]; } else{ userobj["acount"] = sL[1]; userobj["score"] = sL[2]; userobj["password"] = sL[3]; } } newUsers.append(userobj); } //将newUsers重新写入文件 jdoc.setArray(newUsers); if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){ qDebug() << "File opened failed"; return false; } file.write(jdoc.toJson(QJsonDocument::Indented)); file.close(); return true; } bool jsonexe::appendJson(QStringList sL) { if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug() << "File opened failed"; return false; } jdoc = QJsonDocument::fromJson(file.readAll()); users = jdoc.array(); file.close(); if(this->judgeJson(sL[0])){ QJsonObject userobj; userobj["username"] = sL[0]; userobj["password"] = sL[1]; userobj["type"] = sL[2]; userobj["acount"] = 0; userobj["score"] = 0; users.append(userobj); jdoc.setArray(users); file.open(QIODevice::WriteOnly|QIODevice::Text); file.write(jdoc.toJson(QJsonDocument::Indented)); file.close(); return true; }else{ return false; } }
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值