Thanks
Now I know QSqlTableModel::insertRows() adds a new empty row where it is given by the row parameter, but when I set data for that row, the position of the row changes and it will appear at the end (actually it happens when I set data for the path column in the table, you can see table creation command below).
I think the view is not sorted because QTableView::isSortingEnabled() returns false.
The code for creating table:
bool LibMan
::createLib(const QString &libName
) {
return query.exec("CREATE TABLE " + libName + "(\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
title VARCHAR(255),\
path VARCHAR(255) NOT NULL)");
}
bool LibMan::createLib(const QString &libName)
{
QSqlQuery query;
return query.exec("CREATE TABLE " + libName + "(\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
title VARCHAR(255),\
path VARCHAR(255) NOT NULL)");
}
To copy to clipboard, switch view to plain text mode
Inserting new data:
void LibMan::insertRecords()
{
lastIndex = 0;
if (insertionList->count() > 1)
{
}
tabModel->insertRows(0, insertionList->count());
tabModel->submitAll();
}
void LibMan::insertRecords()
{
lastIndex = 0;
if (insertionList->count() > 1)
{
tabModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
}
tabModel->insertRows(0, insertionList->count());
tabModel->submitAll();
tabModel->setEditStrategy(QSqlTableModel::OnRowChange);
}
To copy to clipboard, switch view to plain text mode
Initializing:
tabModel = new SqlTableModel(this);
tabModel->setTable("pap");
tabModel
->setHeaderData
(1,Qt
::Horizontal,
QObject::tr("Title"));
tabModel
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Location"));
proxyModel = new SortFilterProxyModel(this);
proxyModel->setSourceModel(tabModel);
proxyModel->setDynamicSortFilter(true);
proxyModel->setFilterKeyColumn(1);
ui->tableView->setModel(tabModel);
// ui->tableView->setModel(proxyModel);
ui->tableView->setColumnHidden(0, true);
ui->tableView->resizeColumnsToContents();
// if (ui->tableView->isSortingEnabled()) return;
QObject::connect(tabModel,
SIGNAL(primeInsert
(int,QSqlRecord
&)),
this,
SLOT(fill
(int,QSqlRecord
&)));
// tabModel->insertRows(24,1);
tabModel = new SqlTableModel(this);
tabModel->setTable("pap");
tabModel->setHeaderData(1,Qt::Horizontal, QObject::tr("Title"));
tabModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Location"));
tabModel->setEditStrategy(QSqlTableModel::OnRowChange);
proxyModel = new SortFilterProxyModel(this);
proxyModel->setSourceModel(tabModel);
proxyModel->setDynamicSortFilter(true);
proxyModel->setFilterKeyColumn(1);
ui->tableView->setModel(tabModel);
// ui->tableView->setModel(proxyModel);
ui->tableView->setColumnHidden(0, true);
ui->tableView->resizeColumnsToContents();
// if (ui->tableView->isSortingEnabled()) return;
QObject::connect(tabModel, SIGNAL(primeInsert(int,QSqlRecord&)), this, SLOT(fill(int,QSqlRecord&)));
QObject::connect(proxyModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(selectEdited(QModelIndex,QModelIndex)));
// tabModel->insertRows(24,1);
To copy to clipboard, switch view to plain text mode
SqlTableModel is a subclass of QSqlTableModel, does nothing special except coloring empty fields.
And the slot fill():
{
record.setValue(2, insertionList->at(lastIndex++));
}
void LibMan::fill(int row, QSqlRecord &record)
{
record.setValue(2, insertionList->at(lastIndex++));
}
To copy to clipboard, switch view to plain text mode
Added after 55 minutes:
ChrisW67, I have changed your example and now it is a working example that demonstrates the problem:
#include <QtGui>
#include <QtSql>
#include <QDebug>
void createConnection()
{
db.setDatabaseName("D:\\tempDB\\mydb5");
if (db.open()) {
query.exec("CREATE TABLE " + str + "(\
id INTEGER PRIMARY KEY,\
title VARCHAR(255),\
path VARCHAR(255) NOT NULL)");
query.exec("insert into pap values('1', 't1', 'test1')");
query.exec("insert into pap values('2', 't2', 'test2')");
query.exec("insert into pap values('3', 't3', 'test3')");
query.exec("insert into pap values('4', 't4', 'test4')");
query.exec("insert into pap values('5', 't5', 'test5')");
}
}
int main(int argc, char *argv[])
{
createConnection();
model.setTable("pap");
model.select();
view.setModel(&model);
view.resize(640, 480);
view.show();
model.insertRows(2,1);
model.setData(model.index(0,2), "temp");
return app.exec();
}
#include <QtGui>
#include <QtSql>
#include <QDebug>
void createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("D:\\tempDB\\mydb5");
if (db.open()) {
QSqlQuery query;
QString str = "pap";
query.exec("CREATE TABLE " + str + "(\
id INTEGER PRIMARY KEY,\
title VARCHAR(255),\
path VARCHAR(255) NOT NULL)");
query.exec("insert into pap values('1', 't1', 'test1')");
query.exec("insert into pap values('2', 't2', 'test2')");
query.exec("insert into pap values('3', 't3', 'test3')");
query.exec("insert into pap values('4', 't4', 'test4')");
query.exec("insert into pap values('5', 't5', 'test5')");
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
createConnection();
QSqlTableModel model;
model.setTable("pap");
model.setEditStrategy(QSqlTableModel::OnRowChange);
model.select();
QTableView view;
view.setModel(&model);
view.setEditTriggers(QAbstractItemView::DoubleClicked);
view.resize(640, 480);
view.show();
model.insertRows(2,1);
model.setData(model.index(0,2), "temp");
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Now if you change the data in the incomplete row, it will appear at the end of the list. It seems that it is sorted by column id, but sorting is not enabled
Bookmarks