Search code examples
qtqcombobox

How to add a string to QCombobox


Normally I would add items to a QCombobox by saying:

QCombobox cbb;
cbb.addItem("Hello");

But if I try this I get an error:

QComboBox cbb;
QString * s = new QString("hallo");
cbb.addItem(s);

error: no matching function for call to 'QComboBox::addItem(QString*&)'

How can I solve this?


Solution

  • Don't use dynamic memory allocation with QString. QString handles memory management for the string internally - if you allocate the memory for the QString object yourself, you also need to take care of releasing the memory.

    In your case, simply use

    QComboBox cbb;
    QString s = "hallo";
    cbb.addItem(s);