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 )
To copy to clipboard, switch view to plain text mode
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
#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
#ifndef USERTABLEMODEL_H
#define USERTABLEMODEL_H
#include <QAbstractTableModel>
#include <QList>
//Structure to store individual user information
struct userInfo
{
QChar userName;
int userNumber;
int *userData;
};
class userTableModel : public QAbstractTableModel
{
Q_OBJECT
public:
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();
private:
QString systemName;
int systemNumber;
QList<userInfo> listOfUserInfo; //List of user info
};
#endif
To copy to clipboard, switch view to plain text mode
setData() from userTableModel.cpp (note line 17)
{
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;
}
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!!!
else
return false;
listOfUserInfo.replace(row, p);
emit(dataChanged(index, index));
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
How would I use the same function (setData()) to update the "userInfo.userData" pointer?
Bookmarks