Loading QStringList value received from signal slot



  • In my qt c++ application a QStringList is sent from one cpp file(MainWIndow) to another cpp file(Dialog) via signal and slots mechanism! I want to display the elements in the qtringList on a combo box in the Dialog.ui when the interface gets loaded(no button click)!

    following is my code

    #include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    for(int i=0;i<subMessages.size();i++){
    ui->comboBox->addItem(subMessages[i]);

    }
    

    }

    void Dialog::receiveSubMessages(QStringList List){
    subMessages=List;

    }

    The QStringList is received successfully through the slot(already verified).Though I used a for loop and tried display (as in the code) nothing was displayed on the combo box! How can I fix this issue?



  • @Lasith said in Loading QStringList value received from signal slot:

    The QStringList is received successfully through the slot(already verified).

    Hi @Lasith

    The receiveSubMessages(QStringList List) is called after the construction of your Dialog , Every time you try to add an item on your comboxBox your List is empty ,

    Can you share the other parts of your code ?

    Maybe you just wanna have another argument on your Dialog constructor ?

    For example:

    Dialog::Dialog(QStringList listToLoad, QWidget *parent = 0) ;
    

  • Lifetime Qt Champion

    Hi,

    To add to @mostefa, if you want to update your combo box, you should first clear it and the call the QComboBox::addItems method. This will avoid that unnecessary loop.

    Note that you should also pass your QStringList as const reference, that will avoid an unnecessary copy.



  • @mostefa Thanx these are the only parts in my Dialog.cpp file! After I put th QStringList as an argument I get an error stating "Prototype for Dialog does not match any in class Dialog".



  • @SGaist Thanx my combo box is empty from the beginning I just want to load the QStringList contents to it! :(


  • Lifetime Qt Champion

    Hi
    If you changed to
    Dialog::Dialog(QStringList listToLoad, QWidget *parent = 0) ;

    Make sure to change it both in .h file and .cpp file so they match.