Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Text in ListView from QAbstractListModel

    QML and Qt Quick
    listview qabstractlistmo
    2
    3
    1182
    Loading More Posts
    • 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
      Noturnoz last edited by 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 1 Reply Last reply Reply Quote 0
      • J
        jalomic @Noturnoz last edited by

        @Noturnoz said:

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

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

        hint : !!!!!!!!

        N 1 Reply Last reply Reply Quote 0
        • N
          Noturnoz @jalomic last edited by

          @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 Reply Quote 0
          • First post
            Last post