Qt Creator ComboBox crashes everytime and doesn't display a drop down menu
-
Everytime i try to connect to my SQL server to populate a Combobox the box and any other drop down widget no longer "drops down". This is the code im using
void mainwin::populateTableComboBox() { SqlFunc sqlFunc; QSqlDatabase db = sqlFunc.getDatabase(); if (db.isOpen()) { QSqlQuery query(db); query.exec("SELECT name FROM sys.tables WHERE type = 'U'"); combobox->clear(); while (query.next()) { combobox->addItem(query.value(0).toString()); } } else { qDebug() << "Failed to open the SQL Server connection."; } }
The second i run this code and this code only it breaks Combobox for the whole project and i need to create a new one, i've done this 6 times until now.
-
@Faris-Elbaz
If we are talking aQComboBox
, this code can’t break it.
What exactly happens, how and when? -
@Faris-Elbaz
Start by replacingcombobox->addItem(query.value(0).toString());
byqDebug() << query.value(0).toString();
and see how your program goes. Also check the return result from theexec()
. And trycombobox->addItem("Hello");
instead of yours. -
@Axel-Spoerl basically when i run this exact line of code, the combo box stops displaying a drop down menu entirely. I've made multiple new projects to narrow it down and it only happens whenever populateTableComboBox (the code i wrote above) runs. And even when i comment it out or remove it from the code entirely, no matter how many new ui files i make the combobox does not work correctly
-
@Faris-Elbaz
You want to start by isolating whether the issue lies in reading from the database or adding items to a combobox. If you try carefully each of my suggestions in reply above you should discover where the problem manifests. -
@JonB Hi, so i tried replacing the line with
qDebug() << query.value(0).toString(); <
The issue is not that my code doesn't run or that it doesn't open/build the application, the problem is that when i do build the application the drop down menu itself doesn't work.
@Axel-Spoerl
i only have a limited amount of times i can post per 5 minutes so here is some additional information, QODBC does not work for me. Im on macbook running my SQL server through docker to azure data studio, when i try to connect to the server using QODBC it doesn't work and i have to use a normal ODBC connection string to connect to it like thisbool SqlFunc::connOpen() { // Allocate the environment handle SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); // Allocate the connection handle SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); // Connection string // Replace the connection string with your actual connection details SQLCHAR* connectionString = (SQLCHAR*)"DRIVER={ODBC Driver 18 for SQL Server};SERVER=localhost;SqlFunc=HospitalDS;UID=sa;PWD=reallyStrongPwd123;Encrypt=no"; // Connect to the SQL Server SQLRETURN ret = SQLDriverConnect(hdbc, NULL, connectionString, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE); return ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO; } void SqlFunc::connClose() { if (hdbc!= nullptr) { SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); } if (henv!= nullptr) { SQLFreeHandle(SQL_HANDLE_ENV, henv); } }
From all the debugging and analysis i've been doing over the past 2-3 days I believe it has something to do with that and that also somehow the query is executing at the same time as the designer causing errors. This is just my theory.
-
@Faris-Elbaz said in Qt Creator ComboBox crashes everytime and doesn't display a drop down menu:
The issue is not that my code doesn't run or that it doesn't open/build the application
This is not what @JonB was suggesting. He suggested to do some debugging to see where the issue is. Why don't you follow his suggestions?
-