我一直在我的程序中使用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);
}
答案 0 :(得分:2)
我想我已经设法重现了你的问题。
在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");
因此,当您选择公司名称或公司所有者时,索引超出范围。