How to Use QTableView in Qt Database Programming

Source: Internet
Author: User

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.

 
 
  1. Tb1 = newQSqlTableModel (this, * dbR); // dbR is the data source in this application.
  2. Tb1-> setTable ("tmp"); // temporary prescription table

When the program needs to be displayed,

 
 
  1. TbList-> setModel (NULL); // clear the original dataset
  2. TbList-> setModel (tb1); // refresh the display

When editing input is required in the program

 
 
  1. QModelIndexmdidx = m_ui-> tbList-> model ()-> index (row, column); // obtain the location of the cell to be edited
  2. M_ui-> tbList-> setFocus (); // hand over the input focus to tbList
  3. M_ui-> tbList-> setCurrentIndex (mdidx); // you can specify the cell to be edited.
  4. 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.