
The selection can be customized by specifying filters and sort conditions (see the QSqlTableModel class documentation for more details). For that reason we call the QSqlTableModel::select() function later on, populating the model with data from the table. Note that the QSqlTableModel::setTable() function does not select data from the table it only fetches its field information. Model - >setHeaderData( 2, Qt ::Horizontal, tr( "Last name")) įirst we create the data model and set the SQL database table we want the model to operate on. Model - >setHeaderData( 1, Qt ::Horizontal, tr( "First name")) Model - >setHeaderData( 0, Qt ::Horizontal, tr( "ID")) Model - >setEditStrategy( QSqlTableModel ::OnManualSubmit) TableEditor ::TableEditor( const QString &tableName, QWidget *parent) In the constructor we create and customize the data model and the various window elements: The class implementation consists of only two functions, the constructor and the submit() slot. If you want to use another database, simply modify this function's code. The createConnection function opens a connection to an in-memory SQLITE database and creates a test table. exec( "insert into images values(3, 'images/qt-project.png')") exec( "insert into images values(2, 'images/qt-creator.png')") exec( "insert into images values(1, 'images/qt-quick.png')") exec( "insert into images values(0, 'images/qt-logo.png')") exec( "create table images (itemid int, file varchar(20))") exec( "insert into items " "values(3, 3, 'Qt Project'," "'The Qt Project governs the open source development of Qt, " "allowing anyone wanting to contribute to join the effort " "through a meritocratic structure of approvers and " "maintainers.')") exec( "insert into items " "values(2, 2, 'Qt Creator'," "'Qt Creator is a powerful cross-platform integrated " "development environment (IDE), including UI design tools " "and on-device debugging.')") exec( "insert into items " "values(1, 1, 'Qt Quick'," "'Qt Quick is a collection of techniques designed to help " "developers create intuitive, modern-looking, and fluid " "user interfaces using a CSS & JavaScript like language.')")
#SL CACHE VIEWER SORTING FULL#
exec( "insert into items " "values(0, 0, 'Qt'," "'Qt is a full development framework with tools designed to " "streamline the creation of stunning applications and " "amazing user interfaces for desktop, embedded and mobile " "platforms.')") exec( "create table items (id int primary key," "imagefile int," "itemtype varchar(20)," "description varchar(100))") exec( "insert into person values(105, 'Maria', 'Papadopoulos')") exec( "insert into person values(104, 'Roberto', 'Robitaille')") exec( "insert into person values(103, 'Lars', 'Gordon')") exec( "insert into person values(102, 'Christine', 'Holand')") exec( "insert into person values(101, 'Danny', 'Young')") exec( "create table person (id int primary key, " "firstname varchar(20), lastname varchar(20))")

#SL CACHE VIEWER SORTING DRIVER#
Please read " "the Qt SQL driver documentation for information how " "to build it.\n\n" "Click Cancel to exit."), QMessageBox ::Cancel) QMessageBox ::critical(nullptr, QObject ::tr( "Cannot open database"), QObject ::tr( "Unable to establish a database connection.\n" "This example needs SQLite support.

QSqlDatabase db = QSqlDatabase ::addDatabase( "QSQLITE") It is defined in the connection.h file which is located in the sql example directory (all the examples in the sql directory use this function to connect to a database). The createConnection() function is a helper function provided for convenience. For that reason we need to declare a submit() slot in additon to the model and the editor's buttons.īefore we can use the TableEditor class, we must create a connection to the database containing the table we want to edit: int main( int argc, char *argv ) We are also going to show how a table view can be used to cache any changes to the data until the user explicitly requests to submit them. It is build on top of the lower-level QSqlQuery class which provides means of executing and manipulating SQL statements.

The QSqlTableModel class provides an editable data model making it possible to read and write database records from a single table. Note the QSqlTableModel variable declaration: As we will see in this example, the QSqlTableModel class can be used to provide data to view classes such as QTableView. The other is a pointer to the parent widget and is passed on to the base class constructor. The TableEditor constructor takes two arguments: The first is a reference to the database table the TableEditor object will operate on. Explicit TableEditor( const QString &tableName, QWidget *parent = nullptr)
