Qt日常备注(函数/接口实现)
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>='0' && *chString<='9') chString++; if (*chString) { bDigit = false; } else { bDigit = true; } return bDigit; }
2.查找自身最长重复子字符串
QString MaxSubString(QString inputString) { QString subString = ""; int strLen = inputString.size(); if (strLen <= 0) { return subString; } int maxSubStrLen = 0; int tempLen = 0; int subIndex = 0; int i = 0; int j = 0; int k = 0; while (i < strLen) { j = i+1; while(j < strLen) { if (inputString.at(i) == inputString.at(j)) { tempLen = 1; for (k=1; 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=0; i<maxSubStrLen; i++) { subString.append(inputString.at(subIndex+i)); } return subString; }
3.树形列表复选框操作
void CCheckBoxDialog::treeItemsChangeStol(QTreeWidgetItem *pCurrentItem, int) { if (m_itemsCount <= 0)//没有子节点,不存在选中操作 { updateComboInfoStol(); return; } if (Qt::Checked == pCurrentItem->checkState(0)) { //QTreeWidgetItem *parentItem = pCurrentItem->parent(); int childCount = pCurrentItem->childCount(); if (childCount >0)//父节点被选中 { for (int i=0; i<childCount; i++) { pCurrentItem->child(i)->setCheckState(0, Qt::Checked); } } else //更新父节点复选框状态 { updateParentItemStateStol(pCurrentItem); } } else if (Qt::Unchecked == pCurrentItem->checkState(0)) { //QTreeWidgetItem *parentItem = pCurrentItem->parent(); int childCount = pCurrentItem->childCount(); if (childCount >0)//父节点被选中 { for (int i=0; i<childCount; i++) { pCurrentItem->child(i)->setCheckState(0, Qt::Unchecked); } } else //更新父节点复选框状态 { updateParentItemStateStol(pCurrentItem); } } updateComboInfoStol(); }
void CCheckBoxDialog::updateParentItemStateStol(QTreeWidgetItem *pCurrentItem) { QTreeWidgetItem *parentItem = pCurrentItem->parent(); if (parentItem == NULL) { return; } int selectedCount = 0;//当前子节点选中个数 int childCount = parentItem->childCount(); for (int i=0; i<childCount; i++) { QTreeWidgetItem *childItem = parentItem->child(i); if (Qt::Checked == childItem->checkState(0)) { selectedCount++; } } if (selectedCount <= 0) { //未选中任何项 parentItem->setCheckState(0, Qt::Unchecked); } else if (selectedCount>0 && selectedCount<childCount) { //选中部分子项 parentItem->setCheckState(0, Qt::PartiallyChecked); } else if (selectedCount == childCount) { //选中所有子项 parentItem->setCheckState(0, 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 0; } QStringList list1 = strVer1.split( "." ); QStringList list2 = strVer2.split( "." ); int iTotal1 = list1.count(); int iTotal2 = list2.count(); int iTotal=iTotal1>iTotal2?iTotal2:iTotal1; int iValue1 = 0, iValue2 = 0; bool ibOK1 = false, ibOK2 = false; for ( int iNum = 0; iNum < iTotal; ++iNum ) { iValue1 = list1[iNum].toInt( &ibOK1 ); if (!ibOK1) { iValue1=0; } iValue2 = list2[iNum].toInt( &ibOK2 ); if (!ibOK2) { iValue2=0; } if (iValue1==iValue2) { continue; } else if ( iValue1 < iValue2 ) { return -1; } else { return 1; } } return iTotal1<iTotal2?-1:1; }
测试:
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;
ps:本博文只用于日常备注及学习交流,请勿用于任何商业用途,涉及网摘等资料如有侵犯原作者权利,请联系确保及时更正!