Reputation: 1640
i want to display some of the content of my array in a List Widget (item based) with Qt and C++, i tried this, but it dosent work :
QString exemple[2] = 'blablabla'
ui->listWidgetResult->addItem(exemple[2].toStdString().c_str());
Thanks !
Upvotes: 2
Views: 8315
Reputation: 302
I think this should be an easy solution to what you are asking for.
void MyClass::Set_List(QList<QString> filesList, int item_count)
{
QVector<QString> load_set(item_count);
for(int i = 0; i < item_count; i++)
{
load_set[i] = filesList[i];
ui -> listWidget -> addItem(load_set[i]);
}
}
Then to get the info back out...
void MyClass::Selection(QListWidgestItem * item)
{
for(int i = 0; i < item_count; i++)
{
if(ui -> listWidget -> item(i) == item)
{
str = ui -> listWidget -> item(i) -> text();
}
}
}
Upvotes: 0
Reputation: 51920
This can't work:
QString example[2] = 'blablabla'
First, '
is for char
values, not for strings. Second, you are declaring an array of two QStrings, but assign it to a C string. What you mean is perhaps this:
QString example[2] = {"blabla", "blabla"};
Which you can actually abbreviate to:
QString example[] = {"blabla", "blabla"};
To add each string of the array to your list widget, you need to add each one individually. Also, there's no need to convert to a C string. QListWidget::addItem() takes QStrings:
for (int i = 0; i < sizeof(example); ++i) {
ui->listWidgetResult->addItem(exemple[i]);
}
Or, if you have a recent compiler that supports C++-11:
for (const auto& str : example) {
ui->listWidgetResult->addItem(str);
}
Finally, instead of using plain arrays to hold your QStrings, you should instead consider holding them in a QStringList. You can then simply pass the whole QStringList using addItems()
.
Upvotes: 6