Can't read QStringList from QML/JavaScript function

I have a QML class that has a function in it with a QStringList as a parameter. I'm able to access other items in the C++ model from QML just fine.

In my QML:

function recentFiles(recentFilesList)
{
    //This writes "0" for some reason, although it should be "3"
    console.log(recentFilesList.length)

    //Causes error: "Unable to assign [undefined] to QString"
    return recentFilesList[0]
}

...

Text {
    text: recentFiles(rootObject.myModel.recentFiles)
}

In my source file:

QStringList someModel::recentFiles() const
{
    QStringList recentFiles;

    recentFiles << "A" << "B" << "C";

    return recentFiles;
}

In my header file:

Q_INVOKABLE QStringList recentFiles() const;

Ultimately, I'm trying to get my QStringList to work on a QML ListView object where it will display like this:

  1. A
  2. B
  3. C

Answers


I think this is not a method call on myModel, you're passing recentFiles whatever passes for an invokable method wrapper in QML:

Text {
    text: recentFiles(rootObject.myModel.recentFiles)
}

You want to call the recentFiles method:

Text {
    //                                              vv !
    text: recentFiles(rootObject.myModel.recentFiles())
}

Alas, if your string list is mutable and can change while the UI is displayed, you should simply use a QStringListModel.


Need Your Help

Building hadoop using maven

hadoop maven-3

I tried to build hadoop mapreduce project with maven, but it always stuck in following error,

Tool to visualize Function Hierarchy trees?

oop function code-analysis

Any code-analysis or "reverse-engineering" tool that can do either of these?: