Qt-implements simple student information management _ initial knowledge QT, qt-Initial Knowledge qt

Source: Internet
Author: User

Qt-implements simple student information management _ initial knowledge QT, qt-Initial Knowledge qt

Project Introduction: using Qt to implement simple student information management

Function: add, delete, query, and modify information.

If you don't talk nonsense, go to the topic: (I just learned that the interface is not very beautiful. Please forgive me)

Main Interface-mainWindow & login

1. Add students-

1. components used include Dialog window, Label, Line Edit, Spin Box, radio button, check Box, and push button.

2. Implementation ideas:

1. obtain information on the panel

2.write the corresponding information to A. txt file.

3. Implementation steps:

1. Change the tag name. After modifying the tag, we can call it easily.

2. Add a button group for the Gender and interest components, and use the button group to determine which option is selected

3. when getting interest, we need to create a linked list, save the button information, use the for loop traversal to determine whether to press the list, read the information, and use a small trick when reading the information, add a separator between each field to facilitate the following file operations

4. Before writing, if you add the separator according to the previous delimiter, there will be an extra delimiter at the end of the last interest, which must be processed separately.

// 1. obtain the student information entered by the user QString szInfo; szInfo + = ui-> nameLine-> text (); // name szInfo + = ""; szInfo + = ui-> numLine-> text (); // student ID szInfo + = ""; szInfo + = ui-> sexBG-> checkedButton ()-> text (); // gender szInfo + = ""; szInfo + = QString (). setNum (ui-> ageSpin-> value (); // age szInfo + = ""; szInfo + = ui-> colleageCB-> currentText (); // Department szInfo + = ""; QList <qiniactbutton *> btnList = ui-> inBG-> buttons (); for (int I = 0; I <btnList. length (); ++ I) {if (btnList. at (I)-> isChecked () // checks whether the button is pressed and returns: bool {szInfo + = btnList. at (I)-> text (); szInfo + = "-" ;}} szInfo. remove (szInfo. length ()-1, 1 );

The following file operations refer to the implementation methods provided by the help document. I understand that the information is read into the file using an input stream (if any, I hope you can correct it .)

 QFile file("studenInfo.txt");     if (!file.open(QIODevice::WriteOnly |QIODevice::Append| QIODevice::Text))         return;    QTextStream out(&file);    out << szInfo << '\n';    QMessageBox::information(NULL,"iuput state","input success!");

 

2. Browse students

1. The main components used include Table View and Combo Box.

2. Implementation ideas

1. Read the student information that has been added first

2. initialize the model for displaying student information in the table view component.

3. The information read by comparing the selected Query Information and Its keywords

4. display matching information

3. Implementation steps

1. create an important member variable (QList <QString> m_list) to store the Read File Information, because the read information is a whole line of information, we need to cut the whole line of information into several small pieces of information we need. The cut information is stored by the QStringList type variable.

2. after the information is cut, you can find that there are four fields in the query method. The cut Blocks corresponding to the fields are-, and can be saved in an array, in this way, we can save one judgment. We only need to match the corresponding subscript with the obtained keyword, that is, output the corresponding information.

3. Add a model, that is, add a header to facilitate information viewing. You can create a QStandardItemModel object so that the object can call its setHorizontalHeaderItem method to set the header name.

Void queryDlg: readFile () {m_list.clear (); QFile file ("studenInfo.txt"); if (! File. open (QIODevice: ReadOnly | QIODevice: Text) return; QTextStream in (& file); // at. end () determines whether the file is at the end of the while (! In. atEnd () {// readLine () reads a row of QString line = in. readLine (); // Add the student information in the file to the linked list object to m_list.append (line);} return;}/* initialize the list information */void queryDlg: initModel () {// create a data model m_pmodel = new QStandardItemModel; // set the column header name m_pmodel-> setHorizontalHeaderItem (0, new QStandardItem ("name"); m_pmodel-> setHorizontalHeaderItem (1, new QStandardItem ("number"); m_pmodel-> setHorizontalHeaderItem (2, new QStandardItem ("sex"); m_pmodel-> setHorizontalHeaderItem (3, new QStandardItem ("age"); m_pmodel-> setHorizontalHeaderItem (4, new QStandardItem ("college"); m_pmodel-> setHorizontalHeaderItem (5, new QStandardItem ("interes"); // Add to table view ui-> dataTab-> setModel (m_pmodel); return ;} /* display all student information */void queryDlg: display (int iIndex, QString szKey) {m_pmodel-> clear (); initModel (); int iRow = 0; for (int I = 0; I <m_list.size (); ++ I) {// obtain the information of the high school student in the linked list. QString szInfo = m_list.at (I); // separate the student information, become a substring QStringList sList = szInfo. split (""); // display data if (iIndex =-1 | sList. at (iIndex ). compare (szKey) = 0) {for (int j = 0; j <sList. size (); ++ j) {m_pmodel-> setItem (iRow, j, new QStandardItem (sList. at (j) ;}++ iRow ;}}}

3. Modify students (the student modification interface is basically the same as the student addition interface, but the "modify" and "Next" buttons are added)

1. Implementation ideas

1. Obtain the Student File Information (same as reading the file above)

2. obtain all information in the panel (same as the method obtained above)

3. Update the information of the corresponding locations in the linked list storing Student Information

4. Write information in the linked list to the file

2. Implementation steps

1. Read the file .... This is the same as above.

2. Obtain the Panel information, just like the student added above

3. set the initial value of a member variable (m_iCurrIndex) under the initial student subject to 0 by default, because the row number of the Information starts from 0, and it is also the identifier for updating the data below, after you click "modify", the information at the corresponding location (m_iCurrIndex) in m_list needs to be modified again. This m_iCurrIndex is very important in this implementation, it is easy to implement, update information, and browse information.

4. Finally, read the information in the linked list into the file. The method is the same as above. There are not many introductions here.

Void modDlg: display () {initUI (); if (m_list.size () = 0) return; // obtain the student information QString szInfo = m_list.at (m_iCurrIndex ); // break down the student information QStringList sList = szInfo. split (""); if (sList. size () = 0) return; // 1. display name ui-> nameLine-> setText (sList. at (0); // 2. display student ID ui-> numLine-> setText (sList. at (1); // 3. display age ui-> ageSpin-> setValue (sList. at (3 ). toInt (); // 4. display gender QList <qiniactbutton *> sexList = ui-> sexBG-> Buttons (); // obtain the button information for (int I = 0; I <sexList. size (); ++ I) {if (sexList. at (I)-> text (). compare (sList. at (2) = 0) // compare the obtained information with the Panel information sexList. at (I)-> setChecked (true); // set the information to true} // 5. display Department // implementation method: first find the subscript corresponding to the corresponding text, and then display the text int index = ui-> colleageCB-> findText (sList. at (4); ui-> colleageCB-> setCurrentIndex (index); // 6. show interest QStringList inList = sList. at (5 ). split ("-"); for (int I = 0; I <inList. size (); ++ I) {checkInBG (inList. at (I);} return;} void modDlg: checkInBG (QString in) {QList <q1_actbutton *> inList = ui-> inBG-> buttons (); for (int I = 0; I <inList. size (); ++ I) {if (inList. at (I)-> text (). compare (in) = 0) inList. at (I)-> setChecked (true);} return;} void modDlg: on_nextBtn_clicked () {if (m_iCurrIndex ++> = m_list.size ()-1) {int ret = QMessageBox: information (NULL, "information", "Are You restart? "," Yes "," No "); if (0 = ret) m_iCurrIndex = 0; else return;} display (); return ;}

4. Delete students (the component is very simple and will not be introduced too much)

1. Implementation ideas

1. Get keywords entered by users

2. Matching Keywords of Read File segmentation Information

3. Write files

2. Implementation steps (the subsequent steps are basically the same, and some methods can be used directly)

1. Create an object to obtain the keyword information, type: QString

2. Read the file and store the data in the linked list.

3. cyclically traverse the chain table, split the chain table, and make it a sub-string and compare it with the obtained keyword information. If it does not match skipping, It is not saved to another QString object, finally, write it into the file. Note: The Append is no longer used here. You need to remove it and write it from the first row.

 

M_list.clear (); // 1. read user input information QString szInfo1, szInfo2, szInfo, szInfo3; QStringList sList; szInfo1 + = ui-> nameLine-> text (); // name szInfo2 + = ui-> numLine-> text (); // student ID // 2.read the information in studen.txt and store it in QFile file1 ("studenInfo.txt"); if (! File1.open (QIODevice: ReadOnly | QIODevice: Text) return; QTextStream in (& file1); // at. end () determines whether the file is at the end of the while (! In. atEnd () {// readLine () reads a row of QString line = in. readLine (); // Add the student information in the file to the linked list object to m_list.append (line);} QFile file ("studenInfo.txt"); if (! File. open (QIODevice: WriteOnly | QIODevice: Text) return; for (int I = 0; I <m_list.size (); ++ I) {// obtain the student information in the linked list. szInfo = m_list.at (I); // separate the student information to become the sub-string sList = szInfo. split (""); // 3. delete if (! (SList. at (0 ). compare (szInfo1) = 0 & sList. at (1 ). compare (szInfo2) = 0) {szInfo3 + = m_list.at (I); szInfo3 + = '\ n' ;}} QTextStream out (& file); out <szInfo3; QMessageBox: information (NULL, "Prompt", "Delete Success! ");

 

Some fragmented methods: for example, 1. Add a resource image: select a resource file, change the prefix, and then click Add file. 2. Change the UI background, font size, font color, right-click the selected component, and choose change style. 3. Change the window name. The windowTitle option in the lower right corner can be changed directly. 4. Set the window to be stretched. Set the minsize and maxsize of the window to the same value. 5. Help> index: You can view all classes and usage methods in the QT library. And so on

Summary: This project training has gained a lot, but there are also many shortcomings. There are hundreds of errors when writing code, but I learned a lot in the process of finding and correcting errors, it also showed me the power of programming languages. I used to feel that these languages were too abstract and did not know what to do with them. After these small trainings, I discovered the mysteries of the language, I believe that I will go further and further on this road.

 

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.