在我的C++应用程序中,我必须选择带有QFileDialog类的文件。
然后,我验证我的文件名正确(必须以“VG”开头)。

我的文件具有以下结构:VGx-xx-xxxx-xxx-xxx.pigs

之后,我在关联的QLineEdit中进行设置。
但是每次我选择一个好的文件时,它都会崩溃,而且我也不知道为什么。

这是我的不同功能:

打开QFILE对话框

/** OPEN FILE DIALOG WINDOW  **/
void VGCCC::selectPIGSFile()
{
    QString pigsFile = QFileDialog::getOpenFileName
    (
        this,
        tr("Select PIGS file"),
        "./../VGCColorConfigurator/Inputs",
        tr("Document files (*.pigs *.igs)"),
        0,
        QFileDialog::DontUseNativeDialog
    );

    pigsPath = pigsFile;
    if(verifyPIGSFileValidity(pigsPath.toStdString()))
    {
        m_filePathLine->setText("");
        m_filePathLine->setText(pigsPath);
        m_testTextEdit->insertPlainText("File selected : "+pigsPath+"\n");
    }
    else
    {
        m_filePathLine->setText("Please select a valid PIGS (Format VGx-xx-xxxx-xxx-xxx.pigs)");
        m_testTextEdit->insertPlainText("Uncorrect PIGS file.\n");
    }
}

验证文件名
/** VERIFY SELECTED PIG FILE **/
bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
    splitPIGSName(pigsPath);
    std::string verification = pigsNameTable[0].erase(2,2);
    std::string headerName = "VG";

    if(!verification.compare(headerName))
    {
        m_testTextEdit->insertPlainText("PIGS name is correct");
        return true;
    }
    else
        return false;
}

拆分方法
/** SPLIT PIGS NAME INTO TABLE **/
std::string* VGCCC::splitPIGSName(std::string pigsPath)
{
    std::string pigsPathToSplit = pigsPath;
    std::string delimiter = "-";
    size_t position = 0;
    int i=0;
    std::string token;

    while ((position = pigsPathToSplit.find(delimiter)) != std::string::npos)
    {
        token = pigsPathToSplit.substr(0, position);
        std::cout << token << std::endl;
        pigsNameTable[i] = token;
        i++;
        pigsPathToSplit.erase(0, position + delimiter.length());
    }
    pigsNameTable[4] = pigsPathToSplit.c_str();
    std::cout << pigsPathToSplit << std::endl;
}

最佳答案

bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
    splitPIGSName(pigsPath);
    std::string verification = pigsNameTable[0].erase(2,2);
    std::string headerName = "VG";

    if(!verification.compare(headerName))
    {
        m_testTextEdit->insertPlainText("PIGS name is correct");
        return true;
    }
    else
        return false;
}

不安全是因为:

1-您不检查pigsNameTable是否在索引(如果是vector?)或键(如果是map?)上有元素0
2-您不检查pigsNameTable[0]是否包含2个以上的元素。参见erase documentation:



您可以简单地执行以下操作:
bool VGCCC::verifyPIGSFileValidity(std::string pigsPath)
{
    splitPIGSName(pigsPath);

    if ( /* test is pigsNameTable[0] exists depending on pigsNameTable's type */ )
    {
        return pigsNameTable[0].find( "VG" ) == 0; // return true if pigsNameTable[0] starts with "VG"
    }
    else
    {
        return false;
    }
}

如果pigsNameTablevector,则测试可以是!pigsNameTable.empty(),如果它是mappigsNameTable.find(0) != pigsNameTable.end()...。

关于c++ - 设置QLineEdit时std::out_of_range,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34980199/

10-09 18:33