"Golang-gui development" QT Signal and slots (i)

Source: Internet
Author: User

For a long time, I decided to start with the signal and slot (signal slots).

Signal and slots everyone must be familiar with the first example (selected from the document):

1 classCounter: PublicQobject2 {3 Q_object4 5  Public:6Counter () {m_value =0; }7 8     intValue ()Const{returnm_value;}9 Ten  PublicSlots: One     voidSetValue (intvalue); A  - Signals: -     voidValueChanged (intnewvalue); the  - Private: -     intm_value; -};

Classes that use signal and slots must contain Q_object macros, and declaring the slots requires public/private/protected slots:,signal requires signals:.

These are actually macros that instruct the MOC to generate the corresponding code so that the QT program can send a signal and have the slot connect to the signal.

But Golang doesn't have a macro, so what do we do in QT?

Signal----Signal

1. Definition of signal----signal

To customize signals, we need to use a simple feature of Golang--struct tags.

Tags are widely used in the world of Golang, from the standard library Encoding/json to the widely used ORM (Xorm,gorm), tags of the figure everywhere. Tags are widely used because it can be reflect taken,

Rely on the powerful reflect package, you can use tags to achieve a variety of functions, including QT's MOC extension.

Let's take a look at a custom component with two custom signal:

1 " Github.com/therecipe/qt/core " 2 3 struct {4    core. Qobject5     6    _ func () ' Signal: "datachanged" '7     _ Func (int) ' signal: ' valuechanged ' '8 }

First See line Fourth,

Core. Qobject

All classes that need to customize slots and signal must be core. Derived types of qobject, if not directly inherited from Qobject, then the directly inherited type must inherit directly or indirectly from Qobject.

Also, be careful not to use *core. The Qobject form, which causes Qtmoc to ignore this class and ultimately not handle the MOC extension raises the issue .

_ func () ' Signal: "DataChanged" ' _ Func (int) ' signal: "valuechanged" '

We defined two signal, the first without any parameters, and the second with an int type parameter.

Qtmoc will put the contents of tags with strings. The title does the processing, that is to say, DataChanged will become datachanged, this is the name of the signal we define.

Qtmoc then generates three member methods of the custom control class based on the name and the type of the member that the tags are in: connect[signal name],disconnect[signal name],[signal name],

In this case: Connectdatachanged,disconnectdatachanged and datachanged.

2. Connection of signal----signal

To connect with signal, you need to use the Connect[signal name] function mentioned earlier.

QTMOC generates the Connect function based on the type of signal, where the connectdatachanged prototype is the Func connectdatachanged (f func ()).

When you need to connect to this signal, call it and pass the signal handler as a parameter

Func sample () {    fmt. Println ("DATA has been changed. " }widget://  Here is the creation of our custom component, the following article we will focus on //  QT5 with signal can be any function, in QT is the same, So we use an external function to process the signal, and in practice it is recommended to use the class member method or slot for processing widgets. Connectdatachanged (sample)

Here we connect the sample to the signal datachanged, and each time the signal is triggered it will print "Data has been changed." This sentence message.

If you want to cancel a connection to a signal, you need to use the Disconnect[signal name] function, which does not take arguments, which means that you cancel the connection to the function signal the last time you used connect[signal name].

widget. Disconnectdatachanged ()//  We canceled the connection of the sample function to DataChanged

3. Triggering of signal----signal

To trigger a signal in C + +, you only need the following code:

Emit datachanged () emit valuechanged (value)

Emit Grammar that has not been seen in C + + and Golang ... Yes, this is also the MOC extension of Qt.

Remember when we said that Qtmoc would generate three member methods based on signal tags, connectdatachanged,disconnectdatachanged,datachanged

The third function is the one we use to trigger the signal.

The signal trigger function is used instead of emit, which is itself a function generated according to the type of signal tags before, so the type of mywidget.datachanged is func f (), and the type of the ValueChanged function is the func f (value int).

Note that, like QT,signal can not have a return value .

The following is an example of a trigger signal:

// Trigger datachanged Signal widget. DataChanged () Value:5//  trigger valuechanged signal and pass the parameter widget. ValueChanged (1) widget. ValueChanged () widget. ValueChanged (value)

Once the signal is triggered, the previously connected function is called.

4. Automatic connection of signal----signal

If the custom signal is more, then each call Connect[signal name] not only trouble inefficient, but also bring maintenance difficulties, so QT provides the function of automatic connection.

Look at the code first:

Import (    "github,com/therecipe/qt/widgets"struct  {    widgets. Qlabel    _ func () ' Signal:'datachanged,auto'    _ func ( string) ' Signal:'valuechanged,auto (this. Qlabel.settext)"'}

We saw an auto in the back of Signal's name.

This auto is to tell QTMOC this signal need to connect a and signal tags in the name of the same member method, where the name of the member function must be the same as in tags, not through the strings. Title processed signal name.

We then define and implement the member functions for this and datachanged connection:

Func (A *Auto) datachanged () {    fmt. Println ("Data has been changed. " )}

This way you no longer have to show the call connectdatachanged,datachanged will automatically connect to the member function datachanged.

We also see that there is auto (this. Qlabel.settext), which is a member method that automatically joins a base class when the custom type inherits from other Qobject and its derived classes.

This refers to the current object;

Qlabel or other type names represent the base classes of inheritance;

SetText is a member function of the base class that will be associated with the datachanged signal. It is also possible to write settext here, because the function name in () is strings. Title processing.

Whenever we trigger a signal:

0 ) widget. DataChanged () widget. ValueChanged ("Signal & Slots")

The corresponding member function is called, and the code above will react as follows:

// triggered because of a datachanged signal . widget.datachanged () // The widget is triggered because of a valuechanged signal . Qlabel.settext ("Signal & Slots")//  equivalent to widgets. SetText ("Signal & Slots")

Some people will ask, that can use ' signal: ' Datachanged,auto (this. Myfunc) "' Do you specify the function that you want to connect with the signal?

The answer is no . At present, Auto has only the above two usages, but the author and the member functions that implement the automatic connection member variable and the custom connection function join the development plan, I believe that these functions will be used soon.

The above is the specific use of QT signal, the next we will introduce the detailed use of the slot.

If you have any questions, please ask in the comments.

Reference:

Http://doc.qt.io/qt-5/signalsandslots.html

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.