Objective
Learning C + + for a year, and now to do the lesson set, feel the console interface is too ugly, so use QT to do a graphical program out.
Not long to learn QT, just understand a general, this development is basically gnawing 2 days of official help documents, and then use a variety of QT provided by the wheel implementation. Some of the places are really imperfect, but there seems to be no fatal bug.
Under the code quality, be careful to imitate.
Qt really is a very good C + + extension library, learn C + + think there is no useful to learn QT to develop a few graphical software to practice practiced hand.
Project Introduction
Implement a simple phone this program, can be added, find, modify, delete, save to file and other basic functions
Development environment
IDE: qtcreator(I personally think that is very useful, this is I am now writing C + + in Linux, the main IDE, because it is the official IDE so very high degree of fit)
system :Windows(originally wanted to be developed under Linux, but considering the last program to be shown on the Windows platform, do not want to compile two times, so chose the Windows platform, QT is cross-platform so this doesn't have to be too much to consider)
Programming Ideas
In fact, the first reaction must be to use a database to achieve this function. But since it's a C + + class, and given that the amount of data should not be too large, it is a lazy practice to use text documents to store and read data (Orz)
The main interface, looked at, originally wanted to use a table to show, after a day of tuning the discovery is very cumbersome to set up, so instead of using the list control. (actually I found a similar tutorial on the internet I will talk nonsense?) Query, delete, add all can use the list control to bring the method, as to save, directly is the use of text document output way.
function Development Program Body
First to write the main window interface of the program, build a new QT project, base class selected as Qmainwindow, and then edit the interface file as follows (no landscaping, light spray):
The naming of individual parts.
Next, start writing the various features
Add to
Adding features requires a pop-up window, enter your name and phone number, and then insert it into the main program's listwidget. Therefore, you need to write a dialog window class, add a designer interface class to the project, base class select Qdialog, named Editdialog (this window can also be used in the modification function), template selection dialog with Buttons under ( is a dialog box with a set of buttons below), then set the interface to this:
The two input boxes are plaintextedit controls, and the following group of buttons have been associated with the accept () and reject () Slots of the dialog box respectively.
Then add the following common functions in Editdialog:
Const; // const; // void setName (const QString &); // void setnumber (const QString &); //
The above two functions are used to get the contents of the edit box, and the following two functions are used to set the initial content in the edit box (which is available in the following modification function)
The implementation is like this:
QString editdialog::name () const { return ui->nameedit->toplaintext (). Trimmed ();} QString editdialog::number () const { return ui->numedit->toplaintext (). trimmed ();} void editdialog::setname (const QString & name) {UI ->nameedit-> void editdialog::setnumber (const QString & num) {UI ->numedit->
Then let's modify the slot of the corresponding button in the main window (modify slot to quickly switch to the implementation of the slot function if you right-click on the control). Note that instantiating Editdialog is to include the header file, and then the newly added class is the same
voidmainwindow::on_addbutton_clicked () {Editdialog Editdialog ( This); if(editdialog.exec () = =1) {QString line= Editdialog.name () +"---"+Editdialog.number (); UI->listwidget->AddItem (line); This->statusbar ()->showmessage ("1 Item Added", -);//to set the status bar's prompt information }}
We instantiate a Editdialog object in this slot, set its parent object as the main window, use EXEC () to implement modal window effects, get user input through the name and number methods within the Editdialog object, and assemble them together. Use the AddItem () method to insert the Listwidget.
Modify
Modify function and add function basically is the same, we go directly to the corresponding button in the slot to achieve it
voidmainwindow::on_editbutton_clicked () {if(!ui->listwidget->CurrentItem ())return; Qstringlist Parts= Ui->listwidget->currentitem ()->text (). Split ("---"); Editdialog Editdialog ( This); Editdialog.setname (parts[0]); Editdialog.setnumber (parts[1]); if(editdialog.exec () = =1) {UI->listwidget->currentitem ()->settext (Editdialog.name () +"---"+Editdialog.number ()); This->statusbar ()->showmessage ("1 Item Updated", -); }}
It is not difficult to use the split () method of the Qstringlist class and qstring, it is easy to see this example, and the official documents are written in detail.
Delete
The delete function actually takes advantage of the CurrentItem () function and the Delete method provided by Qlistwidget to return the currently selected row, but in order to prevent misoperation, a modal confirmation dialog is required, and a designer interface class is added to the project. Still choose to have two Buttons dialog box, the interface of the dialog only need to put a label write "OK delete?" To
This dialog class I named Confirmdialog, the slot function for the main window corresponding to the button is as follows:
voidmainwindow::on_delbutton_clicked () {if(!ui->listwidget->CurrentItem ())return; Confirmdialog* Confirmdialog =NewConfirmdialog ( This); if(confirmdialog->exec () = =1) { DeleteUi->listwidget->CurrentItem (); This->statusbar ()->showmessage ("1 Item deleted", -); }}
Find
This is the most difficult to write a function, or the first query window, this time I chose a dialog box without a button class, named Querydialog, the interface is as follows:
The following two buttons: "Query" is set to Querybutton, the slot will later be associated with the slot in the main window, "Exit" is directly associated with the Reject () slot of the dialog box.
In addition, in order to be able to associate the Querybutton with the main window slot, he must be set to the public, so I put my head in the file, the UI has been changed to:
Public : *ui;
This is not really a good way to destroy the object-oriented encapsulation, but I am lazy ah. And so it did.
The idea of finding a function is that the user enters the keyword in the Find dialog and then transmits the keyword to the slot in the main window via the signal sent by the query button, which calls Listwidget's FindItem () method and then turns the result (a base class QListWidgetItem *
of Qlist) sent to the query window to display.
Add a function in Querydialog to return the contents of the edit box
QString Querydialog::gettarget () { return ui->queryedit->toplaintext ();}
Add a private Querydialog object Qdlg in the MainWindow class (this is done to enable communication between the two windows, and the Slot association)
The slot of the main window "Find" button is written as a modal window showing Qdlg:
void mainwindow::on_querybutton_clicked () { qdlg.exec ();}
The slot function in the main window, written under public slots:
void Mainwindow::sendqueryresult () { = qdlg.gettarget (); <qlistwidgetitem * > reslist = ui->listwidget->finditems (target,qt::matchcontains); // Qdlg. Showqueryresult (reslist); // qdlg.exec ();}
Next, in the constructor of the MainWindow class, associate this slot with the signal of its member object Qdlg the query button:
Connect (qdlg.ui->querybutton,signal (clicked ()),This, SLOT (Sendqueryresult ()));
Then there is the function that is used to receive the data and display it in Querydialog ShowQueryResult
:
voidQuerydialog::showqueryresult (ConstQlist <qlistwidgetitem * > &reslist) { if(reslist.size () = =0) {UI->reslabel->settext ("No results!"); return; } Else{QString Resnumber= Qstring::number (Reslist.size ());//Note the string handling hereUI->reslabel->settext (resnumber+"Item (s) founded:"); for(intI=0; I<reslist.size (); i++) {UI->listwidget->additem (reslist[i]->text ()); } }}
Here, I would like to be able to directly use the AddItem function overload of the Qlistwidgetitem * parameter to directly read the list of records, but did not find the contents of the display (reason I have not been unclear, looking for the expert pointing), So I used the Qlistwidgetitem text () method to get the string added in.
This basically implements the query function
Save
In fact, I initially wanted to make the results of dynamic preservation (the operation of the listwidget directly affect the save file), but the query is a little cumbersome, so changed to every click on the Save button to re-write the document (or lazy 23333).
To implement the Save, you need an external txt document (I'm named Data.txt here)
In the main window class, add a private Qfile object file, which is used to read and write Data.txt
Next, when you open the program, it automatically loads before you save the content. In fact, it is very simple to add a read txt directly to the constructor and write Listwidget to
File.setfilename ("data.txt"); // If(File.Open (qiodevice::readonly| Qiodevice::text)//{ qtextstream readin (&file); while (! readin.atend ()) { = Readin.readline (); // ui->listwidget->addItem (line); } File.close ();}
Next implement the Save button slot
voidmainwindow::on_savebutton_clicked () {//function: Save all the records in the current program to Data.txt if(File.Open (qiodevice::writeonly)) {Qtextstream out(&file); for(intI=0; I<ui->listwidget->count (); i++) { out<<ui->listwidget->item (i)->text () <<Endl; } file.close (); This->statusbar ()->showmessage ("File saved", -); } Else { This->statusbar ()->showmessage ("Save failed!", -); return; }}
Looking at the use of Qfile and Qtextstream in the Help documentation, the above program should not be difficult to understand.
Now we have finished the program! Let's release the release soon ~
Source
Please check out my github:
Github.com/marklux/qt-phonebook
Remember to develop: QT Simple phone this program