Important: Please read the Qt Code of Conduct -

Only one element os a QStringList in a ListView

  • hi, people.
    I'm playing with the "SDDM":, trying to show the session's list in a dropdown menu instead of a spinbox, but only one element of the list appear in the List View, and i can't realize why the full list is not being loaded.
    The only loaded is the custom. Changing the current index of of List View is possible load any, but not all sessions.

    Anyone can help?

    The code snippets:

    The QStringList is exposed as a property
    @class SessionManager : public QObject {
    Q_PROPERTY(QStringList sessionNames READ sessionNames CONSTANT)@

    and is populated at line 30
    @ SessionManager::SessionManager() : d(new SessionManagerPrivate()) {
    // read session files
    QDir dir(Configuration::instance()->sessionsDir());
    dir.setNameFilters(QStringList() << "*.desktop");
    // read session
    foreach (const QString &session, dir.entryList()) {
    QFile inputFile(dir.absoluteFilePath(session));
    if (!
    SessionInfo si { session, "", "", "" };
    QTextStream in(&inputFile);
    while (!in.atEnd()) {
    QString line = in.readLine();
    if (line.startsWith("Name=")) = line.mid(5);
    if (line.startsWith("Exec="))
    si.exec = line.mid(5);
    if (line.startsWith("Comment="))
    si.comment = line.mid(8);
    // add to sessions list
    // close file
    // check last session index
    d->lastSessionIndex = 0;
    for (int i = 0; i < d->sessions.size(); ++i) {
    d->sessionNames << d->;
    if (d-> == Configuration::instance()->lastSession())
    d->lastSessionIndex = i;

    The property is exposed to QML
    @SessionManager sessionManager;
    view.rootContext()->setContextProperty("sessionNames", QVariant::fromValue(sessionManager.sessionNames()));@

    Recived in Main.qml and sent to SpinBox.qml
    @SpinBox {
    items: sessionNames@

    recived in SpinBox.qml and used
    @FocusScope {
    id: container
    property variant items: [""]
    ListView {
    id: listView
    model: items
    delegate: Text {
    text: modelData

  • I'm not sure how Q_PROPERTY works with setContextProperty. Overall your code looks good apart that. Maybe something like this would work instead of using setContextProperty.

    qmlRegisterType<EventHelpers>("YourModule", 1, 0, "SessionManager");

    import YourModule 1.0

    SessionManager {
    id: sessionManager

    SpinBox {
    items: sessionManager.sessionNames

Log in to reply