Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt Creator ComboBox crashes everytime and doesn't display a drop down menu

Qt Creator ComboBox crashes everytime and doesn't display a drop down menu

Scheduled Pinned Locked Moved Qt Creator and other tools
7 Posts 4 Posters 938 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Faris Elbaz
    wrote on last edited by
    #1

    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.

    Axel SpoerlA JonBJ 2 Replies Last reply
    0
    • F Faris Elbaz

      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.

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      @Faris-Elbaz
      If we are talking a QComboBox, this code can’t break it.
      What exactly happens, how and when?

      Software Engineer
      The Qt Company, Oslo

      F 1 Reply Last reply
      2
      • F Faris Elbaz

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Faris-Elbaz
        Start by replacing combobox->addItem(query.value(0).toString()); by qDebug() << query.value(0).toString(); and see how your program goes. Also check the return result from the exec(). And try combobox->addItem("Hello"); instead of yours.

        1 Reply Last reply
        3
        • Axel SpoerlA Axel Spoerl

          @Faris-Elbaz
          If we are talking a QComboBox, this code can’t break it.
          What exactly happens, how and when?

          F Offline
          F Offline
          Faris Elbaz
          wrote on last edited by
          #4

          @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

          JonBJ 1 Reply Last reply
          0
          • F Faris Elbaz

            @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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            1
            • F Offline
              F Offline
              Faris Elbaz
              wrote on last edited by
              #6

              @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 this

              bool 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.

              jsulmJ 1 Reply Last reply
              0
              • F Faris Elbaz

                @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 this

                bool 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.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • F Faris Elbaz marked this topic as a regular topic on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved