Passing QVariant a pointer
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:
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?
userTableModel.h
Code:
#ifndef USERTABLEMODEL_H
#define USERTABLEMODEL_H
#include <QAbstractTableModel>
#include <QList>
//Structure to store individual user information
struct userInfo
{
int userNumber;
int *userData;
};
{
Q_OBJECT
public:
userTableModel
(QList<userInfo> userInfoList,
QObject *parent
=0);
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
QList<userInfo> getList();
private:
int systemNumber;
QList<userInfo> listOfUserInfo; //List of user info
};
#endif
setData() from userTableModel.cpp (note line 17)
Code:
{
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!!!
else
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?
Re: Passing QVariant a pointer
Try this
in setData():
Code:
else if(index.column() == 2)
*(p.userData) = value.toInt();
data() function should return *(p.userData) in case of second column
Re: Passing QVariant a pointer
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
Try
Code:
userInfo &p = listOfUserInfo.value[row];