How to add line to QCombobox

I used to add items to QCombobox by saying:

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

      

But if I try this I get the 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?

+3


source to share


2 answers


Don't use heap allocation with QString

. QString

handles memory management within a string - if you allocate memory for an object QString

yourself, you also need to take care of memory release.

In your case, just use



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

      

+8


source


If you are using a pointer, you need to remove it first: cbb.addItem (* s); Anyway, why are you allocating the QString on the heap and the comboBox (which is most likely to receive the parent) on the stack?



+1


source







All Articles