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

Add Item to QComboBox

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 3.6k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi, I'm trying to open a file and then set my QComboBox model depending on that file.

    I'm able to get the needed values from the file but when i try to add them to my comboBox with addItem() my app crashes.

    myCombo is QComboBox member of my Widget class.

    void Widget::openKemPiInfoFile(){
    
    [...]
    
        for(int i=0;i<vars.model()->rowCount();i++){
            QModelIndex nIndex = vars.model()->index(i,0);
            QVariant l=vars.model()->data(nIndex);
            AbsKemVar v = l.value<AbsKemVar>(); // tmp object 
            
                   QString alias = v.varAlias();
    
            // myCombo->addItem(alias); // crash
            // emit addItemToCombo(alias); // crash also
            qDebug()<<"alias is : "<< alias; // works
        }
    }
    

    I tryed to emit a signal from my for loop and add the item in a different slot but had the same crash.

    How to solve this please ?

    J.HilkJ 1 Reply Last reply
    0
    • VRoninV VRonin

      @Christian-Ehrlicher said in Add Item to QComboBox:

      Your testapp works very fine

      I think you should also add the button and combo to a layout or call show on them

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #12

      @VRonin every time im trying to past only part of code that may cause the problem, thats why there is no layouts and dtor

      @Christian-Ehrlicher said in Add Item to QComboBox:

      Your testapp works very fine apart from the missing dtor and semicolon after the connect.

      My misstake was to create my Widget on the heap inside the ctor of another 'main' widget...

      MainW::MainW(QWidget *parent) : QWidget(parent)
      {
          Widget *w = new Widget;
          WidgetForce *wf = new WidgetForce;
      }
      
      ... 
      //main.cpp
      
          MainW mw;
          mw.show()
      
      

      I moved it (Widget w) as member to solve this issue.

      Thank you all and sorry for not being explicit about this from the beginning

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        Hi
        Are you sure you new'ed myCombo ?
        myCombo = new QComboBox(this);
        Adding to it should not crash.

        1 Reply Last reply
        5
        • ODБOïO ODБOï

          Hi, I'm trying to open a file and then set my QComboBox model depending on that file.

          I'm able to get the needed values from the file but when i try to add them to my comboBox with addItem() my app crashes.

          myCombo is QComboBox member of my Widget class.

          void Widget::openKemPiInfoFile(){
          
          [...]
          
              for(int i=0;i<vars.model()->rowCount();i++){
                  QModelIndex nIndex = vars.model()->index(i,0);
                  QVariant l=vars.model()->data(nIndex);
                  AbsKemVar v = l.value<AbsKemVar>(); // tmp object 
                  
                         QString alias = v.varAlias();
          
                  // myCombo->addItem(alias); // crash
                  // emit addItemToCombo(alias); // crash also
                  qDebug()<<"alias is : "<< alias; // works
              }
          }
          

          I tryed to emit a signal from my for loop and add the item in a different slot but had the same crash.

          How to solve this please ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @LeLev
          on first glance, this seems like an order of execution problem, are you sure myCombo is initialized before the function is called ?

          you can set myCombo = nullptr; in your header and check for null inside your fucntion to be sure:

          qDebug()<<"alias is : "<< alias << "Combobox exists:" << myCombo ; // works

          My guess is, the debug console will print "Combobox exists: nullpointer"


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #4

            Hi, thx,
            yes of course, init it in the constructor, and add item manually to check

            myCombo=new QComboBox();
              myCombo->addItem("Vid 1");
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #5

              @LeLev said in Add Item to QComboBox:

              valueToSpyCombo

              is not

              myCombo

              /edit: ah, modifed... please show us the complete source code...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #6

                @J.Hilk said in Add Item to QComboBox:

                on first glance, this seems like an order of execution problem, are you sure myCombo is initialized before the function is called ?

                Yes it is.

                @J.Hilk said in Add Item to QComboBox:

                My guess is, the debug console will print "Combobox exists: nullpointer"

                it will print every value in the file i need, but i'm not able to add this values to my qComboBox

                ODБOïO 1 Reply Last reply
                0
                • ODБOïO ODБOï

                  @J.Hilk said in Add Item to QComboBox:

                  on first glance, this seems like an order of execution problem, are you sure myCombo is initialized before the function is called ?

                  Yes it is.

                  @J.Hilk said in Add Item to QComboBox:

                  My guess is, the debug console will print "Combobox exists: nullpointer"

                  it will print every value in the file i need, but i'm not able to add this values to my qComboBox

                  ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by ODБOï
                  #7

                  @Christian-Ehrlicher this is the equivalent of my code,
                  I tryed this very simple exemple, I crerate a QComboBox, init it in the constructor, and use addItem().

                  My app starts i get the default QComboBox with "Vid 1" model, then i click the button to add another item and my app crashes, what is the probleme ?

                  class Widget : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      explicit Widget(QWidget *parent = nullptr);
                      ~Widget();
                  
                  private:
                      QPushButton *addBtn;
                      QComboBox *valueToSpyCombo;
                  public slots :
                   void addItemToCombo();
                  };
                  
                  Widget::Widget(QWidget *parent)
                      : QWidget(parent)
                  {
                      addBtn= new QPushButton (this);
                      valueToSpyCombo=new QComboBox();
                      valueToSpyCombo->addItem("Vid 1");
                  
                  QObject::connect(addBtn,&QPushButton::clicked,this,&Widget::addItemToCombo)
                  
                  }
                  
                  void Widget::addItemToCombo(){
                     valueToSpyCombo->addItem("Added item from slot");
                  }
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • ODБOïO ODБOï

                    @Christian-Ehrlicher this is the equivalent of my code,
                    I tryed this very simple exemple, I crerate a QComboBox, init it in the constructor, and use addItem().

                    My app starts i get the default QComboBox with "Vid 1" model, then i click the button to add another item and my app crashes, what is the probleme ?

                    class Widget : public QWidget
                    {
                        Q_OBJECT
                    
                    public:
                        explicit Widget(QWidget *parent = nullptr);
                        ~Widget();
                    
                    private:
                        QPushButton *addBtn;
                        QComboBox *valueToSpyCombo;
                    public slots :
                     void addItemToCombo();
                    };
                    
                    Widget::Widget(QWidget *parent)
                        : QWidget(parent)
                    {
                        addBtn= new QPushButton (this);
                        valueToSpyCombo=new QComboBox();
                        valueToSpyCombo->addItem("Vid 1");
                    
                    QObject::connect(addBtn,&QPushButton::clicked,this,&Widget::addItemToCombo)
                    
                    }
                    
                    void Widget::addItemToCombo(){
                       valueToSpyCombo->addItem("Added item from slot");
                    }
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @LeLev said in Add Item to QComboBox:

                    what is the probleme

                    Did you try to debug you app? Set a break point at

                    valueToSpyCombo->addItem("Added item from slot");
                    

                    and check whether valueToSpyCombo is valid pointer when debugger stops at that line and if so check the stack trace when the app crashes (post it here so we can take a look as well).

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

                    ODБOïO 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @LeLev said in Add Item to QComboBox:

                      what is the probleme

                      Did you try to debug you app? Set a break point at

                      valueToSpyCombo->addItem("Added item from slot");
                      

                      and check whether valueToSpyCombo is valid pointer when debugger stops at that line and if so check the stack trace when the app crashes (post it here so we can take a look as well).

                      ODБOïO Offline
                      ODБOïO Offline
                      ODБOï
                      wrote on last edited by
                      #9

                      @jsulm that exactly what i was trying to do, but i have :
                      "The GDB process terminated"
                      "The gdb process has not responded to a command within 40 seconds.."

                      as soon i try to expend objects to see what i got in.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        Your testapp works very fine apart from the missing dtor and semicolon after the connect.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        VRoninV 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          Your testapp works very fine apart from the missing dtor and semicolon after the connect.

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

                          @Christian-Ehrlicher said in Add Item to QComboBox:

                          Your testapp works very fine

                          I think you should also add the button and combo to a layout or call show on them

                          "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

                          ODБOïO 1 Reply Last reply
                          0
                          • VRoninV VRonin

                            @Christian-Ehrlicher said in Add Item to QComboBox:

                            Your testapp works very fine

                            I think you should also add the button and combo to a layout or call show on them

                            ODБOïO Offline
                            ODБOïO Offline
                            ODБOï
                            wrote on last edited by
                            #12

                            @VRonin every time im trying to past only part of code that may cause the problem, thats why there is no layouts and dtor

                            @Christian-Ehrlicher said in Add Item to QComboBox:

                            Your testapp works very fine apart from the missing dtor and semicolon after the connect.

                            My misstake was to create my Widget on the heap inside the ctor of another 'main' widget...

                            MainW::MainW(QWidget *parent) : QWidget(parent)
                            {
                                Widget *w = new Widget;
                                WidgetForce *wf = new WidgetForce;
                            }
                            
                            ... 
                            //main.cpp
                            
                                MainW mw;
                                mw.show()
                            
                            

                            I moved it (Widget w) as member to solve this issue.

                            Thank you all and sorry for not being explicit about this from the beginning

                            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