How to use Person class in model in Qt MVC



  • Hello!

    I have such task. I want to display information(including image) about person on button.
    I have QAbstractTable model to set these buttons with that information. I created my own person class
    and give the constructor parameters(name, lastName, date of birth, and path to image);
    I created getter and setter methods to handle this data. so now I want to use this class in my model.
    In the model class I have a QList<Person*>. So the problem is that model methods work with QVariant data type and I cannot get the Person object in the Delegate class. How can I fix it?

    Thank you!


  • Lifetime Qt Champion

    Hi,

    You have to use "Q_DECLARE_METATYPE":http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE to register your custom type then you can use it through QVariant.

    By the way, are you sure that you need a QList<Person *> and not just a QList<Person> ?



  • I have implemented the same with something like the following. I have implemented PersonModel class with following information.

    @class PersonModel : public QAbstractTableModel
    {
    Q_OBJECT
    QList<Person> mydata;@

    Using QModelIndex with appropriate row/column and role information you should be able to fetch the data.



  • thank you all, Q_DECLARE_METATYPE works fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.