[SOLVED] qcombobox and clear()



  • is there another way to clear the data in the qcombobox?

    i tried combo->clear(); but it gives error ASSERT failure in QVector<T>::operator[]: "index out of range", file /usr/include/qt4/QtCore/qvector.h, line 346 and when i comment the line, the process works perfectly except the new data is added to the previous.

    @
    QVector<QMap<QString, QString> > data = myquery_result;
    combo->clear(); // this line is giving the error..
    for (int i=0; i < data.size(); i++) {
    combo->addItem(data[i].value("name"));
    }
    @



  • Can you paste a full testcase? (That is, a minimal, self contained, compilable, correct snippet that shows the error).



  • I would venture a guess that the error is not on these lines of code.

    Most likely is something that you doing somewhere else while processing signals that get triggered by adding/removing items. For example you may have code to process when the selected item changes on the combo - clearing the combo and adding new items will probably trigger that.



  • ok here is the whole function :

    @
    void AddItemPurchaseOrderImpl::changeCategory(QString val)
    {
    bool firstrun = true;
    if (val == "New Category") {
    dropdownCategory->setEditable(true);
    } else {
    dropdownCategory->setEditable(false);
    QSqlDatabase db = dbaseAddItemPO.dbConnect();
    QVector<QMap<QString, QString> > products;
    if (val == "Uncategorized") {
    products = dbaseAddItemPO.getData("SELECT id, product_name FROM products WHERE category_id IS NULL");
    } else {
    QVector<QMap<QString, QString> > category = dbaseAddItemPO.getData("SELECT id FROM category WHERE category_name='" + val + "'");
    products = dbaseAddItemPO.getData("SELECT id, product_name FROM products WHERE category_id='" + category[0].value("id") + "'");
    }
    dropdownProduct->clear(); // if commented, there will be no error
    dropdownBrand->clear(); // if commented, there will be no error
    if (products.size() != 0) {
    for (int i=0; i < products.size(); i++) {
    dropdownProduct->addItem(products[i].value("product_name"));
    if (firstrun) {
    QVector<QMap<QString, QString> > brands = dbaseAddItemPO.getData("SELECT brand_name FROM product_details WHERE products_id='" + products[i].value("id") + "'");
    if (brands.size() != 0) {
    for (int j=0; j < brands.size(); j++) {
    dropdownBrand->addItem(brands[i].value("brand_name"));
    }
    }
    firstrun = false;
    }
    }
    }
    db.close();
    }
    dbaseAddItemPO.dbRemove();
    }
    @



  • What does your debugger say? You can break at the assertpoint to get a stack trace, or force Qt to crash on a failed assert (which would also get a stack trace). Follow that trace to the first point in your own code, and you'll be on to where the problem came from.



  • this is what my debugger says :

    Debug...
    Reading symbols from /directory/bin/icons...done.
    (gdb)
    (gdb) (gdb) (gdb) (gdb) Starting program: /directory/bin/icons
    [Thread debugging using libthread_db enabled]
    [New Thread 0x7fffe2e4c700 (LWP 27112)]
    [Thread 0x7fffe2e4c700 (LWP 27112) exited]
    Program received signal SIGABRT, Aborted.
    0x00007ffff489da75 in raise () from /lib/libc.so.6
    (gdb)
    QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: No such file or directory
    QFileSystemWatcher: failed to add paths: /home/my_profile/.config/ibus/bus
    Bus::open: Can not get ibus-daemon's address.
    IBusInputContext::createInputContext: no connection to ibus-daemon
    Object::connect: (sender name: 'AddItemPurchaseOrder')
    Object::connect: (receiver name: 'CreatePurchaseOrder')
    ASSERT failure in QVector<T>::operator[]: "index out of range", file /usr/include/qt4/QtCore/qvector.h, line 346

    then it crash.. need to force quit...



  • No, you need to get a stack trace. Gdb supplies the bt command for that.



  • how to get the stacktrace? i dont know how.. im using QDevelop & QTDesigner..



  • i even try only this :

    @
    void AddItemPurchaseOrderImpl::changeCategory(QString val)
    {
    if (val == "New Category") {
    dropdownCategory->setEditable(true);
    } else {
    dropdownProduct->clear();
    }
    }
    @

    and still gives me an error.. clearing-up the qcombobox is giving an error..



  • How do you fill the combo box?



  • this is how i fill it :
    dropdownProduct->addItem("Value");



  • [quote author="xeroblast" date="1294126914"]how to get the stacktrace? i dont know how.. im using QDevelop & QTDesigner..[/quote]

    I suggested two ways earlier. Anyhow, this is one (rather brute force) way to do it. When an assert is triggered, it will force a hard crash that you can catch in your debugger.

    @
    //in main.cpp
    void crashingMessageHandler(QtMsgType type, const char *msg)
    {
    switch (type) {
    case QtDebugMsg:
    fprintf(stderr, "Debug: %s\n", msg);
    break;
    case QtWarningMsg:
    fprintf(stderr, "Warning: %s\n", msg);
    break;
    case QtCriticalMsg:
    fprintf(stderr, "Critical: %s\n", msg);
    break;
    case QtFatalMsg:
    fprintf(stderr, "Fatal: %s\n", msg);
    __asm("int3");
    abort();
    }
    }

    int main(int argc, char ** argv)
    {
    qInstallMsgHandler(crashingMessageHandler);
    ...// rest of your main function
    }
    @

    It is a blunt axe, but it works (ok, depending on your compiler or platform I guess).

    After inserting the code above, make sure that you compile in debug mode, not in release mode. Then, in QtCreator, press F5 to start your debug run. Your program will start (though it will take more time!). Trigger the problem, and you will be able to see a stack trace: the table with the Level, Function, File and Line headers. Find the first file that contains code that you wrote, and you will see the function name and line number that triggered the problem.



  • this is the result that the debugger catch using the above code..

    Warning: Object::connect: (sender name: 'AddItemPurchaseOrder')
    Warning: Object::connect: (receiver name: 'CreatePurchaseOrder')
    Fatal: ASSERT failure in QVector<T>::operator[]: "index out of range", file /usr/include/qt4/QtCore/qvector.h, line 346
    Scope for 20:
    Symbol type is a variable with complex or multiple locations (DWARF2), length 4.
    Symbol msg is a variable with complex or multiple locations (DWARF2), length 8.

    i tried everything as simple like inputting manual data in the qcombobox. and if there is already item/s in the combobox then an error appear... if i dont put initial item/s, it will only run correctly in the first, on second select, the error appears again..



  • No, this is not the backtrace.

    You should really get the list of function calls that led to your crash.



  • that's what appears in the debugger.. btw, im using ubuntu..



  • If you are running your app in the debugger from the commandline, issue the bt command to gdb after the crash. From QtCreator, the backtrace should pop up automatically if a crash happens in debug mode.



  • im sorry but there is no popup.. im using QDevelop & QtDesigner in ubuntu..



  • QDevelop is not the same as QtCreator. And it is not a popup, it is part of the debug mode in that program.

    Try this, on the commandline:

    gdb path/to/your/executable

    let your application crash, and then, still on the gdb prompt:

    bt

    That should give you your stack/back trace.



  • im sorry but the gdb /path/to/exe is not working. no program running..

    Reading symbols from /home/edril/Documents/Desktop Projects/EquiApps/bin/EquiApps...done.
    (gdb)

    cursor is always blinking..



  • i found the problem...

    i disconnect first the signal in the dropdownProduct before using the clear().

    @
    void AddItemPurchaseOrderImpl::changeCategory(QString val)
    {
    disconnect(dropdownProduct, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeProduct(QString)));
    bool firstrun = true;
    dropdownProduct->clear();
    if (val == "New Category") {
    dropdownCategory->setEditable(true);
    } else {
    dropdownCategory->setEditable(false);
    QSqlDatabase db = dbaseAddItemPO.dbConnect();
    QVector<QMap<QString, QString> > products;
    if (val == "Uncategorized") {
    products = dbaseAddItemPO.getData("SELECT id, product_name FROM products WHERE category_id IS NULL");
    } else {
    QVector<QMap<QString, QString> > category = dbaseAddItemPO.getData("SELECT id FROM category WHERE category_name='" + val + "'");
    products = dbaseAddItemPO.getData("SELECT id, product_name FROM products WHERE category_id='" + category[0].value("id") + "'");
    }
    if (products.size() != 0) {
    for (int i=0; i < products.size(); i++) {
    dropdownProduct->addItem(products[i].value("product_name"));
    if (firstrun) {
    QVector<QMap<QString, QString> > brands = dbaseAddItemPO.getData("SELECT brand_name FROM product_details WHERE products_id='" + products[i].value("id") + "'");
    if (brands.size() != 0) {
    for (int j=0; j < brands.size(); j++) {
    dropdownBrand->addItem(brands[i].value("brand_name"));
    }
    }
    firstrun = false;
    }
    }
    }
    dropdownProduct->addItem("New Product");
    db.close();
    }
    dbaseAddItemPO.dbRemove();
    connect(dropdownProduct, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeProduct(QString)));
    }
    @

    and reconnect it at the end.
    thanx to all your replies..



  • Glad that you found the problem, but I would really like to advice you to learn some basic debugging skills. Problems of finding why an application crashes are something every developer will have to diagnose, sooner or later. Getting and reading & understanding a backtrace is a basic tool to fix those.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.