Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a string to QCombobox

Tags:

qt

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?

like image 421
Frank Avatar asked Oct 26 '25 07:10

Frank


2 Answers

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);
like image 184
Andreas Fester Avatar answered Oct 29 '25 09:10

Andreas Fester


If you use a pointer you'll need to de-referenciate it first: cbb.addItem(*s); Anyway why are you allocating a QString on heap and the comboBox (which most likely will get a parent) on stack?

like image 42
Zlatomir Avatar answered Oct 29 '25 07:10

Zlatomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!