In this article, we will briefly describe how to design a platform to simulate the SMS sending and receiving effect in QT.
The following shows the session module as follows:
The title of the interface is displayed in "session" first, and the receiver has a drop-down menu. The drop-down menu initially shows "NONE". The drop-down menu stores all contacts in the phone book, you can select a contact. After a contact is selected, the corresponding number corresponding to the selected contact is displayed in the corresponding text box, and the title of the session window changes accordingly, as shown in:
After selecting the recipient, click "send". If the sent content is blank, the message "The sent content cannot be blank" appears ", the content sent by the reminder sender cannot be blank, as shown below:
Of course, if the content to be sent is not blank but the recipient is not selected, the message "select the recipient" appears, reminding the sender to select the recipient, as shown in:
When the recipient is selected and the sent content is not empty, you can simulate sending and receiving of text messages. Next we will use the test tool to simulate receiving and sending of text messages. In the test tool, the user name is "Li Liqun" and the number is "8901". Other information can also be set. In the session window, select "Li Liqun" from the receiver drop-down menu. In the test tool's receiver drop-down menu, select the previously set user information. For example, if "Wei Chao" is selected ", the "4567" number is displayed on the page, as shown in:
Text message sending and receiving simulation refers to sending and receiving text messages. Two people have been selected for communication. One is "Li Liqun", the number is "8901", and the other is "Wei Chao ", the number is 4567 ".
The dialog content is as follows:
Wei Chao: Hello, how are you doing recently?
Li Liqun: Fortunately, I participated in a ZTE competition. What about you?
Wei Chao: It's okay, so please be busy!
Li Liqun: Okay. Next time you have dinner, bye-bye.
Wei Chao: Bye bye
The simulated text message sending and receiving interface is as follows. In the send content dialog box, write the information to be sent and click send. In this case, the sender name of the message is recorded in the session record dialog box, time and content of the message. When receiving the message, the sender and content of the received message are added to the session record dialog box.
If you want to send the email to other contacts after sending the email, you only need to change the contacts in the drop-down list. The original sending and receiving records are cleared.
Running the preceding dialog content in a program can be as follows:
The text message simulated sending and receiving function is implemented above. The following describes the blacklist and spam text message filtering.
The first is the blacklist. Each time we receive the information sent from the blacklist, we need to save the sent information to the recycle bin. To facilitate testing, we set a blacklist in the phone book, the name is "Ding Jian" and the number is "1323". We define an XML file to store the received information. In this file, each piece of information is stored at the same time. The following is an example.
Use the test tool to send two sentences to "are you okay ?" And "Why don't you speak !", Because the sender is "Ding Jian" and is in the blacklist, the information sent by the sender is not displayed and is directly placed in the garbage bin. As shown in:
As shown in, the recipient does not display the sent messages in the blacklist. At the same time, the received messages are directly stored in the recycle bin. As shown in, in the xmlmessage. xml file that stores the information content we define, set the storage location of this information to "garbage bin", as shown in:
Next, let's talk about the spam text message filtering function. A blocked keyword is stored in the key.txt text file. We can check whether the received information contains any blocking keyword. If yes, this information is blocked, the content is not displayed in the session record window, and the information is directly put into the garbage bin.
On the phone, Zhai weichao belongs to the white list. In the key.txt text file, we set some blocking keywords in advance, such as "dumb", "Fuck", "kill you", and "TMD", as shown in the text file:
In the following description, we will use "Wei Chao" and "Li Liqun" for simple conversations. Some information contains blocking keywords, and some information does not include blocking keywords, view the specific effect of the conversation and the specific location of information storage. The dialog content is as follows:
Li Liqun: Hello, Xiao!
Wei Chao: Well, hello, what have you done with your project?
Li Liqun: It's a bit difficult for me to operate. I can only continue to work hard.
Wei Chao: Yes, you only need to work hard!
Li Liqun: Yes, bye
The running result of the preceding dialog is shown in:
From the conversation and information storage diagram above, we can see that it is a bit difficult because "I am running" sent by "Li Liqun". I can only continue to work hard ." It contains the blocking keyword "fuck", so the information content is blocked in the "Wei Chao" session record dialog box, and the information is also marked in the garbage bin.
The relevant procedures are attached below:
1. Dialog. h file:
# Ifndef dialog_h # define dialog_h # include <qdialog> # include <ui_dialog.h> # include <qhash. h> # include <qpalette. h> # include <qtextcodec> # include <qtnetwork/qudpsocket. h> # include <qtnetwork/qhostaddress. h ># include <qlist> namespace UI {class dialog;} class dialog: Public qdialog, public ui_dialog {q_object public: explicit Dialog (qwidget * parent = 0 ); Void dealmessage (const qstring &); bool location (const qstring, const qstring );~ Dialog (); Public slots: // display the recipient's name and number void setreciever (const qstring &); // write the XML file void writexml (const qstring, const qstring ); // rename the session window name void renamewintitle (const qstring &); // defines the sending function void send (); // defines the receiving function void recieve (); Private: qtextcodec * codec; qpalette warning_color; qudpsocket * udpsocket; // defines the number qstring number of the sent information stored in the variable; // defines the content qstring content of the sent information stored in the variable; // use a hash table to store the phone book qhash <qstring, qstring> qhash_phone; UI: Dialog * UI; // define its own information qstring my_number;}; # endif // dialog_h
2. Dialog. cpp File
# Include "dialog. H "# include <ui_dialog.h> # include <qfile> # include <qstring> # include <qtextstream> # include <qxmlstreamreader> # include <qxmlstreamwriter> # include <qmessagebox. h> # include <qtime> # include <qiodevice> // constructor dialog: Dialog (qwidget * parent): qdialog (parent), UI (new UI: Dialog) {UI-> setupui (this); // compatible with Chinese characters codec = qtextcodec: codecforname ("gb2312"); qtextcodec: setcodecforlocale (codec); qtextcodec :: Setcodecforcstrings (codec); qtextcodec: setcodecfortr (codec); // set its own information my_number = "4567"; // set the font color of the reminder text warning_color.setcolor (qpalette: windowtext, qt: Red); UI-> label_warning-> setpalette (warning_color); // UI without sending reminder-> label_warning-> settext (TR ("")); // set the maximum number of ComboBox UIS to 5-> combobox_phone-> setmaxvisibleitems (5 ); // set the first entry of ComboBox to empty UI-> combobox_phone-> additem ("NONE", "char"); // Add an empty or empty key-value pair to qhash_phone Qhash_phone.insert ("", ""); // open the text file qfile phonefile ("phbooks.txt") at the specified position; // determine whether the file is successfully opened and open if (phonefile in read-only mode. open (qfile: readonly) {qstring phonebook; // defines two string variables to store the name and phone number qstring name, phone, and inblacklist in the phone book; // define a Text Stream object in and use phonefile to initialize qtextstream in (& phonefile); // check whether the text stream reaches the end of the text file while (! In. atend () {phonebook = in. readline (50); // if not, read the two rows and the variable "inblacklist = phonebook" exists. left (1); phonebook = phonebook. mid (2); name = phonebook. left (phonebook. indexof (""); phone = phonebook. mid (phonebook. indexof ("") + 1); // Save the name and phone number to a qhash variable qhash_phone.insert (name, phone ); // Add the contact UI in the phone book to ComboBox-> combobox_phone-> additem (name, "qstring");} // close the text file phonefile. close ();} else {// if the file fails to be opened The obox displays error UI-> combobox_phone-> additem ("error", "char");} udpsocket = new qudpsocket; udpsocket-> BIND (qhostaddress: localhost, 1573 ); // when the combobox_phone option is changed, lineedit_reciever also changes qobject: connect (ui-> combobox_phone, signal (currentindexchanged (qstring), this, slot (setreciever (qstring); // when the combobox_phone option is changed, the title of the dialog window changes accordingly. qobject: connect (ui-> combobox_phone, signal (currentindexchanged (qstring), this, Slot (renamewintitle (qstring); // when the combobox_phone option is changed, clear the session record qobject: connect (ui-> combobox_phone, signal (currentindexchanged (INT )), UI-> textedit_message, slot (clear (); // click the "close" button to close the window qobject: connect (ui-> pushbutton_close, signal (clicked ()), this, slot (close (); // when you click "send", qobject: connect (ui-> pushbutton_send, signal (clicked (), this, slot (send (); // receive packet data qobject: connect (udpsocket, signal (ready Read (), this, slot (recieve ();} // display the phone number corresponding to the name selected by ComboBox in lineedit_reciever void dialog :: setreciever (const qstring & name_select) {UI-> lineedit_reciever-> settext (qhash_phone [name_select]);} // determines the information storage location. False indicates the inbox. True indicates the bin bool dialog:: location (const qstring number, const qstring content) {// inbox bool result = false by default; // read the qfile phonenumb ("phbooks.txt") of the phone book text file "); if (phonenumb. open (qfile: readonly) {Qs Tring words; qtextstream in (& phonenumb); // check whether the text stream reaches the end of the text file while (! In. atend () {words = in. readline (50); If (words. contains (number) {qstring flag = words. left (1); If (flag = "1") {result = true; phonenumb. close (); return result ;}} phonenumb. close ();} else {UI-> textedit_message-> append ("phbooks.txt text file opening failed! ") ;}// Define the keyword qlist <qstring> keywordlist in key.txt of the chain table Storage; // read the blocked keyword from the key.txt file, determine whether the received information contains the keyword qfile keyword ("key.txt"); If (keyword. open (qfile: readonly) {qstring words; qtextstream in (& keyword); // check whether the text stream reaches the end of the text file while (! In. atend () {words = in. readline (50); keywordlist. append (words);} keyword. close ();} else {UI-> textedit_message-> append ("key.txt text file opening failed! ");} Int listlength = keywordlist. count (); For (INT I = 0; I <listlength; I ++) {If (content. contains (keywordlist. at (I) {result = true ;}return result ;}// write the XML file void dialog: writexml (const qstring number, const qstring content) {// obtain the information storage location bool loc = location (number, content); // define the Read File qfile message_xml ("xmlmessage. XML "); // define the Write File qfile message_xml_1 (" temp. XML "); // open the file if (! Message_xml.open (qiodevice: Text | qiodevice: readonly) {qmessagebox: About (this, "xmlmessage cannot be opened. XML file "," error "); message_xml.close (); return;} If (! Message_xml_1.open (qiodevice: writeonly | qiodevice: Text) {qmessagebox: About (this, "cannot open temp. XML file "," error "); message_xml_1.close (); Return ;}// define the read/write stream qxmlstreamreader xmlreader (& message_xml) of the XML file; qxmlstreamwriter xmlwriter (& message_xml_1 ); // whether it is the first element of the XML file, that is, whether it is the first SMS bool flag = true; // The Setting Mode xmlwriter. setautoformatting (true); xmlwriter. setcodec (codec); // whether it is at the end of the file while (! Xmlreader. atend () {If (xmlreader. isstartdocument () {xmlwriter. writestartdocument ();} xmlreader. readnext (); If (xmlreader. isstartelement () {// if it is the first information in the XML file, add the newly received information if (xmlreader. name (). tostring () = "SMS" & flag) {xmlwriter. writestartelement ("SMS"); xmlwriter. writetextelement ("Number", number); xmlwriter. writetextelement ("name", qhash_phone.key (number); xmlwriter. writetextelement ("content", con Tent); If (LOC) xmlwriter. writetextelement ("location", "garbage bin"); else xmlwriter. writetextelement ("location", "inbox"); xmlwriter. writeendelement (); flag = false;} // read the element attribute qxmlstreamattributes ATTR = xmlreader. attributes (); // write element name xmlwriter. writestartelement (xmlreader. name (). tostring (); // write the element attribute if (ATTR. size ()> 0) {xmlwriter. writeattributes (ATTR) ;}} if (xmlreader. ischaracters () {xmlwriter. writecharacter S (xmlreader. text (). tostring ();} If (xmlreader. isendelement () {xmlwriter. writeendelement () ;}} xmlwriter. writeenddocument (); // close the Read File message_xml.close (); // close the Write File message_xml_1.close (); // remove the original Read File message_xml.remove (); // rename the qfile :: rename ("temp. XML "," xmlmessage. XML ") ;}// rename the form name void dialog: renamewintitle (const qstring & Title) {qstring win_title =" "; if (title! = "") Win_title = "and" + title + "in-call"; else win_title = "in-session"; this-> setwindowtitle (win_title );} // The sending function implements void dialog: Send () {// defines the variable qstring message_send that stores the sent content; // defines the recipient qstring message_reciever; // obtain the sent content message_send = UI-> textedit_sender-> toplaintext (); // obtain the Recipient Information message_reciever = UI-> lineedit_reciever-> text (); // determine whether the sent content is null if (message_send = NULL) {// when the content to be sent is empty, a reminder UI is displayed-> label_warning-> settext (TR ("the content to be sent cannot be blank! "); Return;} else {// clear the reminder text UI-> label_warning-> settext (TR ("")); // if the recipient is empty, if (message_reciever = NULL) {UI-> label_warning-> settext (TR ("select the recipient! "); Return;} else {qstring current_time; qdatetime datetime = qdatetime: currentdatetime (); current_time =" "+ datetime. time (). tostring ("H: mm: SS"); // Add the sender UI to the session record-> textedit_message-> append (qhash_phone.key (my_number) + current_time ); UI-> textedit_message-> append ("" + message_send) ;}// read the sender's number qstring phonenumber = my_number; // set the sent content qstring temp = "<SMS> <number>" + phonenumber + "</number> <content>" + Messa Ge_send + "</content> </SMS>"; // send data udpsocket-> writedatable (qprintable (temp), 256, qhostaddress: localhost, 1600 ); // clear the sending content UI-> textedit_sender-> clear ();} // The receiving function implements void dialog: recieve () {char buffer [256]; udpsocket-> readdatagram (buffer, 256, 0, 0); qstring temp (buffer); // process the received string and separate the number and content dealmessage (temp); qstring current_time; // obtain the current time qdatetime datetime = qdatetime: currentdatetime (); current_time = "" + Datetime. time (). tostring ("H: mm: SS"); // Add the sender UI to the session record-> textedit_message-> append (qhash_phone.key (number) + current_time ); // determine the information location bool blackflag = location (number, content); // if it is a blacklist or the information contains a blocking keyword, the information content if (! Blackflag) {UI-> textedit_message-> append ("" + content);} else {UI-> textedit_message-> append ("");} // write the received message writexml (number, content) to the XML file;} // message data processing function void dialog: dealmessage (const qstring & numb_cont) {// locate the start position of the sending number: int numb_start = numb_cont.indexof ("<number>") + 8; qstring temp = numb_cont.mid (numb_start ); // locate the end position of the sending number int numb_end = temp. indexof ("</number>"); // obtain the sending Number = temp. left (numb_end );/ /Locate the starting position of the sent content, int cont_start = numb_cont.indexof ("<content>") + 9; temp = numb_cont.mid (cont_start ); // locate the end position of the sent content int cont_end = temp. indexof ("</content>"); // get the sent content: content = temp. left (cont_end);} // destructor dialog ::~ Dialog () {Delete UI ;}
3. Main. cpp File
#include <QtGui/QApplication>#include "dialog.h"#include <QTextStream>int main(int argc, char *argv[]){ QApplication a(argc, argv); Dialog w; w.show(); return a.exec();}
Note: The test project program is basically the same as the above program. You only need to modify the corresponding port number.