First off, I am new somewhat new to using Qt. About two years ago I used it for a specific project, but I never had to dive too deep into the Model/View and never touched the SQL plug-ins. In my current project, I have a product table in which the user needs to be able to add, delete, edit, and sort:

CREATE TABLE Product (
ProductId INTEGER PRIMARY KEY AUTOINCREMENT,
PriceListId INTEGER NOT NULL,
ProductType INTEGER NOT NULL DEFAULT 1,
SortOrder INTEGER NOT NULL,
Description CHAR(80) NOT NULL,
Price NUMERIC(10,4) NOT NULL
)

The only two things displayed are the description and price. I have created a custom QSqlTableModel to display the price correctly. I have implemented my own delegate for the TableView to present the correct editor with the correct limitations on both fields. To add something, I have extra widgets to enter the description and price, then I use this SQL to insert it into the database:

Qt Code:
  1. INSERT INTO Product (PriceListId, SortOrder, Description, Price)
  2. VALUES(:priceListId1, (SELECT MAX(P.SortOrder) + 1 FROM Product P WHERE P.PriceListId = :priceListId2), :description, :price)
To copy to clipboard, switch view to plain text mode 

I am trying to figure out how to delete and to sort, but it is turning out doing it manually seems like a lot of work and I hafe a feeling there is a better way to leverage Qt to make life much easier.

  1. How does one go about adding a none trivial item to a ModelView?
  2. Like in the current .Net datagrids, is it possible to get a blank line at the bottom of the TableView for a user to add a new row?
  3. What is the standard way for a user to delete something from a TableView?
  4. Any and all suggestions on how I am implementing sorting is welcome, but at the DB level and how to implement it at the UI level is welcome.


See the next post for the ui file I generated for this

Sam