首页 > 代码库 > Qt日常备注(函数/接口实现)

Qt日常备注(函数/接口实现)

1.判断QString是否为纯数字

2.查找自身最长重复子字符串

3.树形列表复选框操作

4.更改文件权限

 

//-----实现-----

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 }

 

Qt日常备注(函数/接口实现)