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. Text in ListView from QAbstractListModel

Text in ListView from QAbstractListModel

Scheduled Pinned Locked Moved QML and Qt Quick
listviewqabstractlistmo
3 Posts 2 Posters 1.4k 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.
  • N Offline
    N Offline
    Noturnoz
    wrote on 10 Mar 2015, 20:11 last edited by Noturnoz 3 Oct 2015, 20:32
    #1

    I have a subclass of QAbstractListModel:

    int JobListModel::rowCount(const QModelIndex &parent) const{
        return m_items.size();
    }
    
    QVariant JobListModel::data(const QModelIndex &index, int role) const{
        if(index.isValid()){
            return QVariant();
        }
        if(index.row() > (m_items.size() - 1)){
            return QVariant();
        }
    
        Job* jobObj = m_items.at(index.row());
    
        switch (role) {
        case IdRole:
            return QVariant::fromValue(jobObj->jobId());
        case TitleRole:
            return QVariant::fromValue(jobObj->jobTitle());
        default:
            return QVariant();
        }
    }
    
    QHash<int, QByteArray> JobListModel::roleNames() const{
        QHash<int, QByteArray> roles;
        roles[IdRole] = "jobId";
        roles[TitleRole] = "jobTitle";
        return roles;
    }
    

    Then I have a main.cpp:

    QApplication app(argc, argv);
    
    JobListModel jobModel;
    
    jobModel.generateJobs();
    
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("jobModel", &jobModel);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    return app.exec();
    

    Jobs.qml:

    ListView {
        id: jobs
    
        width: 600
        height: 250
    
        model: jobModel
    
        delegate: Item {
            x: 0
            width: 80
            height: 50
            Row {
                id: row1
                x: 0
                y: 0
                z: 100
                width: 600
                height: 50
                spacing: 10
                layoutDirection: Qt.LeftToRight
    
                Text {
                    x: 59
                    text: <<What here?>>
                    font.bold: false
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }
    }
    

    What should I write into the text: in the Jobs.qml to show the jobId and jobTitle values? The list shows correct number of items but without text. I have tried a lot of combinations...

    J 1 Reply Last reply 10 Mar 2015, 20:56
    0
    • N Noturnoz
      10 Mar 2015, 20:11

      I have a subclass of QAbstractListModel:

      int JobListModel::rowCount(const QModelIndex &parent) const{
          return m_items.size();
      }
      
      QVariant JobListModel::data(const QModelIndex &index, int role) const{
          if(index.isValid()){
              return QVariant();
          }
          if(index.row() > (m_items.size() - 1)){
              return QVariant();
          }
      
          Job* jobObj = m_items.at(index.row());
      
          switch (role) {
          case IdRole:
              return QVariant::fromValue(jobObj->jobId());
          case TitleRole:
              return QVariant::fromValue(jobObj->jobTitle());
          default:
              return QVariant();
          }
      }
      
      QHash<int, QByteArray> JobListModel::roleNames() const{
          QHash<int, QByteArray> roles;
          roles[IdRole] = "jobId";
          roles[TitleRole] = "jobTitle";
          return roles;
      }
      

      Then I have a main.cpp:

      QApplication app(argc, argv);
      
      JobListModel jobModel;
      
      jobModel.generateJobs();
      
      QQmlApplicationEngine engine;
      engine.rootContext()->setContextProperty("jobModel", &jobModel);
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
      return app.exec();
      

      Jobs.qml:

      ListView {
          id: jobs
      
          width: 600
          height: 250
      
          model: jobModel
      
          delegate: Item {
              x: 0
              width: 80
              height: 50
              Row {
                  id: row1
                  x: 0
                  y: 0
                  z: 100
                  width: 600
                  height: 50
                  spacing: 10
                  layoutDirection: Qt.LeftToRight
      
                  Text {
                      x: 59
                      text: <<What here?>>
                      font.bold: false
                      anchors.verticalCenter: parent.verticalCenter
                  }
              }
          }
      }
      

      What should I write into the text: in the Jobs.qml to show the jobId and jobTitle values? The list shows correct number of items but without text. I have tried a lot of combinations...

      J Offline
      J Offline
      jalomic
      wrote on 10 Mar 2015, 20:56 last edited by
      #2

      @Noturnoz said:

      if(index.isValid()){
      return QVariant();
      }

      =)Try to read your code again. You forgot something

      hint : !!!!!!!!

      N 1 Reply Last reply 10 Mar 2015, 21:02
      0
      • J jalomic
        10 Mar 2015, 20:56

        @Noturnoz said:

        if(index.isValid()){
        return QVariant();
        }

        =)Try to read your code again. You forgot something

        hint : !!!!!!!!

        N Offline
        N Offline
        Noturnoz
        wrote on 10 Mar 2015, 21:02 last edited by
        #3

        @jalomic Ok, I think I will kill myself :D Thanks a lot, I've spent a lot of time thinking about why does it not work and was not expecting something such as this...

        1 Reply Last reply
        0

        1/3

        10 Mar 2015, 20:11

        • Login

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