Important: Please read the Qt Code of Conduct -

[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();
    //etc etc
    QStandardItem item = new QStandardItem();

    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

    QVariant variant =;
    UserInformation *childInfo = reinterpret_cast<UserInformation*>(variant.value<void*>());
          //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":

    Thanks :)

  • I found "address book example":
    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