[Solved]QTableView storing Objects of a class.



  • 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 :)



  • 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 :)


Log in to reply
 

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