View Full Version : Passing QVariant a pointer

9th August 2010, 08:07
I'm both a Qt newbie and a C++ newbie, so bear with me.

I've created a QAbstractTableModel for use in a QTableView table.
The basic data in the model for each row of the table is a list (QList) of structures that hold three things:
1. A single letter name (type QChar)
2. An integer
3. A pointer to some data

The first two are to be displayed in the table. The third is not to be displayed in the QTableView, but is simply a pointer to data that is linked to the row.

I've implemented a "setData()" function for accessing the row data, as per an example I'm using for help, with the following parameters:

bool QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )

When I pass the setData function the first two variable types (QChar or int) 'value', there is no problem using the data; I simply use the QVariant conversion functions to convert from QVariant to the correct types and then place the value in the appropriate table entry.
But when I want to use setData to update the pointer, I'm not sure how to do it.... Can I pass QVariant a pointer?



#include <QAbstractTableModel>
#include <QList>

//Structure to store individual user information
struct userInfo
QChar userName;
int userNumber;
int *userData;

class userTableModel : public QAbstractTableModel

userTableModel(QObject *parent=0);
userTableModel(QList<userInfo> userInfoList, QObject *parent=0);

int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
QList<userInfo> getList();

QString systemName;
int systemNumber;

QList<userInfo> listOfUserInfo; //List of user info


setData() from userTableModel.cpp (note line 17)

bool userTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
if (index.isValid() && role == Qt::EditRole) {
int row = index.row();

//Select the correct row
userInfo p = listOfUserInfo.value(row);

//User's Name (Initial)
if(index.column() == 0)
p.userName = value.toChar();
//User's Number
else if(index.column() == 1)
p.userNumber = value.toInt();
//Pointer to User's Data
else if(index.column() == 2)
//Not Sure what to put here!!!

return false;

listOfUserInfo.replace(row, p);
emit(dataChanged(index, index));

return true;

return false;

How would I use the same function (setData()) to update the "userInfo.userData" pointer?

9th August 2010, 10:57
Try this
in setData():

else if(index.column() == 2)
*(p.userData) = value.toInt();

data() function should return *(p.userData) in case of second column

9th August 2010, 11:11
I have noticed line 7 in your code userInfo p = listOfUserInfo.value(row);
QList<T>::value() function returns copy of structure that is referenced by list. More over operator=() will call default copy constructor

userInfo &p = listOfUserInfo.value[row];