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. comboBox->additem() crash application..
Forum Update on Monday, May 27th 2025

comboBox->additem() crash application..

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 6 Posters 4.6k 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.
  • TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by TheCipo76
    #1

    Hi,
    i've populated from database n.1 comboBox and all is OK

    if i click on Annulla pushbutton (this is the code):

    void inserisciarticolo::on_pushButton_Annulla_clicked()
    {
        // ANNULLA
        if (aDatabase.open()) {
            aDatabase.close();
        }
        close();
    }
    

    and try to re-load the dialog (from application menu)

    after first record of database additem() crashes the application

    if i comment the additem() all works

    this is the code i've used to load the comboBox data:

    QSqlQuery cl;
        cl.prepare("Select RAGSOC from CLIENTI order by RAGSOC ASC");
        ui->comboBox_Cliente->clear();
        if (cl.exec()) {
            cl.first();
            do {
                ui->comboBox_Cliente->addItem(cl.value(0).toString());
            } while (cl.next());
        }
    

    ideas??

    Thanks in advance.

    mrjjM JonBJ 2 Replies Last reply
    0
    • TheCipo76T TheCipo76

      Hi,
      i've populated from database n.1 comboBox and all is OK

      if i click on Annulla pushbutton (this is the code):

      void inserisciarticolo::on_pushButton_Annulla_clicked()
      {
          // ANNULLA
          if (aDatabase.open()) {
              aDatabase.close();
          }
          close();
      }
      

      and try to re-load the dialog (from application menu)

      after first record of database additem() crashes the application

      if i comment the additem() all works

      this is the code i've used to load the comboBox data:

      QSqlQuery cl;
          cl.prepare("Select RAGSOC from CLIENTI order by RAGSOC ASC");
          ui->comboBox_Cliente->clear();
          if (cl.exec()) {
              cl.first();
              do {
                  ui->comboBox_Cliente->addItem(cl.value(0).toString());
              } while (cl.next());
          }
      

      ideas??

      Thanks in advance.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Check if
      cl.first(); returns true before using it.

      TheCipo76T 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Check if
        cl.first(); returns true before using it.

        TheCipo76T Offline
        TheCipo76T Offline
        TheCipo76
        wrote on last edited by TheCipo76
        #3

        @mrjj

        if (cl.exec()) {
                msg.setText("QUI");
                msg.exec();
                cl.first();
                if (cl.first()) {
                        msg.setText("OK");
                        msg.exec();
                }
                do {
                    ui->comboBox_Cliente->addItem(cl.value(0).toString());
                } while (cl.next());
            }
        

        messagebox will appear with OK message

        if i replace additem with messagebox all works correctly

        mrjjM 1 Reply Last reply
        0
        • TheCipo76T TheCipo76

          @mrjj

          if (cl.exec()) {
                  msg.setText("QUI");
                  msg.exec();
                  cl.first();
                  if (cl.first()) {
                          msg.setText("OK");
                          msg.exec();
                  }
                  do {
                      ui->comboBox_Cliente->addItem(cl.value(0).toString());
                  } while (cl.next());
              }
          

          messagebox will appear with OK message

          if i replace additem with messagebox all works correctly

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Super.
          Try
          QString test=cl.value(0).toString();
          and see if it crash on that line instead.
          else it must mean
          ui->comboBox_Cliente is not valid pointer.

          TheCipo76T 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            I know this does not address your problem but it should solve it anyway. Replace all your code with:

            QSqlQueryModel* queryModel = new QSqlQueryModel(this);
            queryModel.setQuery(QStringLiteral("Select RAGSOC from CLIENTI order by RAGSOC ASC"));
            if(dynamic_cast<QSqlQueryModel*>(ui->comboBox_Cliente->model()))
            ui->comboBox_Cliente->model()->deleteLater();
            ui->comboBox_Cliente->setModel(queryModel);
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            TheCipo76T 1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              Super.
              Try
              QString test=cl.value(0).toString();
              and see if it crash on that line instead.
              else it must mean
              ui->comboBox_Cliente is not valid pointer.

              TheCipo76T Offline
              TheCipo76T Offline
              TheCipo76
              wrote on last edited by
              #6

              @mrjj i've tried and crash on additem()..

              comboBox is not a pointer .. i've create it with designer in the ui

              Cobra91151C 1 Reply Last reply
              0
              • VRoninV VRonin

                I know this does not address your problem but it should solve it anyway. Replace all your code with:

                QSqlQueryModel* queryModel = new QSqlQueryModel(this);
                queryModel.setQuery(QStringLiteral("Select RAGSOC from CLIENTI order by RAGSOC ASC"));
                if(dynamic_cast<QSqlQueryModel*>(ui->comboBox_Cliente->model()))
                ui->comboBox_Cliente->model()->deleteLater();
                ui->comboBox_Cliente->setModel(queryModel);
                
                TheCipo76T Offline
                TheCipo76T Offline
                TheCipo76
                wrote on last edited by
                #7

                @VRonin it will crash however

                1 Reply Last reply
                0
                • TheCipo76T TheCipo76

                  @mrjj i've tried and crash on additem()..

                  comboBox is not a pointer .. i've create it with designer in the ui

                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by
                  #8

                  @TheCipo76

                  Hi! Actually in designer it creates pointers, ui-> is the namespace, and comboBox_Cliente-> is a C++ pointer. What is the error message when it crashes?

                  TheCipo76T 1 Reply Last reply
                  0
                  • Cobra91151C Cobra91151

                    @TheCipo76

                    Hi! Actually in designer it creates pointers, ui-> is the namespace, and comboBox_Cliente-> is a C++ pointer. What is the error message when it crashes?

                    TheCipo76T Offline
                    TheCipo76T Offline
                    TheCipo76
                    wrote on last edited by TheCipo76
                    #9

                    @Cobra91151

                    in application output :

                    18:45:34: The process was ended forcefully.
                    18:45:34: /Users/matteo/Desktop/C++/QT/EasyGest/build-EasyGest-Desktop_Qt_5_11_2_clang_64bit18-Release/EasyGest.app/Contents/MacOS/EasyGest crashed.

                    Cobra91151C 1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      in the constructor of your class, can you add QObject::connect(ui->comboBox_Cliente,&Qobject::destroyed,[]()->void{qDebug("Something exploded");});?

                      If you see the output it means you are deleting your ui before reusing it

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      TheCipo76T VRoninV 2 Replies Last reply
                      0
                      • VRoninV VRonin

                        in the constructor of your class, can you add QObject::connect(ui->comboBox_Cliente,&Qobject::destroyed,[]()->void{qDebug("Something exploded");});?

                        If you see the output it means you are deleting your ui before reusing it

                        TheCipo76T Offline
                        TheCipo76T Offline
                        TheCipo76
                        wrote on last edited by
                        #11

                        @VRonin said in comboBox->additem() crash application..:

                        QObject::connect(ui->comboBox_Cliente,&Qobject::destroyed,->void{qDebug("Something exploded");});

                        i don't understand where i have to put it..

                        in the .h file

                        after Q_OBJECT ??

                        1 Reply Last reply
                        0
                        • VRoninV VRonin

                          in the constructor of your class, can you add QObject::connect(ui->comboBox_Cliente,&Qobject::destroyed,[]()->void{qDebug("Something exploded");});?

                          If you see the output it means you are deleting your ui before reusing it

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          @VRonin said in comboBox->additem() crash application..:

                          in the constructor of your class

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          TheCipo76T 1 Reply Last reply
                          0
                          • VRoninV VRonin

                            @VRonin said in comboBox->additem() crash application..:

                            in the constructor of your class

                            TheCipo76T Offline
                            TheCipo76T Offline
                            TheCipo76
                            wrote on last edited by TheCipo76
                            #13

                            @VRonin ok..

                            no output were showed

                            i've put it after

                            ui->setupUi(this);
                            

                            in the dialog

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #14

                              And it still crashes? did you set Qt::WA_DeleteOnClose?

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              TheCipo76T 1 Reply Last reply
                              0
                              • VRoninV VRonin

                                And it still crashes? did you set Qt::WA_DeleteOnClose?

                                TheCipo76T Offline
                                TheCipo76T Offline
                                TheCipo76
                                wrote on last edited by
                                #15

                                @VRonin No, i don't know what is it.. sorry but i'm new user of Qt

                                1 Reply Last reply
                                0
                                • VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #16

                                  One more try, add QObject::connect(this,&QObject::destroyed,[]()->void{qDebug("Something big exploded");}); and see if you have any output when you crash the application

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  TheCipo76T 1 Reply Last reply
                                  0
                                  • VRoninV VRonin

                                    One more try, add QObject::connect(this,&QObject::destroyed,[]()->void{qDebug("Something big exploded");}); and see if you have any output when you crash the application

                                    TheCipo76T Offline
                                    TheCipo76T Offline
                                    TheCipo76
                                    wrote on last edited by
                                    #17

                                    @VRonin said in comboBox->additem() crash application..:

                                    QObject::connect(this,&QObject::destroyed,->void{qDebug("Something big exploded");});

                                    no, i don't see any output..

                                    1 Reply Last reply
                                    0
                                    • VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #18

                                      This is turning into a mistery now. Can you post the stack trace at the moment of crash?

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      TheCipo76T 1 Reply Last reply
                                      2
                                      • TheCipo76T TheCipo76

                                        @Cobra91151

                                        in application output :

                                        18:45:34: The process was ended forcefully.
                                        18:45:34: /Users/matteo/Desktop/C++/QT/EasyGest/build-EasyGest-Desktop_Qt_5_11_2_clang_64bit18-Release/EasyGest.app/Contents/MacOS/EasyGest crashed.

                                        Cobra91151C Offline
                                        Cobra91151C Offline
                                        Cobra91151
                                        wrote on last edited by Cobra91151
                                        #19

                                        @TheCipo76

                                        This crash happens after you addItem() to the ui->comboBox_Cliente?

                                        Try to change the code to:

                                        if (cl.exec()) {
                                            msg.setText("QUI");
                                            msg.exec();
                                        
                                            if (cl.first()) {
                                                 msg.setText("OK");
                                                 msg.exec();
                                            }
                                        
                                            do {
                                                ui->comboBox_Cliente->addItem(cl.value(0).toString());
                                            } while (cl.next());
                                         }
                                        

                                        and check if it still crashes?

                                        1 Reply Last reply
                                        0
                                        • TheCipo76T TheCipo76

                                          Hi,
                                          i've populated from database n.1 comboBox and all is OK

                                          if i click on Annulla pushbutton (this is the code):

                                          void inserisciarticolo::on_pushButton_Annulla_clicked()
                                          {
                                              // ANNULLA
                                              if (aDatabase.open()) {
                                                  aDatabase.close();
                                              }
                                              close();
                                          }
                                          

                                          and try to re-load the dialog (from application menu)

                                          after first record of database additem() crashes the application

                                          if i comment the additem() all works

                                          this is the code i've used to load the comboBox data:

                                          QSqlQuery cl;
                                              cl.prepare("Select RAGSOC from CLIENTI order by RAGSOC ASC");
                                              ui->comboBox_Cliente->clear();
                                              if (cl.exec()) {
                                                  cl.first();
                                                  do {
                                                      ui->comboBox_Cliente->addItem(cl.value(0).toString());
                                                  } while (cl.next());
                                              }
                                          

                                          ideas??

                                          Thanks in advance.

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

                                          @TheCipo76

                                          after first record of database additem() crashes the application

                                          Has nobody yet asked you to run this in a debugger (e.g. from Qt Creator)? And give us the stacktrace? EDIT: Oh, @VRonin has 2 posts above. Please do so!

                                          1 Reply Last reply
                                          1

                                          • Login

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