A simple example of QT signal Slot Mechanism

Source: Internet
Author: User
Tags qt designer

Return to QT and create a new QT application in vs. There are several directories in the project:

1. The form files directory contains the XML-based layout file created using QT designer. Double-click it to automatically enter QT designer.

2. generated files directory, which contains temporary files used to process QT signals and slots.

3. The resource files directory contains XML-based resource files, which can be used in the form.

4. header files and source files are the same as those of vs by default.

After understanding the directory structure, first try to write a hello World, first put apart main. remove all files other than CPP (using QT designer improves production efficiency, but increases the QT entry threshold )! Open main. cpp and only keep the following code:

# Include <qtgui/qapplication>
Int main (INT argc, char * argv [])

{
Qapplication A (argc, argv );
Return a.exe C ();
}

Compiled. The Operation has no response, because nothing has been added to it.

In the code, the main function is the entry of the C language, and the qapplication applied later is used to manage the control flow and main settings. This is the core and must be retained.

Button is the most basic control in the GUI. First, let's see how to add a button. The button control must first contain the header file:

# Include <qtgui/qpushbutton>

Then insert the following code in the middle of qapplication A (argc, argv); and return a.exe C:

Qpushbutton button ("hello ");

Button. setgeometry (100,100,300,300 );

Button. Show ();

The first line of the code is to apply for a button and set the caption title of the button to hello. The second line indicates that the button appears at the position of the screen coordinate (100,100), and the width and height are (300,300 ), the last line shows this button. You can try to remove it to see the effect (for more information about qpushbutton in the official assistant, please check it yourself ).

After compilation, a box is displayed on the screen. There is a button in the box that can be clicked, but there is no response, because no slot has been added for this button ).

In MFC, the control is generally handled through the event mechanism, while in QT, the signal and slot mechanisms are used. In fact, you can also regard it as an event mechanism.

Simply understanding the signal is actually the input, while the slot is the output. For example, in a single click, this click is a signal, and the feedback after the click is a slot.

Each control has some default signal and slot, which can be viewed in the official assistant.

The static function connect is used to bind signal and slot. The function prototype is:

Bool connect (const qobject * sender, const char * signal, const qobject * receiver, const char * method, QT: connectiontype type = QT: autoconnection)

The sender is the sender, the receiver is the receiver, the signal is the signal, and the method is the slot. The type provides several binding methods. You can view the assistant in detail.

Let's take a look at an example. In the above Code, add the button to close the application. It is very simple. You only need
Button. setgeometry (100,100,300,300 );

Add later

