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. Convert Custom Class to QVariant
Qt 6.11 is out! See what's new in the release blog

Convert Custom Class to QVariant

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 7.9k 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.
  • F Offline
    F Offline
    Fuel
    wrote on last edited by
    #1

    I read that with Q_DECLARE_METATYPE i can add Custom Classes to the accepted Templates of QVariant. But i was thinking i can return now my Custom Class to a QVariant. This is my Code.

    QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role == Qt::DisplayRole)
        {
            if (index.column() == 0)
                return int(mItems.at(index.row())->Id);
            else if (index.column() == 1)
                return QString(mItems.at(index.row())->Date);
            else if (index.column() == 2)
                return StarTable(mItems.at(index.row())->Rating);
        }
    
        return QVariant();
    }
    

    But my Debugger says that StarTable cannot be converted to a QVariant. Why? StarTable has Q_DECLARE_METATYPE(StarTable)

    raven-worxR 1 Reply Last reply
    0
    • F Fuel

      I read that with Q_DECLARE_METATYPE i can add Custom Classes to the accepted Templates of QVariant. But i was thinking i can return now my Custom Class to a QVariant. This is my Code.

      QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
      {
          if (!index.isValid())
              return QVariant();
      
          if (role == Qt::DisplayRole)
          {
              if (index.column() == 0)
                  return int(mItems.at(index.row())->Id);
              else if (index.column() == 1)
                  return QString(mItems.at(index.row())->Date);
              else if (index.column() == 2)
                  return StarTable(mItems.at(index.row())->Rating);
          }
      
          return QVariant();
      }
      

      But my Debugger says that StarTable cannot be converted to a QVariant. Why? StarTable has Q_DECLARE_METATYPE(StarTable)

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Fuel

      QVariant::fromValue<StarTable>( starTableVar );
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      5

      • Login

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