A small record of qt Learning (2), qt Learning Record
Well... This time I received a ticket
A graphical interface is required. You can just consolidate and learn QT. I took it without hesitation.
The following issues are recorded:
1. QWidget and QDialog
The slot functions in QDialog include accept () and reject (), which can be directly used.
In addition, the QDialog window is displayed in exec () mode, which is blocked.
In QWidget, only show () needs to write blocking by itself.
2. Window communication 2.1 Main Window-> subwindow
A lot of methods are introduced.
I used to directly pass values to the variables in the subwindow:
For example:
Subwindows include:
class changemsg : public QDialog{ Q_OBJECTpublic: changemsg(QWidget *parent = 0); ~changemsg(); person *pn = nullptr; void setMsg();private: Ui::changemsg ui;public slots: void rev_change();};
The main window is:
void addressbookqt5vesion::rev_btn_change() { int i = getObjFromtableSelected(); if (i != -1) { changemsg* chmsh = new changemsg(this); chmsh->pn = &vt_pn[getObjFromtableSelected()]; chmsh->setMsg(); chmsh->exec(); }}
Pass in the parameter before exec.
2.2 subwindow-> Main Window
The sub-window can be delivered to the main window using a signal slot.
But here, because I still use directly calling the sub-window variables...
class addmsg : public QDialog { Q_OBJECTpublic: addmsg(QDialog *parent = 0); ~addmsg(); person *pn = nullptr; person getPerson() { return *pn; }private: Ui::addmsg ui;public slots: void rev_addp();};
You can write the following in the slot function of the main window:
void addressbookqt5vesion::rev_btn_add() { addmsg add; if (add.exec() == QDialog::Accepted) { if (add.pn != nullptr) { vt_pn.push_back(*add.pn); } else { QMessageBox::warning(this, tr("Add"), tr("Please enter a name!.")); } }
Directly modify and delete through the pointer pn
3. Use QDataStream
One of the constructors is:
QDataStream (QIODevice * d)
QIODevice describes an input/output medium that can read data from and write data to it. The QFile class is an example of an IO Device.
First, the code for opening the file is as follows:
Useless parts deleted
Void addressbookqt5vesion: rev_openfile () {QString path = qfiledision: getOpenFileName (this, tr ("open file "),". ", tr (" Text Files (*. dat) "); if (! Path. isEmpty () {QFile file (path); if (! File. open (QIODevice: ReadOnly | QIODevice: Text) {QMessageBox: warning (this, tr ("Read File"), tr ("Cannot open file: \ n % 1 "). arg (path); return;} // stream Read file QDataStream ins (& file); person pn; while (! Ins. atEnd () {ins> pn. pid> pn. name> pn. ismale> pn. type> pn. date> pn. phone> pn. email; char * ch = new char (MAXPERSONSIZE + 1); ins> ch; person: recoverboolarr (ch); delete (ch); vt_pn.push_back (pn );} ishavecreatenewfile = true; havecreatefilepath = path; // allows you to save the ui. actionSave-> setEnabled (true); ui. actionSaveAs-> setEnabled (true); file. close ();} else {QMessageBox: warning (this, tr ("Path"), tr ("You did not select any file. "));}}
Key code:
ins >> pn.pid >> pn.name >> pn.ismale >> pn.type >> pn.date >> pn.phone >> pn.email;char *ch = new char(MAXPERSONSIZE + 1);ins >> ch;
From these three lines, we can know:
1. QDataStream can be used> and <stream operations
2. The data type of the written file can be retained.
3. Multiple data types are allowed, including QString, int, bool, and char *
(Since there is no bool... Therefore, I first convert bool [] to a char * array (marked with '0' and '1') and write it. After reading the data, convert it back)
4. Because there is data-type storage, the storage file cannot be opened in txt.
As can be seen from the writing of QDataStream, we will not repeat it here.
4. Add an icon to the toolbar
You can find the following on the QT designer interface:
Click Create
And then the important part .. Drag an entry in the Action editor to the window action bar...
(I have studied this step for a long time before I find out... It turns out that you can drag !!!!!)
Then add several slots to the main window.
In the signal slot Editor, link the action to the slot.
The click signal is triggred ()
:
5. Set the icon for the exe file in VS + qt
1. Create a file named *. rc in the project folder, such as myapp. rc.
2. Write IDI_ICON1 icon discardable "myapp. ico" to the file. myapp. ico is the name of the ICON file.
3. Copy the myapp. ico file to the project folder.
4. In Solution Explorer, right-click the project and add myapp. rc and myapp. ico.
5. regenerate the solution.
6. VS + QT package and release
1. Add the bin/directory of qt to the system path variable. You can set C: \ Qt \ Qt5.4.0 \ 5.4 \ msvc2013_64 \ bin in the System Properties and add it to the system path environment variable.
2. Create a folder in any directory and copy the compiled qt exe executable program.
Enter the folder in command line mode and run the command windeployqt xxxx.exe
The tool then analyzes program dependencies and automatically adds necessary dll files to the directory.
3. Use NSIS and other packaging software for packaging