Qt DatabaseProgrammingQTableViewThe usage is the content to be introduced in this article, mainly to learnQTMediumDatabaseAboutQTableViewFor more information, see this document.
QTableView isQTIs used to provide a dataset as a table to the user. It is similar to DBGrid in C ++ Builder. Frankly speaking, DBGrid is easier to use than QTableView. However, QTableView provides more flexibility while being difficult to use.
The following procedure is available in a drug price planning module:
After a prescription project is retrieved, the project is displayed in QTableView, and the quantity field to be edited is provided to the user for input. After the user enters the field for confirmation, the project is left in the list, then, start the next project search entry.
The implementation process is as follows:
The entered project is retained in the temporary table tmp. The QTableView on the interface is named tbList, and the Model associated with tbList is named tb1. After the search is successful, insert the search results to the temporary table and provide the fields to be edited.
- Tb1 = newQSqlTableModel (this, * dbR); // dbR is the data source in this application.
- Tb1-> setTable ("tmp"); // temporary prescription table
When the program needs to be displayed,
- TbList-> setModel (NULL); // clear the original dataset
- TbList-> setModel (tb1); // refresh the display
When editing input is required in the program
- QModelIndexmdidx = m_ui-> tbList-> model ()-> index (row, column); // obtain the location of the cell to be edited
- M_ui-> tbList-> setFocus (); // hand over the input focus to tbList
- M_ui-> tbList-> setCurrentIndex (mdidx); // you can specify the cell to be edited.
- M_ui-> tbList-> edit (mdidx); // start editing
Pay attention to one problem. DirectionQTableViewWhen adding a record, the field must be complete and there must be no blank fields; otherwise, the results cannot be saved. Remember.
To restrict user input, for example, you can only enter the specified data type in the specified field, you can use QItemDelegate.
Summary:Qt DatabaseProgrammingQTableViewThe content of the usage method has been introduced. I hope this article will help you!