Originally Posted by
panoss
I haven't done anything yet, I 'm trying to become familiar with models and their subclassing,...
Without trying anything, this is how far you can get familiar with Qt MVC. I suggest to start implementing some basic working model / view, you will get a more clarity.
Originally Posted by
panoss
If I remove the empty first row from my db table, I shall add it in the combo 's model with model->insertRow()?
If I do this, it will also be added in the source model and in the table 's model, right?
No, it should taken care by overriding
QSortFilterProxyModel::rowCount()
To copy to clipboard, switch view to plain text mode
Originally Posted by
panoss
How shall I concatenate the 2 fields for the combo?
FirstName + LastName to be replaced with FullName?
Override the
QSortFilterProxyModel::data()
To copy to clipboard, switch view to plain text mode
Added after 1 51 minutes:
For a head start, this is how ComboBox model will look like (working code)
{
public:
{ }
{
if(!sourceModel())
if(!sourceIndex.isValid())
return index(sourceIndex.row() + 1, sourceIndex.column());
}
{
if(!proxyIndex.isValid())
if(sourceModel())
{
if(proxyIndex.row() == 0)
else
return sourceModel()->index(proxyIndex.row() - 1, proxyIndex.column());
}
}
protected:
{
if(!parent.isValid())
return createIndex(row, column);
}
{
}
{
if(!parent.isValid() && sourceModel())
return 0;
}
{
if(!parent.isValid() && sourceModel())
return 0;
}
{
if(index.row() == 0)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
return sourceModel()->flags(mapToSource(index));
}
{
if(!index.isValid() || !sourceModel())
int row = index.row();
if(row > 0)
{
if(role == Qt::DisplayRole)
{
QModelIndex firstNameIndex
= mapToSource
(this
->index
(row,
0));
QModelIndex lastNameIndex
= mapToSource
(this
->index
(row,
1));
.arg(firstNameIndex.data().toString())
.arg(lastNameIndex.data().toString());
}
else
return mapToSource(this->index(row, 0)).data(role);
}
if(role == Qt::DisplayRole)
return QString("--no selection--");
if(role == Qt::ForegroundRole)
}
};
class ComboModel : public QAbstractProxyModel
{
public:
ComboModel(QObject * parent = 0)
: QAbstractProxyModel(parent)
{ }
QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
{
if(!sourceModel())
return QModelIndex();
if(!sourceIndex.isValid())
return QModelIndex();
return index(sourceIndex.row() + 1, sourceIndex.column());
}
QModelIndex mapToSource(const QModelIndex & proxyIndex) const
{
if(!proxyIndex.isValid())
return QModelIndex();
if(sourceModel())
{
if(proxyIndex.row() == 0)
return QModelIndex();
else
return sourceModel()->index(proxyIndex.row() - 1, proxyIndex.column());
}
return QModelIndex();
}
protected:
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
{
if(!parent.isValid())
return createIndex(row, column);
return QModelIndex();
}
QModelIndex parent(const QModelIndex & /*child*/) const
{
return QModelIndex();
}
int rowCount(const QModelIndex & parent) const
{
if(!parent.isValid() && sourceModel())
return sourceModel()->rowCount(QModelIndex()) + 1;
return 0;
}
int columnCount(const QModelIndex & parent) const
{
if(!parent.isValid() && sourceModel())
return sourceModel()->columnCount(QModelIndex());
return 0;
}
Qt::ItemFlags flags(const QModelIndex & index) const
{
if(index.row() == 0)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
return sourceModel()->flags(mapToSource(index));
}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
{
if(!index.isValid() || !sourceModel())
return QVariant();
int row = index.row();
if(row > 0)
{
if(role == Qt::DisplayRole)
{
QModelIndex firstNameIndex = mapToSource(this->index(row, 0));
QModelIndex lastNameIndex = mapToSource(this->index(row, 1));
return QString("%1, %2")
.arg(firstNameIndex.data().toString())
.arg(lastNameIndex.data().toString());
}
else
return mapToSource(this->index(row, 0)).data(role);
}
if(role == Qt::DisplayRole)
return QString("--no selection--");
if(role == Qt::ForegroundRole)
return QVariant(QColor(Qt::lightGray));
return QVariant();
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks