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
Forum Updated to NodeBB v4.3 + New Features

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.6k Views 2 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.
  • P Offline
    P Offline
    PurityLake
    wrote on 2 Aug 2016, 19:04 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 R 2 Replies Last reply 2 Aug 2016, 19:52
    0
    • P PurityLake
      2 Aug 2016, 19:04

      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 2 Aug 2016, 19:52 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)

      P 1 Reply Last reply 2 Aug 2016, 20:30
      0
      • P PurityLake
        2 Aug 2016, 19:04

        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.

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 2 Aug 2016, 19:52 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
          2 Aug 2016, 19:52

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

          P Offline
          P Offline
          PurityLake
          wrote on 2 Aug 2016, 20:30 last edited by
          #4

          @koahnig

          Hi, the problem seems to be

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

          Not the actual clear() method.

          M 1 Reply Last reply 2 Aug 2016, 20:36
          0
          • P PurityLake
            2 Aug 2016, 20:30

            @koahnig

            Hi, the problem seems to be

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

            Not the actual clear() method.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 2 Aug 2016, 20:36 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 ?

            P 1 Reply Last reply 2 Aug 2016, 20:56
            0
            • M mrjj
              2 Aug 2016, 20:36

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

              P Offline
              P Offline
              PurityLake
              wrote on 2 Aug 2016, 20:56 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

              M 1 Reply Last reply 2 Aug 2016, 20:58
              0
              • P PurityLake
                2 Aug 2016, 20:56

                @mrjj

                Ya it is a valid Person object.

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

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Aug 2016, 20:58 last edited by
                #7

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

                does it still crash?

                P 1 Reply Last reply 2 Aug 2016, 21:34
                0
                • M mrjj
                  2 Aug 2016, 20:58

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

                  does it still crash?

                  P Offline
                  P Offline
                  PurityLake
                  wrote on 2 Aug 2016, 21:34 last edited by
                  #8

                  @mrjj

                  Yes it still crashes

                  M 1 Reply Last reply 2 Aug 2016, 21:35
                  0
                  • P PurityLake
                    2 Aug 2016, 21:34

                    @mrjj

                    Yes it still crashes

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 2 Aug 2016, 21:35 last edited by
                    #9

                    @mrjj said:

                    this->ui->personComboBox

                    then pointer must be corrupt?

                    1 Reply Last reply
                    0

                    9/9

                    2 Aug 2016, 21:35

                    • Login

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