Reference Variables
Database View
A database view is not very different from an ordinary playlist view except that it permits you to see a larger collection of songs, organized in different ways to aid in the selection process.
Typically, a database view can be achieved with a table, a tree of lists, or a table of lists. The simplest way is to use the QTableView and then connect it to a model that represents the data.
Qt, when it is built with the correct options, can access SQL databases, so that we can use QSqlTableModel. Using this with the regular QTableView gives you an editable tabular view of a database table. The database view shown in Figure 25.11 looks like an ordinary table view, because it is! It's just hooked up to a database model.
Figure 25.11. QTableView of a database
Example 25.5. ../src/libs/filetagger/filetagger.sql
create database mp3db; use mp3db; grant all on mp3db.* to 'mp3db'@'localhost' identified by 'mp3dbpw'; grant all on mp3db.* to mp3db identified by 'mp3dbpw'; drop table FileTagger; CREATE TABLE FileTagger ( Artist varchar(100), TrackTitle varchar(100), AlbumTitle varchar(100), TrackNumber varchar(10), Genre varchar(20), Comment varchar(200), Preference varchar(20), Filename varchar(200), PRIMARY KEY(Filename), INDEX(Preference), INDEX(Artist), INDEX(Genre) ); |
Example 25.5 contains the SQL table definition, and Figure 25.12 contains a suggested initial design for the model.
1. |
Extend the Mp3TableModel so that it derives from PlayListModel and fits into your larger program.
|
2. |
Write a Library menu with an option to "import songs into library," which imports the songs into the database and updates the view.
|
Figure 25.12. Database table model