0

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
0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Doh! I wish it would have thrown a compilation-like error...thank you!
@user1595510 It's still valid code, there's no error, it just doesn't mean what you think it means :) You can, of course, type check the arguments, but that's only of benefit if you do it everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.