[QT learning] how to analyze a QT class

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Let's take the qlineedit class as an example to see how to learn and analyze a QT class.

1. Public types:

This is a public Enumeration type that can be used in this class. In other words, we can use this public type as follows:

// some code..QLineEdit::EchoMode mode;mode = myLineEdit->echoMode(); ... mode = QLineEdit::NoEcho;myLineEdit->setEchoMode( mode );// or directly myLineEdit->setEchoMode( QLineEdit::NoEcho );

Public types are often used in properties.

2. Properties

Properties can be get and set, as shown in the preceding example.

3. Public Functions

This section provides a large number of methods for this class instance, textedit-> append ("path to file:" +Lineedit-> text ());

4. Public Slots

This part describes some actions that can connect signals. These actions constitute the result of the signal connected to them, that is, one slot action occurs when a signal is sent back. We can also customize the signal slot, for example:

Myqtapp. h

Public slots:

Void myslot (const qstring &);

Myqtapp. cpp

void myQtApp::mySlot(const QString& passedStr){    QMessageBox::information(this, "Some window label here", "String: " + passedStr);}
Then we connect to this slot in this way: connect (lineedit, signal (textedited (const qstring &), this, slot (myslot (const qstring &)));

5. Signals

This section describes the parts of the class that can send signals. These signals are connected with specific slots, resulting in corresponding slot actions. That is to say, the signal is equivalent to the reason, while slots is equivalent to the result. For example

 connect( pushButton_clear, SIGNAL( clicked() ), this, SLOT( clear() ) );

Here we connect the clicked signal of pushbutton_clear to the clear () slot of this. When this key is pressed, this will execute this clear action. We can also customize the signal:

Myqtapp. h

public:    myQtApp(QWidget *parent = 0); signals:    void customSignal( const QString& );..

Myqtapp. cpp-To useEmit keywords

Void myqtapp: clear ()

{

Emit customsignal (textedit-> toplaintext (); // The signal is sent here with a string

Textedit-> clear ();

}

We can connect our custom signals as follows: connect (this, signal (customsignal (const qstring &), this, slot (myslot (const qstring &)));

6. Protected Functions

This is the protected method in the traditional sense of C ++.

7. Static public members

This is a global class method that can be called directly without creating an instance.

8. Const qstring &

This definition is often seen in QT documents. In fact, this can be skipped.Const &To improve the program performance, you do not need to copy a parameter in the method. Const indicates that the input parameter cannot be modified.

9. allocate memory on the stack or stack

For example, if we use the qprocess class, we write the following functions:

void myQtApp::function() {    QProcess proc; // we create this proc variable on stack    proc.start("regedit.exe"); // start program regedit.exe}

We thought we would call this EXE, but we didn't actually. The terminal debugging information is shown as follows:

Qprocess: destroyed while process is still running.

This is because the variable is on the program stack. When the variable is out of this range, the memory of this part on the stack will be released. In our example, Proc is destroyed before execution of this program. The solution is to create this variable on the stack: When the programmer uses new for dynamic memory allocation, the memory is allocated in the heap (virtual memory, including the on-board RAM and the remaining disk space allocated by the hard disk.

void myQtApp::function() {    QProcess *proc; // pointer definition    proc = new QProcess( this ); // memory allocation from heap, created with parent    proc->start("regedit.exe"); // start program}

Note that the reason for using this is that the child process is also destroyed after the parent process is destroyed.

Another method is to put proc into the class myqtapp to become a class member.

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

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.