#include <QApplication>
#include <QTableView>
#include <QSqlQueryModel>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSortFilterProxyModel>
bool createData()
{
db.setDatabaseName(":memory:");
if (db.open()) {
query.exec("create table test (id int, name text, value real)");
query.exec("insert into test values(101, 'Name 1', 0.3)");
query.exec("insert into test values(102, 'Name 2', 0.34)");
query.exec("insert into test values(103, 'Name 3', -0.1)");
query.exec("insert into test values(104, 'Name 4', 4.0)");
query.exec("insert into test values(105, 'Name 5', 0.11)");
return true;
}
return false;
}
int main(int argc, char *argv[])
{
if (!createData())
return 1;
model.setQuery("select * from test");
proxy.setSourceModel(&model);
view.setSortingEnabled(true);
view.setModel(&proxy);
view.show();
return app.exec();
}
#include <QApplication>
#include <QTableView>
#include <QSqlQueryModel>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSortFilterProxyModel>
bool createData()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (db.open()) {
QSqlQuery query;
query.exec("create table test (id int, name text, value real)");
query.exec("insert into test values(101, 'Name 1', 0.3)");
query.exec("insert into test values(102, 'Name 2', 0.34)");
query.exec("insert into test values(103, 'Name 3', -0.1)");
query.exec("insert into test values(104, 'Name 4', 4.0)");
query.exec("insert into test values(105, 'Name 5', 0.11)");
return true;
}
return false;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!createData())
return 1;
QSqlQueryModel model;
model.setQuery("select * from test");
QSortFilterProxyModel proxy;
proxy.setSourceModel(&model);
QTableView view;
view.setSortingEnabled(true);
view.setModel(&proxy);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks