-2

I have been using QString lists in my program but they are breaking when i change the index of my combobox for a second time (By Breaking I mean that the program is crashing) and i'm unsure why. the only code that is using QStringLists is

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;
        }
    }

}

and

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);
}
Root0x
  • 113
  • 1
  • 2
  • 12
  • *How* is it "breaking"? Can you please explain the actual and the expected behavior? – Some programmer dude Dec 29 '14 at 16:23
  • @Root0x That didn't improve much :-P ... – πάντα ῥεῖ Dec 29 '14 at 16:27
  • what would you like to know specifically? – Root0x Dec 29 '14 at 16:28
  • What's the range of `index` coming from the combobox? – Tay2510 Dec 29 '14 at 16:30
  • 2
    @Root0x Where exactly the program stops, when you're stepping through with the debugger? What are the actual index values you used, and how you are sure about that these aren't (shouldn't) go out of bounds? – πάντα ῥεῖ Dec 29 '14 at 16:30
  • @Tay2510 the range of values in the index of my combobox are 0-2 – Root0x Dec 29 '14 at 16:32
  • What about `ui->comboBox_Tabel_Select` and `ui->comboBox_select` ? – Tay2510 Dec 29 '14 at 16:36
  • comboBox_Tabel_Select has 0-2 and comboBox_select has changing contents depending on what is selected from tabel select, but its from 0-4 to 0-8 – Root0x Dec 29 '14 at 16:39
  • Which combination(s) fail? Did you debug it? Your program referred something not in the range. – Tay2510 Dec 29 '14 at 16:43
  • The program works when i change the index of comboBox_Tabel_Select for the first time but when i change it for a second time the program crashes – Root0x Dec 29 '14 at 16:45
  • 3
    @Root0x Are you using the debugger? Why does it sound like you're not, and only giving us what you surmise are the ranges? The error suggests that you are out of range, but you have yet to post what the ranges actually are, and what the number is that falls out of the range. – PaulMcKenzie Dec 29 '14 at 16:54

1 Answers1

2

I think I have managed to reproduce your issue.

in on_comboBox_Tabel_Select_currentIndexChanged there is a line:

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

In CreatQuery there is a line:

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

So when you pick Company Name or Company Owner the index is out of range.

luantkow
  • 2,809
  • 20
  • 14