Add a new row at the end of QSqlTableModel while changing from last row
-
There are some important differences:
The proxy solution is the more general one. It will also work with any other (editable) model.
Really inserting a row to display, in your case means that you are actually creating a database record. That might not be what you want.
In terms of UX design: the new row is there to see for the user, instead of the surprise of a new row being created on tab from the last cell of the previous row. That reduces the surpise factor, and thus enhances the experience. Furthermore, it allows for the mouse to be used to create the new row by simply clicking it.
However, if you feel that your approach is more suitable for your application: by all means, go ahead!
-
[quote author="Andre" date="1340796957"]
In terms of UX design: the new row is there to see for the user, instead of the surprise of a new row being created on tab from the last cell of the previous row. That reduces the surpise factor, and thus enhances the experience. Furthermore, it allows for the mouse to be used to create the new row by simply clicking it.
[/quote]
How is your idea about the GUI for creating a new row? is it uses a push button?
Now I think the proxy approach is more suitable, and setData function can be used to validate data before submitting to the data base. -
Hi,
I cant write the function for rowCount, when executing/debugging it returns with following error
@Starting D:\QT\example\debug\example.exe...
ASSERT: "sourceIndex.isValid()" in file itemviews/qidentityproxymodel.cpp, line 169
D:\QT\example\debug\example.exe exited with code 3@@int MyProxyModel::rowCount(const QModelIndex &parent) const
{
return 10;
}@It is also not worked with
@int MyProxyModel::rowCount(const QModelIndex &parent) const
{
return QIdentityProxyModel::rowCount()+1;
}@But no problems with the following, (without adding a row)
@
int MyProxyModel::rowCount(const QModelIndex &parent) const
{
return QIdentityProxyModel::rowCount();
}@ -
Hi,
Too works to get a result, I think it is better to use insertRows() on proxyModel, because I don't know how to set ModelIndex for the new row, here is my code.
@Qt::ItemFlags MyProxyModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QIdentityProxyModel::flags(index);
if (index.column() > 0)
flags |= Qt::ItemIsEditable;
return flags;
}int MyProxyModel::rowCount(const QModelIndex &parent) const
{
return QIdentityProxyModel::rowCount()+1;
}QVariant MyProxyModel::data(const QModelIndex &index, int role) const
{
if(index.row() < QIdentityProxyModel::rowCount())
return QIdentityProxyModel::data(index, role);
return QVariant();
}bool MyProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(index.row() < QIdentityProxyModel::rowCount())
return QIdentityProxyModel::setData(index, value, role);return true;
}
QModelIndex MyProxyModel::index(int row, int column, const QModelIndex &parent) const
{
return QModelIndex(parent);
}@It returns all blank table
-
The (main) problem is in the implementation of index. It always returns the same index, for each item, and an invalid one at that.
Did you read the documentation for QAbstractItemModel::index()? It states:
[quote]When reimplementing this function in a subclass, call createIndex() to generate model indexes that other components can use to refer to items in your model.[/quote]However, even here, you could re-use the base class implementation for all but the last row.
-
Sorry I give up.
@Qt::ItemFlags MyProxyModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QIdentityProxyModel::flags(index);
if (index.column() > 0)
flags |= Qt::ItemIsEditable;
return flags;
}int MyProxyModel::rowCount(const QModelIndex &parent) const
{
return QIdentityProxyModel::rowCount()+1;
}QVariant MyProxyModel::data(const QModelIndex &index, int role) const
{
return QIdentityProxyModel::data(index, role);
}bool MyProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
return QIdentityProxyModel::setData(index, value, role);}
QModelIndex MyProxyModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column);
}@It displayed database data correctly. But newly entered values disappears as field changes. So can I use
@ int row = proxyModel->rowCount();
proxyModel->insertRow(row);@Are we coding to get the same result?
-
I realize this thread has been dead for some time, I would like to understand how can you return an index to row count()+1
I assume it should be something like (sorry translating from Python):
@QModelIndex MyProxyModel::index(int row, int column, const QModelIndex &parent) const
{
if (row == super(addItemProxyModel, self).rowCount())
return super(addItemProxyModel, self).createIndex(row, column, 0)
else:
return index(row, column, parent)
}@
But then you are returning a reference to a row that does not exist anywhere in the model, as you return it on the fly with data()
@QVariant MyProxyModel::data(const QModelIndex &index, int role) const
{
if (not index.isValid() or not (0 <= index.row() <= super(addItemProxyModel, self).rowCount()))
return QVariant();
if index.row == rowCount(index)
if role == Qt.DisplayRole
return QVariant(self.message)
else
return QVariant();
else
return QIdentityProxyModel::data(index, role);
}@