{
Q_OBJECT
enum sessionTypesEnum {
secureShell,
remoteFileBrowser
};
struct connectionData {
int port;
sessionTypesEnum sessionType;
};
public:
ConnectionListModel
(QObject *parent
= 0);
~ConnectionListModel();
//Required items to make this model work
//required items to be able to edit items
//Required to be able to resize the model (add & remove shit)
private:
QList<connectionData> connectionList;
};
ConnectionListModel
::ConnectionListModel(QObject *parent
){
}
ConnectionListModel::~ConnectionListModel()
{
}
int ConnectionListModel
::rowCount(const QModelIndex &parent
) const {
return connectionList.count();
}
Qt
::ItemFlags ConnectionListModel
::flags(const QModelIndex &index
) const{
if (!index.isValid())
return Qt::ItemIsEnabled;
}
{
if ( ! index.isValid())
int row = index.row();
if ( row < 0 || row >= connectionList.count() ) //Is the row invalid?
if (role == Qt::DisplayRole)
{
switch( index.column() ){
case 0: return connectionList[row].name;
case 1: return connectionList[row].hostAddress;
case 2: return connectionList[row].username;
case 3: return connectionList[row].password;
case 4: return connectionList[row].port;
case 5: return connectionList[row].sessionType;
}
}
else
}
{
if (index.isValid() && role == Qt::DisplayRole)
{
int row = index.row();
//Lets cast our QVariant so we can get the data out
const connectionData *newData = qvariant_cast<connectionData>( value );
//get a pointer to the list item so we can change its contents
connectionList[row].name = newData.name;
connectionList[row].hostAddress = newData.hostAddress;
connectionList[row].username = newData.username;
connectionList[row].password = newData.password;
connectionList[row].port = newData.port;
connectionList[row].sessionType = newData.sessionType;
emit dataChanged(index, index);
return true;
}
return false;
}
bool ConnectionListModel
::insertRows(int row,
int count,
const QModelIndex &parent
) {
beginInsertRows(parent, row, row +count-1);
//Not finished!
endInsertRows();
}
bool ConnectionListModel
::removeRows(int row,
int count,
const QModelIndex &parent
) {
beginRemoveRows(parent, row, row+count+1);
//Not finished!
endRemoveRows();
}
class ConnectionListModel : public QAbstractListModel
{
Q_OBJECT
enum sessionTypesEnum {
secureShell,
remoteFileBrowser
};
struct connectionData {
QString name;
QString hostAddress;
QString username;
QString password;
int port;
sessionTypesEnum sessionType;
};
public:
ConnectionListModel(QObject *parent = 0);
~ConnectionListModel();
//Required items to make this model work
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
//required items to be able to edit items
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex &index) const;
//Required to be able to resize the model (add & remove shit)
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
private:
QList<connectionData> connectionList;
};
ConnectionListModel::ConnectionListModel(QObject *parent)
: QAbstractListModel(parent)
{
}
ConnectionListModel::~ConnectionListModel()
{
}
int ConnectionListModel::rowCount(const QModelIndex &parent) const
{
return connectionList.count();
}
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
{
if ( ! index.isValid())
return QVariant();
int row = index.row();
if ( row < 0 || row >= connectionList.count() ) //Is the row invalid?
return QVariant();
if (role == Qt::DisplayRole)
{
switch( index.column() ){
case 0: return connectionList[row].name;
case 1: return connectionList[row].hostAddress;
case 2: return connectionList[row].username;
case 3: return connectionList[row].password;
case 4: return connectionList[row].port;
case 5: return connectionList[row].sessionType;
return QVariant();
}
}
else
return QVariant();
}
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::DisplayRole)
{
int row = index.row();
//Lets cast our QVariant so we can get the data out
const connectionData *newData = qvariant_cast<connectionData>( value );
//get a pointer to the list item so we can change its contents
connectionList[row].name = newData.name;
connectionList[row].hostAddress = newData.hostAddress;
connectionList[row].username = newData.username;
connectionList[row].password = newData.password;
connectionList[row].port = newData.port;
connectionList[row].sessionType = newData.sessionType;
emit dataChanged(index, index);
return true;
}
return false;
}
bool ConnectionListModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(parent, row, row +count-1);
//Not finished!
endInsertRows();
}
bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row+count+1);
//Not finished!
endRemoveRows();
}
To copy to clipboard, switch view to plain text mode
The problem that I have though is that the qvariant_cast<connectionData> doesn't work. I'm not really sure how to pass in the connectionData object using a QVariant.
Bookmarks