Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Only one element os a QStringList in a ListView

Only one element os a QStringList in a ListView

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cochise
    wrote on last edited by
    #1

    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
    }
    }
    }@

    http://cochise.tumblr.com

    1 Reply Last reply
    0
    • D Offline
      D Offline
      daliusd
      wrote on last edited by
      #2

      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
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved