Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use Person class in model in Qt MVC
Forum Updated to NodeBB v4.3 + New Features

How to use Person class in model in Qt MVC

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.0k 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.
  • T Offline
    T Offline
    tokafr
    wrote on last edited by
    #1

    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!

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

      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> ?

      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
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1
        • T Offline
          T Offline
          tokafr
          wrote on last edited by
          #4

          thank you all, Q_DECLARE_METATYPE works fine.

          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