Reputation: 15
I need to insert items into a ComboBox, but to do so, i need to have a QStringList instead of a QList.
In my .h i have the QList of a Object.
QList<placavideo> listaPlacavideo;
In my .cpp i have the implementation:
placavideo placavideo1;
placavideo1.setNome("EVGA NVIDIA GeForce RTX 2080");
listaPlacavideo.insert(0,placavideo1);
I have tried this conversion based on another questions, that isn't working:
void intcomputer::on_box_placavideo_activated(const QString &arg1)
{
QStringList listaPlacavideos;
foreach(placavideo *item, listaPlacavideo)
listaPlacavideos << item;
ui->box_fonte->addItems(listaPlacavideos);
}
The question is, how do I insert the items from listaPlacavideo into a QStringList, so I can insert the entire QStringList into the ComboBox?
Upvotes: 0
Views: 1656
Reputation: 1
Assuming you have loaded QList listaPlacavideo; Then:
QStringList slist = QStringList(listaPlacavideo);
combobox.addItems(slist); // assuming 'combobox' is the name of the QComboBox
Upvotes: 0
Reputation: 5095
If you have a function in placavideo
to get the name, for example getNome()
, then you just need to change one line of code.
void intcomputer::on_box_placavideo_activated(const QString &arg1)
{
QStringList listaPlacavideos;
foreach(const placavideo& item, listaPlacavideo)
{
listaPlacavideos << item.getNome(); // Add the name to the list, not the whole object
}
ui->box_fonte->addItems(listaPlacavideos);
}
EDIT: I added const
before placavideo&
. That should fix your error.
EDIT2: You should declare the function getNome()
as const.
QString getNome() const;
Upvotes: 1
Reputation: 10103
You cannot make the object into a name. It is expecting a QString
for a display name. If you want to store the objects in the combo box as well, then do something like this:
foreach ( plavavideo &item, listaPlacavideo ) {
ui->box_fonte->addItem( item.getName(), item );
}
This will require placavideo
to be a meta type usable by QVariant
though.
Upvotes: 3