[SOLVED] qcombobox and clear()
-
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 346then it crash.. need to force quit...
-
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..
-
[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..
-
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.
-
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.
-
@fcrochik said in [SOLVED] qcombobox and clear():
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.
Yes you are right. I also facing the same problem.