Only one element os a QStringList in a ListView



  • hi, people.
    I'm playing with the "SDDM":https://github.com/sddm/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.
    !http://img546.imageshack.us/img546/8692/snapshot1gd.png!
    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_OBJECT
    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");
    dir.setFilter(QDir::Files);
    // read session
    foreach (const QString &session, dir.entryList()) {
    QFile inputFile(dir.absoluteFilePath(session));
    if (!inputFile.open(QIODevice::ReadOnly))
    continue;
    SessionInfo si { session, "", "", "" };
    QTextStream in(&inputFile);
    while (!in.atEnd()) {
    QString line = in.readLine();
    if (line.startsWith("Name="))
    si.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
    d->sessions.push_back(si);
    // close file
    inputFile.close();
    }
    // check last session index
    d->lastSessionIndex = 0;
    for (int i = 0; i < d->sessions.size(); ++i) {
    d->sessionNames << d->sessions.at(i).name;
    if (d->sessions.at(i).file == 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.