Qobject: connect (& button, signal (clicked (), & A, slot (quit ()));

Compile and run the program. Click and close the form.

This is an example of using the default slot. Sometimes you need to click the button to execute the custom effect, so you need to use the custom slot.

The following is an example of using a custom slot. After you click the button, the text in the text box changes.

Add a qlabel control first. Add the header file first:

 
# Include <qtgui/qlabel>

Then add before connect

Qlabel label ("world ");

Label. setgeometry (50, 50, 300,300 );
 

Compile the code first. The result label does not appear in the form! Of course, it will not appear in the form, because we only use the show () function for the button and try to add label. show (), two forms are displayed, one with buttons and the other with a label. So how can we put them together?

Through the test above, it is found that a window will be generated when show is called once. Is it enough to call show only once? Change the code in the function:

Qapplication A (argc, argv );

Qwidget window;

Qpushbutton button ("hello ");

Button. setgeometry (100,100,300,300 );

Qlabel label ("world ");

Label. setgeometry (50, 50, 300,300 );

Qhboxlayout layout;

Layout. addwidget (& button );

Layout. addwidget (& label );

Qobject: connect (& button, signal (clicked (), & window, slot (close ()));

Window. setlayout (& layout );

Window. Show ();

Return a.exe C ();

The following is a list of header files:

# Include <qtgui/qpushbutton>

# Include <qtgui/qapplication>

# Include <qtgui/qlabel>

# Include <qtgui/qhboxlayout>

# Include <qtgui/qwidget>
 

At the beginning, I applied for a qwidget. The qwidget class is the base class of all user interface objects in QT. It has no practical significance, here you can regard it as a form container, and then add another

Qhboxlayout layout; qhboxlayout is a control that can be used to layout child widgets. With this control, buttons and labels can be placed side by side, and then layout of the form is set to the specified layout, then call show ().

After debugging and running, both controls finally appeared.

Return to the previous topic and customize the slot. All user-defined slots in QT must be compiled into MOC before they can be used. Let's rest assured that this process is automatically completed by qt2. Of course, you can also compile and use moc.exe in the qtbinlog.

You can see that I have secretly switched the button clicking signal to the close slot of the form. Why do we need to do this, because we need to put the custom slot function definition in the header file.

Step 1: encapsulate the window first. I create a mainwidget class that inherits from the qwidget class. The header file of the class is as follows:

View plaincopy to clipboardprint?
# Ifndef _ main_widget_h _
 
# DEFINE _ main_widget_h _
 
# Include <qtgui/qlabel>
 
# Include <qtgui/qhboxlayout>
 
# Include <qtgui/qwidget>
 
# Include <qtgui/qpushbutton>

Class mainwidget: Public qwidget

{

Public:

Mainwidget ();

~ Mainwidget ();



Protected:



PRIVATE:

Qlabel * m_plabel;

Qpushbutton * m_pbutton;

Qhboxlayout * m_playout;

};
 
# Endif

CPP is as follows:
 
# Include "mainwidget. H"

Mainwidget: mainwidget ()

{

M_plabel = new qlabel ("world ");

M_plabel-> setgeometry (50, 50, 300,300 );

M_pbutton = new qpushbutton ("hello ");

M_pbutton-> setgeometry (100,100,300,300 );

M_playout = new qhboxlayout ();

M_playout-> addwidget (m_pbutton );

M_playout-> addwidget (m_plabel );

Connect (m_pbutton, signal (clicked (), this, slot (close ()));

Setlayout (m_playout );

}

Mainwidget ::~ Mainwidget ()

{

}
Change main. cpp:

# Include <qtgui/qapplication>
 
# Include "mainwidget. H"

Int main (INT argc, char * argv [])

{

Qapplication A (argc, argv );

Mainwidget window;

Window. Show ();

Return a.exe C ();

}

Compile and run the program. The result is the same as the previous one.

Apply for a custom slot, first add the macro before the header file public:

Q_object;

Only when q_object is added can you use the signal and slot mechanisms in QT. This is very important. Otherwise, the "slot not found" error will be reported during compilation.

Then add the following before protected:
Private slots:

Void settext ();
 

Slot is also divided into private, public, and protected, meaning the same as C ++.

Add the corresponding execution to CPP:

Void mainwidget: settext ()

{

M_plabel-> settext ("test ");

}
 

Change connect:
Connect (m_pbutton, signal (clicked (), this, slot (settext ()));

Compile and run the program. Then, click the button to change the text. That's simple.

The signal can also be customized, but the signal customization is relatively less useful. The definition method is similar to the slot definition and must be defined in the header file. For example: after you click the button, the text changes and a new signal is triggered, which changes the text back.

Add the following to the header file:
Signals:

Void textchanged ();
 

Add another slot to feedback the signal. Add after private slots:

Void recovertext ();

Add execution in CPP:

Void mainwidget: recovertext ()

{

M_plabel-> settext ("hello ");

}
 

Note that the signal does not need to be executed.

Then modify the settext () function to add the code that triggers the new signal:

Emit textchanged ();

Finally, add the new Connect:

 
Connect (this, signal (textchanged (), this, slot (recovertext ()));

Compile and run the program. The result is the same as what we want.

Note: Both the signal and the slot can have parameters.

The basic knowledge about QT is introduced here. For details about how to use the control, refer to the assistant.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/pizi0475/archive/2010/05/19/5609474.aspx

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tulun/archive/2010/05/31/5635782.aspx

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.