QList::at 中的 Qt 断言失败<T>:"索引超出范围

Qt ASSERT failure in QList<T>::at: "index out of range

本文关键字:gt 范围 索引 lt 中的 at Qt 断言 失败 QList      更新时间:2023-10-16

我一直在我的程序中使用QString列表,但是当我第二次更改组合框的索引时,它们会中断(中断是指程序崩溃),我不确定为什么。 唯一使用 QStringLists 的代码是

void Search::on_comboBox_Tabel_Select_currentIndexChanged(int index)
{
    QStringList customer = (QStringList() << "Customer_ID" << "Company Name" << "City" << "Phone Number" << "Street Adress"
                                          << "County" << "BULSTAT" << "Company Owner" << "Account Since");
    QStringList invoice  = (QStringList() << "Invoice Number" << "Date Time" << "Total Purchased" << "Company Name" << "Company Owner");
    QStringList product  = (QStringList() << "Product Code" << "Product Name" << "Product Quantity"
                                          << "Reorder Level" << "Max Product Quantity" << "Product Purchase Cost"
                                          << "Selling Price" << "Selling Price VAT");
    switch(index)
    {
        case 0:
        {
            ui->comboBox_select->clear();
            ui->comboBox_select->addItems(customer);
            break;
        }
        case 1:
        {
            ui->comboBox_select->clear();
            ui->comboBox_select->addItems(invoice);
            break;
        }
        case 2:
        {
            ui->comboBox_select->clear();
            ui->comboBox_select->addItems(product);
            break;
        }
    }
}

void Search::CreatQuery()
{
    QStringList customer = (QStringList() << "Customer_ID" << "Company_Name" << "City" << "Phone_Number" << "Street_Adress"
                                          << "County" << "BULSTAT" << "Company_Owner" << "Account_Since");
    QStringList invoice  = (QStringList() << "Invoice_Number" << "Date_Time" << "Total_Purchased");
    QStringList product  = (QStringList() << "Product_CODE" << "Product_Name" << "ProductQ"
                                          << "Reorder_Level" << "Max_Product_Quantity" << "Product_Purchase_Cost"
                                          << "Selling_Price" << "Selling_Price_VAT");
    QStringList tabel    = (QStringList() << "Customer" << "Invoice" << "Product");
    int tabelIndex = ui->comboBox_Tabel_Select->currentIndex();
    int columnIndex = ui->comboBox_select->currentIndex();
    QString TabelData = tabel.at(tabelIndex).toLocal8Bit().constData();
    QString ColumnData;
    QString Parameter = ui->lineEdit->text();
    switch (tabelIndex)
    {
        case 0:
            ColumnData = customer.at(columnIndex).toLocal8Bit().constData();
            break;
        case 1:
            ColumnData = invoice.at(columnIndex).toLocal8Bit().constData();
            break;
        case 2:
            ColumnData = product.at(columnIndex).toLocal8Bit().constData();
            break;
    }
    if(ButtonPressed == "BETWEEN")
    {
        QString ParameterBetween = ui->lineEdit_between->text();
        query = "SELECT * FROM " + TabelData  + " WHERE " + ColumnData + " BETWEEN " + Parameter + " AND " + ParameterBetween;
    }
    else
    {
        query = "SELECT * FROM " + TabelData  + " WHERE " + ColumnData + " " + ButtonPressed + " " + Parameter;
    }
    ui->textEdit->setText(query);
}
我想

我已经设法重现了您的问题。

on_comboBox_Tabel_Select_currentIndexChanged有一行:

QStringList invoice  = (QStringList() << "Invoice Number"
                                      << "Date Time"
                                      << "Total Purchased"
                                      << "Company Name" 
                                      << "Company Owner");

CreatQuery有一行:

QStringList invoice  = (QStringList() << "Invoice_Number"
                                      << "Date_Time"
                                      << "Total_Purchased");

因此,当您选择"公司名称"或"公司所有者"时,索引超出了范围。