Chechen Itza
Chechen Itza

Reputation: 141

Can't return QStringList from lambda into a function

I'm trying to fill the combobox in QT 5.7 using following code:

ui->comboBox_2->addItems([]() -> QStringList {
        QDate date = QDate::currentDate();
        int current_year = date.toString("yyyy").toInt();
        QStringList year_list;
        for (int i = 0; i <= 50; i++) {
            year_list << QString::number(current_year - (50 - i));
        }
        return year_list;
    });

But it gives me this error: no viable conversion from '(lambda at ..)' to 'const QStringList'
What's the problem?

Upvotes: 0

Views: 511

Answers (2)

AmirShrestha
AmirShrestha

Reputation: 21

include () at the end of {} to explicitly call this lambda function.

Upvotes: 1

Julien Lopez
Julien Lopez

Reputation: 1021

addItems expects a QStringList, not a lambda that produces a QStringList, so you have to call your lambda to get the QStringList:

ui->comboBox_2->addItems([]() { ... }());

Upvotes: 1

Related Questions