In Qt, the content in Qtableview is displayed on the corresponding control in real time.

Source: Internet
Author: User

In Qt, the content in Qtableview is displayed on the corresponding control in real time.

I have been studying Qt for a while. I need to display the data in Qtableview on the corresponding control for a few days. I want to watch it for a long time and finally get it done. I 'd like to share it with you, it also serves as a learning note.

First, the content in my Qtableview is retrieved from the database and then displayed. The following is a data about a company's information types. This is a record in the database.

The effect is as follows:

Click a row of data above, and the corresponding data is displayed in lineEdit below. As shown in

Click the second data record and it will be displayed.


The results are very good. I am very happy to implement it perfectly. I will write the process. For Beginners, refer to it. Pass

First of all, I think it is necessary to establish a connection between the signal and the slot. Only in this way can we achieve real-time display. Which signal does it need? Since it is a mouse click, it must be a click signal.

<span style="font-family:KaiTi_GB2312;font-size:18px;"> connect(ui->Supplier_tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(ShowSupplyclick()));</span>
Associate the click signal of Qtableview on the interface with the self-written slot function.

The following is a self-written slot function:


<Span style = "font-family: KaiTi_GB2312; font-size: 18px;">/** get the content of the data table clicked by the mouse, and display it on the corresponding control */void Basic_Data_Dialog: ShowSupplyclick () {static QSqlQueryModel supplymodel (ui-> Supplier_tableView); supplymodel. setQuery (QString ("select * from Supply;"); int row = ui-> Supplier_tableView-> currentIndex (). row (); QSqlRecord record = supplymodel. record (row); ui-> EditsupId-> setText (record. value (0 ). toString (); ui-> EditSupName-> setText (record. value (1 ). toString (); ui-> EditSupAdress-> setText (record. value (2 ). toString (); ui-> EditSupPhon-> setText (record. value (3 ). toString (); ui-> EditSupMail-> setText (record. value (4 ). toString (); ui-> EditSupRemark-> setText (record. value (5 ). toString () ;}</span>

The code is detailed, so you don't need to introduce it too much. The above record. value (0 ). toString () is the attribute of a column in the data table. It must correspond to the column in the data table. Otherwise, the displayed content does not match.

Other operations on the database are simple, that is, adding, deleting, and modifying the database.

Only when you do something can you discover the fun.


A three-Stream Environment, first-class people ......

Related Article

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.