1.判断QString是否为纯数字
2.查找自身最长重复子字符串
3.树形列表复选框操作
4.更改文件权限
5.判断系统64位
6.文件生成md5值
7.版本号比较(字符串比较)
//-----实现-----
1.判断QString是否为纯数字
bool IsDigitString(QString strSource)
{
bool bDigit = false; if (strSource.isEmpty())
{
return bDigit;
} QByteArray strByteArray = strSource.toLatin1();
const char *chString = strByteArray.data(); while (*chString && *chString>='' && *chString<='') chString++; if (*chString)
{
bDigit = false;
}
else
{
bDigit = true;
} return bDigit;
}
2.查找自身最长重复子字符串
QString MaxSubString(QString inputString)
{
QString subString = "";
int strLen = inputString.size(); if (strLen <= )
{
return subString;
} int maxSubStrLen = ;
int tempLen = ;
int subIndex = ;
int i = ;
int j = ;
int k = ; while (i < strLen)
{
j = i+;
while(j < strLen)
{
if (inputString.at(i) == inputString.at(j))
{
tempLen = ;
for (k=; j+k<strLen && inputString.at(i+k)==inputString.at(j+k); k++)
{
tempLen++;
} if (tempLen > maxSubStrLen)
{
subIndex = i;
maxSubStrLen = tempLen;
} j += tempLen;
}
else
{
j++;
}
}
i++;
} for (i=; i<maxSubStrLen; i++)
{
subString.append(inputString.at(subIndex+i));
} return subString;
}
3.树形列表复选框操作
void CCheckBoxDialog::treeItemsChangeStol(QTreeWidgetItem *pCurrentItem, int)
{
if (m_itemsCount <= )//没有子节点,不存在选中操作
{
updateComboInfoStol();
return;
} if (Qt::Checked == pCurrentItem->checkState())
{
//QTreeWidgetItem *parentItem = pCurrentItem->parent(); int childCount = pCurrentItem->childCount();
if (childCount >)//父节点被选中
{
for (int i=; i<childCount; i++)
{
pCurrentItem->child(i)->setCheckState(, Qt::Checked);
}
}
else //更新父节点复选框状态
{
updateParentItemStateStol(pCurrentItem);
}
}
else if (Qt::Unchecked == pCurrentItem->checkState())
{
//QTreeWidgetItem *parentItem = pCurrentItem->parent(); int childCount = pCurrentItem->childCount();
if (childCount >)//父节点被选中
{
for (int i=; i<childCount; i++)
{
pCurrentItem->child(i)->setCheckState(, Qt::Unchecked);
}
}
else //更新父节点复选框状态
{
updateParentItemStateStol(pCurrentItem);
}
}
updateComboInfoStol();
}
void CCheckBoxDialog::updateParentItemStateStol(QTreeWidgetItem *pCurrentItem)
{
QTreeWidgetItem *parentItem = pCurrentItem->parent();
if (parentItem == NULL)
{
return;
} int selectedCount = ;//当前子节点选中个数
int childCount = parentItem->childCount();
for (int i=; i<childCount; i++)
{
QTreeWidgetItem *childItem = parentItem->child(i);
if (Qt::Checked == childItem->checkState())
{
selectedCount++;
}
} if (selectedCount <= )
{
//未选中任何项
parentItem->setCheckState(, Qt::Unchecked);
}
else if (selectedCount> && selectedCount<childCount)
{
//选中部分子项
parentItem->setCheckState(, Qt::PartiallyChecked);
}
else if (selectedCount == childCount)
{
//选中所有子项
parentItem->setCheckState(, Qt::Checked);
}
}
4.更改文件权限
static void ChangeFilePermission( const QString &filePath )
{
#ifdef WIN32
QString cmd("icacls.exe \"") ;
cmd += filePath;
cmd += "\" /grant Everyone:(F)";
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si); #ifndef _DEBUG
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
#endif ZeroMemory(&pi, sizeof(pi));
CreateProcessW (NULL, // No module name (use command line).
(wchar_t*)cmd.utf16(), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi); // Pointer to PROCESS_INFORMATION structure. WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
#endif
}
5.判断系统64位
static bool IsWin64 (void)
{
SYSTEM_INFO si = {0};
typedef void (WINAPI *LPFN_PGNSI)(LPSYSTEM_INFO);
LPFN_PGNSI pGNSI = (LPFN_PGNSI)GetProcAddress(GetModuleHandleA(("kernel32.dll")),"GetNativeSystemInfo"); if (pGNSI) pGNSI(&si); if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
return true;
}
return false;
}
6.文件生成md5值
static bool GetFileMd5Code(const QString strFilePath, QString &strMD5Code)
{
bool bRet = false;
strMD5Code = ""; if (!QFile::exists(strFilePath))
{
qDebug() << strFilePath << " don't exist!";
return bRet;
} QFile file(strFilePath);
if (file.open(QIODevice::ReadOnly))
{
QCryptographicHash cryptographicHash(QCryptographicHash::Md5);
cryptographicHash.addData(&file);
strMD5Code = QString::fromLatin1(cryptographicHash.result().toHex());
file.close();
bRet = true;
} qDebug() << "md5:" << strMD5Code;
return bRet;
} qDebug() << "md5:" << strMD5Code;
return bRet;
}
QByteArray块数据生成Md5
QByteArray GetByteArrayMd5(const QByteArray &byteArrayData)
{
return QCryptographicHash::hash(byteArrayData, QCryptographicHash::Md5).toHex();
}
7.版本号比较(字符串比较)
int CompareVersion(QString strVer1, QString strVer2)
{
if ( !strVer1.compare( strVer2 ) )
{
return ;
} QStringList list1 = strVer1.split( "." );
QStringList list2 = strVer2.split( "." ); int iTotal1 = list1.count();
int iTotal2 = list2.count(); int iTotal=iTotal1>iTotal2?iTotal2:iTotal1; int iValue1 = , iValue2 = ;
bool ibOK1 = false, ibOK2 = false;
for ( int iNum = ; iNum < iTotal; ++iNum )
{
iValue1 = list1[iNum].toInt( &ibOK1 );
if (!ibOK1)
{
iValue1=;
} iValue2 = list2[iNum].toInt( &ibOK2 );
if (!ibOK2)
{
iValue2=;
} if (iValue1==iValue2)
{
continue;
}
else if ( iValue1 < iValue2 )
{
return -;
}
else
{
return ;
}
} return iTotal1<iTotal2?-:;
}
测试:
CompareVersion("16.01.19", "16.01.19") //return 0;
CompareVersion("16.01.19", "16.01.18") //return 1;
CompareVersion("16.01.18", "16.01.19") //return -1;