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. [Solved]QTableView storing Objects of a class.
Forum Updated to NodeBB v4.3 + New Features

[Solved]QTableView storing Objects of a class.

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

    Hi,

    I am working on delegates and I have a requirement where i can store an object of a class in a QTableView. For eg
    I have a class UserInformation storing some values. For each entry i am storing the object using

    @info = new UserInformation();
    info->setFirstName(ui->txtFirstName->text());
    info->setLastName(ui->txtLastName->text());
    //etc etc
    QStandardItem item = new QStandardItem();
    item->setData(QVariant::fromValue<void
    >(info),Qt::DisplayRole);
    model->appendRow(item);@

    I have a custom TableViewDelegate subclassed from QStyledItemDelegate. Where i am overriding the paint() function and trying to set/display the values associated to a particular object of class UserInformation in the tableview.

    @void TableViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QStyledItemDelegate::paint(painter,option,index);

    QVariant variant = index.data(Qt::DisplayRole);
    UserInformation *childInfo = reinterpret_cast<UserInformation*>(variant.value<void*>());
    
    if(childInfo!=NULL)
    {  
          //here how can i set the values for each column in the QTableView
    }
    

    }@

    Even if i set the values its only painted in the first column. Also i need to get back the object on_tableView_clicked(.....) when a row is selected. But the object is valid only if first column is selected.

    I tried googling it but didnt get much help. I got a bit confused whether i need to override the paint() function of my custom delegate or i need to subclass QStandardItemModel and override data() / setData() function and do some changes there.

    Here is a sample "image":http://imageshack.us/f/192/capturexhj.png/

    Thanks :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      I found "address book example":http://doc.qt.nokia.com/4.7-snapshot/itemviews-addressbook.html
      and after going through the code i came to know that i need to subclass QAbstractTableModel and override the required functions. But still find this a long way to go as i need to store a single object for each/particular row.

      Thanks for your time :)

      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