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. Program crashes when calling QComboBox::clear() on a non-empty Combo Box
QtWS25 Last Chance

Program crashes when calling QComboBox::clear() on a non-empty Combo Box

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcomboboxqcombobox crashcrash
9 Posts 4 Posters 5.4k 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.
  • PurityLakeP Offline
    PurityLakeP Offline
    PurityLake
    wrote on last edited by
    #1

    Hi all,

    I'm currently working on a project in Qt and I have come across a problem in my project that if I try to call clear on an empty Combo Box there is no problem but when I call clear on a ComboBox with items in it then the program crashes. All it tells me is The program has unexpectedly finished.

    This isn't just for one form, it is for all forms that I am using but I'll post the details of the one that alerted me to the problem.

    #ifndef CITYUI_H
    #define CITYUI_H
    
    #include <QMainWindow>
    
    #include <string>
    
    #include "city.h"
    
    namespace Ui {
    class CityUI;
    }
    
    class CityUI : public QMainWindow {
        Q_OBJECT
    public:
        explicit CityUI(QWidget *parent = 0);
        ~CityUI();
    
        const std::string& getCityName() const;
        void setCityName(const std::string& city);
    
    public slots:
        void updateDisplay();
    
    private:
        Ui::CityUI *ui;
        std::string city;
    
        std::vector<const std::string> alreadyAdded;
    
        bool alreadyAddedItem(const std::string& person);
    
    private slots:
        void shopButtonClicked();
        void talkButtonClicked();
        void shopComboIndexChanged();
        void personComboIndexChanged();
    
    signals:
        void playerChanged();
    };
    
    #endif // CITYUI_H
    

    For brevity I am only including the updateDisplay()

    void CityUI::updateDisplay() {
        City city = allCities[this->city];
    
        PeopleVector people = city.getPeople();
        ShopVector shops = city.getShops();
    
        //this->ui->personComboBox->clear();
        for (const auto& aPerson : people) {
            if (!this->alreadyAddedItem(aPerson)) {
                Person person = allPeople[aPerson];
                this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
                this->alreadyAdded.push_back(aPerson);
            }
        }
    
        //this->ui->shopComboBox->clear();
        for (const auto& aShop : shops) {
            Shop shop = allShops[aShop];
            this->ui->shopComboBox->addItem(QString::fromStdString(shop.getName()));
        }
    
        std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
        std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
    
        std::string stringToBeWritten = "Shop: ";
        stringToBeWritten.append(allShops[shopComboEntry].getDesc());
        stringToBeWritten.append("\nPerson: ");
        stringToBeWritten.append(allPeople[personComboEntry].getDesc());
    
        this->ui->gameOutput->setText(QString::fromStdString(stringToBeWritten));
    
        this->ui->personComboBox->setDuplicatesEnabled(false);
        this->ui->shopComboBox->setDuplicatesEnabled(false);
    }
    

    I'm not entirely sure why this is happening but any help that can be given would be greatly appreciated.

    K raven-worxR 2 Replies Last reply
    0
    • PurityLakeP PurityLake

      Hi all,

      I'm currently working on a project in Qt and I have come across a problem in my project that if I try to call clear on an empty Combo Box there is no problem but when I call clear on a ComboBox with items in it then the program crashes. All it tells me is The program has unexpectedly finished.

      This isn't just for one form, it is for all forms that I am using but I'll post the details of the one that alerted me to the problem.

      #ifndef CITYUI_H
      #define CITYUI_H
      
      #include <QMainWindow>
      
      #include <string>
      
      #include "city.h"
      
      namespace Ui {
      class CityUI;
      }
      
      class CityUI : public QMainWindow {
          Q_OBJECT
      public:
          explicit CityUI(QWidget *parent = 0);
          ~CityUI();
      
          const std::string& getCityName() const;
          void setCityName(const std::string& city);
      
      public slots:
          void updateDisplay();
      
      private:
          Ui::CityUI *ui;
          std::string city;
      
          std::vector<const std::string> alreadyAdded;
      
          bool alreadyAddedItem(const std::string& person);
      
      private slots:
          void shopButtonClicked();
          void talkButtonClicked();
          void shopComboIndexChanged();
          void personComboIndexChanged();
      
      signals:
          void playerChanged();
      };
      
      #endif // CITYUI_H
      

      For brevity I am only including the updateDisplay()

      void CityUI::updateDisplay() {
          City city = allCities[this->city];
      
          PeopleVector people = city.getPeople();
          ShopVector shops = city.getShops();
      
          //this->ui->personComboBox->clear();
          for (const auto& aPerson : people) {
              if (!this->alreadyAddedItem(aPerson)) {
                  Person person = allPeople[aPerson];
                  this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
                  this->alreadyAdded.push_back(aPerson);
              }
          }
      
          //this->ui->shopComboBox->clear();
          for (const auto& aShop : shops) {
              Shop shop = allShops[aShop];
              this->ui->shopComboBox->addItem(QString::fromStdString(shop.getName()));
          }
      
          std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
          std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
      
          std::string stringToBeWritten = "Shop: ";
          stringToBeWritten.append(allShops[shopComboEntry].getDesc());
          stringToBeWritten.append("\nPerson: ");
          stringToBeWritten.append(allPeople[personComboEntry].getDesc());
      
          this->ui->gameOutput->setText(QString::fromStdString(stringToBeWritten));
      
          this->ui->personComboBox->setDuplicatesEnabled(false);
          this->ui->shopComboBox->setDuplicatesEnabled(false);
      }
      

      I'm not entirely sure why this is happening but any help that can be given would be greatly appreciated.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @PurityLake

      Hi and welcome to the forum

      My guess is that it is not the clear, but an access to a non-existing item afterwards.

      Can you run it in the debugger? There you can find at which line the actual crash is occuring.

          std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
          std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
      
      

      Possibly there are no entries added for personComboBox after the clear. Therefore the retrieval of currentText will fail.

      Vote the answer(s) that helped you to solve your issue(s)

      PurityLakeP 1 Reply Last reply
      0
      • PurityLakeP PurityLake

        Hi all,

        I'm currently working on a project in Qt and I have come across a problem in my project that if I try to call clear on an empty Combo Box there is no problem but when I call clear on a ComboBox with items in it then the program crashes. All it tells me is The program has unexpectedly finished.

        This isn't just for one form, it is for all forms that I am using but I'll post the details of the one that alerted me to the problem.

        #ifndef CITYUI_H
        #define CITYUI_H
        
        #include <QMainWindow>
        
        #include <string>
        
        #include "city.h"
        
        namespace Ui {
        class CityUI;
        }
        
        class CityUI : public QMainWindow {
            Q_OBJECT
        public:
            explicit CityUI(QWidget *parent = 0);
            ~CityUI();
        
            const std::string& getCityName() const;
            void setCityName(const std::string& city);
        
        public slots:
            void updateDisplay();
        
        private:
            Ui::CityUI *ui;
            std::string city;
        
            std::vector<const std::string> alreadyAdded;
        
            bool alreadyAddedItem(const std::string& person);
        
        private slots:
            void shopButtonClicked();
            void talkButtonClicked();
            void shopComboIndexChanged();
            void personComboIndexChanged();
        
        signals:
            void playerChanged();
        };
        
        #endif // CITYUI_H
        

        For brevity I am only including the updateDisplay()

        void CityUI::updateDisplay() {
            City city = allCities[this->city];
        
            PeopleVector people = city.getPeople();
            ShopVector shops = city.getShops();
        
            //this->ui->personComboBox->clear();
            for (const auto& aPerson : people) {
                if (!this->alreadyAddedItem(aPerson)) {
                    Person person = allPeople[aPerson];
                    this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
                    this->alreadyAdded.push_back(aPerson);
                }
            }
        
            //this->ui->shopComboBox->clear();
            for (const auto& aShop : shops) {
                Shop shop = allShops[aShop];
                this->ui->shopComboBox->addItem(QString::fromStdString(shop.getName()));
            }
        
            std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
            std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
        
            std::string stringToBeWritten = "Shop: ";
            stringToBeWritten.append(allShops[shopComboEntry].getDesc());
            stringToBeWritten.append("\nPerson: ");
            stringToBeWritten.append(allPeople[personComboEntry].getDesc());
        
            this->ui->gameOutput->setText(QString::fromStdString(stringToBeWritten));
        
            this->ui->personComboBox->setDuplicatesEnabled(false);
            this->ui->shopComboBox->setDuplicatesEnabled(false);
        }
        

        I'm not entirely sure why this is happening but any help that can be given would be greatly appreciated.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @PurityLake
        stupid question: are you calling setupUI() in the constructor to actually build the form widgets?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • K koahnig

          @PurityLake

          Hi and welcome to the forum

          My guess is that it is not the clear, but an access to a non-existing item afterwards.

          Can you run it in the debugger? There you can find at which line the actual crash is occuring.

              std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
              std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
          
          

          Possibly there are no entries added for personComboBox after the clear. Therefore the retrieval of currentText will fail.

          PurityLakeP Offline
          PurityLakeP Offline
          PurityLake
          wrote on last edited by
          #4

          @koahnig

          Hi, the problem seems to be

          this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
          

          Not the actual clear() method.

          mrjjM 1 Reply Last reply
          0
          • PurityLakeP PurityLake

            @koahnig

            Hi, the problem seems to be

            this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
            

            Not the actual clear() method.

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

            @PurityLake said:

            Person person = allPeople[aPerson];

            Hi
            does this return a valid person ?
            only thing i can think of why it should crash in
            this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));

            if you change to
            this->ui->personComboBox->addItem("TEST");

            does it still crash?

            also, as @raven-worx ask
            You have called
            setupUI()
            so that this->ui->personComboBox is in fact a valid object ?

            PurityLakeP 1 Reply Last reply
            0
            • mrjjM mrjj

              @PurityLake said:

              Person person = allPeople[aPerson];

              Hi
              does this return a valid person ?
              only thing i can think of why it should crash in
              this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));

              if you change to
              this->ui->personComboBox->addItem("TEST");

              does it still crash?

              also, as @raven-worx ask
              You have called
              setupUI()
              so that this->ui->personComboBox is in fact a valid object ?

              PurityLakeP Offline
              PurityLakeP Offline
              PurityLake
              wrote on last edited by
              #6

              @mrjj

              Ya it is a valid Person object.

              If you refer to my response to @koahnig you'll see my update

              mrjjM 1 Reply Last reply
              0
              • PurityLakeP PurityLake

                @mrjj

                Ya it is a valid Person object.

                If you refer to my response to @koahnig you'll see my update

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

                @PurityLake
                ok.
                if you change to
                this->ui->personComboBox->addItem("TEST");

                does it still crash?

                PurityLakeP 1 Reply Last reply
                0
                • mrjjM mrjj

                  @PurityLake
                  ok.
                  if you change to
                  this->ui->personComboBox->addItem("TEST");

                  does it still crash?

                  PurityLakeP Offline
                  PurityLakeP Offline
                  PurityLake
                  wrote on last edited by
                  #8

                  @mrjj

                  Yes it still crashes

                  mrjjM 1 Reply Last reply
                  0
                  • PurityLakeP PurityLake

                    @mrjj

                    Yes it still crashes

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

                    @mrjj said:

                    this->ui->personComboBox

                    then pointer must be corrupt?

                    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