Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. App crashes clearing QComboBox
Forum Update on Monday, May 27th 2025

App crashes clearing QComboBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcombobox crashclear
9 Posts 4 Posters 4.1k Views
  • 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.
  • B Offline
    B Offline
    bclay
    wrote on 5 Jun 2016, 01:25 last edited by
    #1

    I have an app that queries a database for last names then populates a combobox. When the user selects a last name the first name combobox is supposed to clear and load first names that go with the selected last name. The first time through when the first name combo box is empty the clear has no effect. The second time through the app crashes. Both comboboxes are tied to slots so the clear firstname action in the lastname combobox callback is triggering the firstname slot while the lastname slot is still active. How can we clear the combo box without causing the crash. The code is shown below

    void GcfProjects::on_mLastNameList_currentIndexChanged(int index)
    {
    ui->mFirstNameList->clear();

    if (index >= 0)
    {
        QString name = ui->mLastNameList->itemText(index);
    
        QString errMsg;
        char tempBuf[255];
    
        QSqlQuery *searchQuery = new QSqlQuery(mSqlDatabase);
    
        std::string dbTableName = "gcf_2016.";
    
        dbTableName += mProjectToDbTableMap[mCurrentSpieces];
    
        QComboBox box;
    
        if (searchQuery != NULL)
        {
     //       if (dbTableName != NULL)
            {
                sprintf(tempBuf, "SELECT distinct first_name FROM %s "
                    "where last_name = '%s'", dbTableName.c_str(),
                    ui->mLastNameList->currentText().toStdString().c_str());
    
                searchQuery->prepare(tempBuf);
                if (!searchQuery->exec())
                {
                    QSqlError sqlError = searchQuery->lastError();
                    errMsg = sqlError.databaseText();
                    qDebug() << errMsg;
                }
                else
                {
                    if (searchQuery->isActive())
                    {
                        while (searchQuery->next())
                        {
                            QString name = searchQuery->record().value(0).toString();
    
                            ui->mFirstNameList->addItem(name);
                        }
    
                        searchQuery->finish();
                    }
                }
            }
    //		delete searchQuery;
        }
    }
    

    }

    void GcfProjects::on_mFirstNameList_currentIndexChanged(int index)
    {
    if (index >= 0)
    {
    QString name = ui->mLastNameList->itemText(index);
    }

    }

    K 1 Reply Last reply 5 Jun 2016, 11:42
    0
    • B bclay
      5 Jun 2016, 01:25

      I have an app that queries a database for last names then populates a combobox. When the user selects a last name the first name combobox is supposed to clear and load first names that go with the selected last name. The first time through when the first name combo box is empty the clear has no effect. The second time through the app crashes. Both comboboxes are tied to slots so the clear firstname action in the lastname combobox callback is triggering the firstname slot while the lastname slot is still active. How can we clear the combo box without causing the crash. The code is shown below

      void GcfProjects::on_mLastNameList_currentIndexChanged(int index)
      {
      ui->mFirstNameList->clear();

      if (index >= 0)
      {
          QString name = ui->mLastNameList->itemText(index);
      
          QString errMsg;
          char tempBuf[255];
      
          QSqlQuery *searchQuery = new QSqlQuery(mSqlDatabase);
      
          std::string dbTableName = "gcf_2016.";
      
          dbTableName += mProjectToDbTableMap[mCurrentSpieces];
      
          QComboBox box;
      
          if (searchQuery != NULL)
          {
       //       if (dbTableName != NULL)
              {
                  sprintf(tempBuf, "SELECT distinct first_name FROM %s "
                      "where last_name = '%s'", dbTableName.c_str(),
                      ui->mLastNameList->currentText().toStdString().c_str());
      
                  searchQuery->prepare(tempBuf);
                  if (!searchQuery->exec())
                  {
                      QSqlError sqlError = searchQuery->lastError();
                      errMsg = sqlError.databaseText();
                      qDebug() << errMsg;
                  }
                  else
                  {
                      if (searchQuery->isActive())
                      {
                          while (searchQuery->next())
                          {
                              QString name = searchQuery->record().value(0).toString();
      
                              ui->mFirstNameList->addItem(name);
                          }
      
                          searchQuery->finish();
                      }
                  }
              }
      //		delete searchQuery;
          }
      }
      

      }

      void GcfProjects::on_mFirstNameList_currentIndexChanged(int index)
      {
      if (index >= 0)
      {
      QString name = ui->mLastNameList->itemText(index);
      }

      }

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 5 Jun 2016, 11:42 last edited by kshegunov 6 May 2016, 11:42
      #2

      @bclay
      Not exactly an answer to your question, but QComboBox box; is a local variable (and you don't seem to use it).
      Also this:

      char tempBuf[255];
      // ...
      sprintf(tempBuf, "SELECT distinct first_name FROM %s "
                      "where last_name = '%s'", dbTableName.c_str(),
                      ui->mLastNameList->currentText().toStdString().c_str());
      

      is just terrible!
      Use the QString's API, which is also type safe and doesn't require all those type conversions. E.g:

      QString query = QStringLiteral("SELECT distinct first_name FROM %1 where last_name = '%2'").arg(dbTableName).arg(ui->mLastNameList->currentText());
      

      Or better yet, bind your variables to the query (as described in the docs) so they will also be properly escaped.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • B Offline
        B Offline
        bclay
        wrote on 5 Jun 2016, 23:33 last edited by
        #3

        Sorry to offend your senses. I have been coding for 35 years and mostly in C or C++. I have been coding Qt apps for only a year so the old school stuff comes out a lot.

        the "QComboBox box" is left over from some debugging and is not actually used at all. The app was created in QtCreator so the ui->mFirstNameList is specified in the auto-generated code.

        Still the question is how to get past the crash when I call clear on the combobox. I saw at one point something about disable but disabling the ui->mFirstNameList does not change anything and I do not see a way to get to the action to disable that. ui->mFirstNameList.action returns an empty list.

        K 1 Reply Last reply 6 Jun 2016, 07:14
        1
        • B bclay
          5 Jun 2016, 23:33

          Sorry to offend your senses. I have been coding for 35 years and mostly in C or C++. I have been coding Qt apps for only a year so the old school stuff comes out a lot.

          the "QComboBox box" is left over from some debugging and is not actually used at all. The app was created in QtCreator so the ui->mFirstNameList is specified in the auto-generated code.

          Still the question is how to get past the crash when I call clear on the combobox. I saw at one point something about disable but disabling the ui->mFirstNameList does not change anything and I do not see a way to get to the action to disable that. ui->mFirstNameList.action returns an empty list.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 6 Jun 2016, 07:14 last edited by
          #4

          @bclay

          Sorry to offend your senses.

          Not at all, I'm not that sensitive. :)

          Still the question is how to get past the crash when I call clear on the combobox.

          What do you get? Is it a failed assertion, segmentation fault? Would you provide a stack trace for the crash?

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bclay
            wrote on 7 Jun 2016, 02:03 last edited by
            #5

            The error message is "GcfProjects.exe has triggered a breakpoint."

            The stack trace shows it to be way down to Qt5Cored.dll and into ntdll.dl. Nothing in the stack trace shows the app that failed. In other words it one of Visual Studio's useless errors.

            K 1 Reply Last reply 7 Jun 2016, 16:10
            0
            • B bclay
              7 Jun 2016, 02:03

              The error message is "GcfProjects.exe has triggered a breakpoint."

              The stack trace shows it to be way down to Qt5Cored.dll and into ntdll.dl. Nothing in the stack trace shows the app that failed. In other words it one of Visual Studio's useless errors.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 7 Jun 2016, 16:10 last edited by
              #6

              @bclay
              I don't see anything really suspicious in the code. However, I'm not a machine and will sometimes miss things, that's why I requested the stack. Anyway, perhaps someone else will find what I've missed.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bclay
                wrote on 8 Jun 2016, 02:23 last edited by
                #7

                I found what I was looking for but that did not fix the issue either. Basically I wrapped the calls to clear in blockSignal calls such as the following

                bool oldState = ui->mLastNameList->blockSignals(true);
                ui->mLastNameList->clear();
                ui->mLastNameList->blockSignals(oldState);
                

                Even though the signal was blocked the app still crashed then clear was called.

                Thanks for your suggestions.

                Bruce

                the_T 1 Reply Last reply 8 Jun 2016, 05:36
                0
                • B bclay
                  8 Jun 2016, 02:23

                  I found what I was looking for but that did not fix the issue either. Basically I wrapped the calls to clear in blockSignal calls such as the following

                  bool oldState = ui->mLastNameList->blockSignals(true);
                  ui->mLastNameList->clear();
                  ui->mLastNameList->blockSignals(oldState);
                  

                  Even though the signal was blocked the app still crashed then clear was called.

                  Thanks for your suggestions.

                  Bruce

                  the_T Offline
                  the_T Offline
                  the_
                  wrote on 8 Jun 2016, 05:36 last edited by
                  #8

                  @bclay
                  One thing you could try is to manually add breakpoints and step through in debug mode to see how far your code goes and where it triggers the crash.

                  -- No support in PM --

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    ZacDu
                    wrote on 1 Aug 2023, 02:28 last edited by
                    #9

                    When clear() function is called, currentIndexChanged(int index) function also will be called with index = -1. So if you uses index at some variables, it cause the memory faults. So you have to add the following code at the very first line.

                    if(index <0) return;

                    I hope this will be helpful.

                    1 Reply Last reply
                    0

                    • Login

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