0

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]);

    }
    }

//slot which is used to get the qstringList
    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?

Kushan Peiris
  • 167
  • 3
  • 15
  • You didn't expose the declaration of `subMessages`. What is it?. A member variable `Dialog`? Why do you think assinging something to `subMessages` may have any effect on `ui->comboBox`? In the constructor of `Dialog`, you have already shown how values of `subMessages` become items of `ui-comboBox` - calling `ui->comboBox->addItem(subMessages[i]);` with `i` indexing each element of `subMessages`. – Scheff's Cat Dec 03 '17 at 07:26
  • @Scheff subMessages is QStringList! I want to load the contents of it into a combo box when the Dialog.ui loads (without any button click). Currently there is no error but the combo box seems to be empty :( – Kushan Peiris Dec 03 '17 at 07:36
  • @Scheff The assignment is because receiveSubMessages() is the slot used to get the QStringList from the previous cpp file! The QstringList which is received from the previous file is assigned to the subMessages QStringList! – Kushan Peiris Dec 03 '17 at 07:48
  • Fine. You assigned `List` to `subMessages`. Why this should have any impact on `ui->comboBox`? – Scheff's Cat Dec 03 '17 at 07:50

2 Answers2

1

I did this answer rather to show you how to solve your problem. (I've the feeling that I even didn't understand what your actual problem is.)

When asking a question the chances to get a helpful answer increase if an MCVE is provided. (Please, follow this link. It teachs you really basic skills every S/W developer shouldmust have. I would also recommend to follow-up to How to debug small programs.)

As I did understand your problem I made such an MCVE. This is the code testQComboBox:

#include <QtWidgets>

int main(int argc, char **argv)
{
  // build appl.
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // a QStringList with items
  QStringList items;
  items
    << QString::fromUtf8("first item")
    << QString::fromUtf8("second item")
    << QString::fromUtf8("third item");
  // build GUI
  QDialog dlg;
  QVBoxLayout vBox;
  QComboBox cmbBox;
  cmbBox.addItems(items);
  vBox.addWidget(&cmbBox);
  dlg.setLayout(&vBox);
  dlg.show();
  app.exec();
  // done
  return 0;
}

I compiled it in VS2013 with Qt 5.9.2 on Windows 10 (64 bit). This is how it looks:

Snapshot of testQComboBox

As you see, the usage of combobox is rather easy – no secret trap doors to use it. The actual code which is directly related to QComboBox is exactly 4 lines of code:

  QVBoxLayout vBox;
  QComboBox cmbBox;
  cmbBox.addItems(items);
  vBox.addWidget(&cmbBox);

And there is exactly one line of code where items are added to the QComboBox:

  cmbBox.addItems(items);

Note:

I used QComboBox::addItems() instead of QComboBox::addItem() as the former has already a loop built-in to add a complete QStringList. It doesn't make any difference to the loop you used in your code Dialog::Dialog().

So, finally I dare to do the following statement:

If your combobox doesn't show items then:

  1. You added items from an empty list.

  2. Or, you forgot to add the items from list.

  3. Or, something very weird happend.

I would always bet for 1. or 2. reason – the 3. reason is for real emergency cases only (e.g. broken Qt installation).


Concerning 3. reason:

I saw many questions where some lines of innocent looking code were presented which looked exactly as they should but were claimed to fail. And finally almost everytimes it showed that these lines worked fine when isolated in an MCVE but they didn't in the original code. How can this happen? Either there is some context which changes the behavior of the code in your original program or there is UB – undefined behavior. Something else does bad things but instead of crashing your process immediately (which would mean you're lucky) it goes on for a while corrupting the data more and more until finally everything breaks completely. Looking into the core-dump doesn't help at all. Therefore my recommendation of How to debug small programs.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
1

In order to get a working code you need to place your for llop inside you slot:

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

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

//slot which is used to get the qstringList
void Dialog::receiveSubMessages(QStringList List){ 
    ui->comboBox->addItems (List);
}

If you want to fill the comboBox with the contents of some QStringList upon Dialog construction then you should either pass this list as constructor argument:

Dialog::Dialog(QStringList List, QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
    ui->setupUi(this); 
    ui->comboBox->addItems (List);
}

or call Dialog::receiveSubMessages() right after Dialog object construction:

// somewhere in code, where you construct Dialog object
// ...
auto parent = SomeQWidgetDerivedClass (...);
QStringList someStringList {
    "string 1",
    "string 2"
};
// ...
auto dialog {new Dialog ()};

dialog->receiveSubMessages (someStringList);
// ...

The code that you have provided will never allow you to achieve the desired result because your for loop that is supposed to fill the QComboBox is executed on your Dialog object creation. At that point your subMessages is empty. The slot that you have created is not called before the constructor - the slot is just a member function that can only be called after the object is created. You can only call a member function without an object if the function itself is static, in which case it is definitely not a slot.