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
Qt 6.11 is out! See what's new in the release blog

Text in ListView from QAbstractListModel

Scheduled Pinned Locked Moved QML and Qt Quick
listviewqabstractlistmo
3 Posts 2 Posters 1.6k Views 1 Watching
  • 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 last edited by Noturnoz
    #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
    0
    • N Noturnoz

      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 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
      0
      • J jalomic

        @Noturnoz said:

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

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

        hint : !!!!!!!!

        N Offline
        N Offline
        Noturnoz
        wrote on 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

        • Login

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