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. Access QObject Item from a QList

Access QObject Item from a QList

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.2k 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.
  • M Offline
    M Offline
    minhblabla
    wrote on last edited by
    #1

    Hi
    I got a class inherited from QObject like this:
    champion.h
    @#ifndef CHAMPION_H
    #define CHAMPION_H

    #include <QObject>

    class Champions : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ name)
    Q_PROPERTY(QString color READ color)

    public:

    Champions(QObject *parent=0);
    Champions(const QString &name, const QString &color,QObject *parent=0);
    QString name() const;
    
    QString color() const;
    

    private:
    QString c_name;
    QString cc_name;

    };
    #endif // DATAOBJECT_H@

    champion.cpp
    @#include "champion.h"
    #include <QDebug>

    Champions::Champions(QObject *parent)
    : QObject(parent)
    {
    }

    Champions::Champions(const QString &name, const QString &color, QObject parent)
    : QObject(parent), c_name(name), cc_name(color)
    {
    }
    QString Champions::name() const
    {
    return c_name;
    }
    QString Champions::color() const
    {
    return cc_name;
    }
    @
    In my main.cpp, I created a @QList<QObject
    > dataList@ to add some Champions class in. Now because I want to access c_name and cc_name so I try some code like :
    @datalist.at(2).name()@
    or
    @datalist[2].name()@
    but neither of them works. Is there any method to get these items ?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      You need to cast the pointer to the correct type, recommended ways are: dynamic_cast or (because you have a QObject) "qobject_cast":http://qt-project.org/doc/qt-5/qobject.html#qobject_cast:
      @
      Champions* item2 = qobject_cast<Champions*>(datalist.at(2));
      if(item2)
      item2->name();
      @

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Since it's a QObject, the correct way is qobject_cast

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          minhblabla
          wrote on last edited by
          #4

          [quote author="Zlatomir" date="1405190039"]You need to cast the pointer to the correct type, recommended ways are: dynamic_cast or (because you have a QObject) "qobject_cast":http://qt-project.org/doc/qt-5/qobject.html#qobject_cast:
          @
          Champions* item2 = qobject_cast<Champions*>(datalist.at(2));
          if(item2)
          item2->name();
          @
          [/quote]

          Thanks a lot ! It works well for me !

          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