Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Add Item to QComboBox



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



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


  • Lifetime Qt Champion

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


  • Moderators

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



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

    myCombo=new QComboBox();
      myCombo->addItem("Vid 1");
    

  • Qt Champions 2019

    @LeLev said in Add Item to QComboBox:

    valueToSpyCombo

    is not

    myCombo

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



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



  • @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");
    }
    

  • Qt Champions 2019

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



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


  • Qt Champions 2019

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



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



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


Log in to reply