"Golang-gui Development" struct tags system (i)

Source: Internet
Author: User

We have introduced the QT signal and slots, now it is time to talk about its struct tags system. QT has a variety of struct tags, and we'll get to know them all.

What is a struct tags?

struct tagAlso called the structure tag, as the name implies, it is used to mark the structure of the field. For example, we are familiar with the JSON used tags:

type User struct {    UserId   int    `json:"user_id" bson:"user_id"`    UserName string `json:"user_name" bson:"user_name"`}

Tags are wrapped by an anti-quote, name : precedes, and value is enclosed : by double quotation marks.
With these tags, our code can easily use reflect to get tags of the name and name corresponding value:

u := &User{UserId: 1, UserName: "tony"}t := reflect.TypeOf(u)field := t.Elem().Field(0)fmt.Println(field.Tag.Get("json"))    // "user_id"fmt.Println(field.Tag.Get("bson"))    // "user_id"

Our QT is dependent on this feature to achieve the QT MOC system, the use of different tags in addition to the implementation of signal and slots can also realize the various functions of the MOC, even QT own some of the extensions.

"--" and "<-"

In signal we have introduced the auto, it has a lot of limitations, the project author also said that auto should try to use alone, should not be used auto(...) in the form. And for more convenient connection signal and slots, we need to use -> and <- .

Let's take a look at an example, this time with an excerpt from the official example:

type Chart struct {    core.QObject    *charts.QChart    _ func() `constructor:"init"`    _ func() `slot:"handleTimeout,<-(this.m_timer.timeout)"`}

For the slot handletimeout, we used <- it, and it is equivalent to the following sentence:

this.m_timer.ConnectTimeout(this.handleTimeout)

This means that the this.m_timer timeout signal and the this.handleTimeout function connect will be called when the this.m_timer timeout signal is triggered.
You can also connect without specifying a signal name by default with a function with the same name as the signal name specified by the signal tag:

_ func() `slot:"handleTimeout,<-(this.m_timer)"`

And

_ func() `slot:"handleTimeout,<-(this.m_timer.handleTimeout)"`

Equivalent.

Let -> 's take a look at the use:

import "controller"type dialogTemplate struct {    core.QObject    _ func() `constructor:"init"`    _ func(cident string) `signal:"show,<-(controller.Controller)"`    _ func(bool)          `signal:"blur,->(controller.Controller)"`}

As can be seen, we use the signal blur -> , the meaning of this expression <- is the opposite, it is the signal tag declaration of the signal or slot tag declaration slot with -> the following function to connect, when you trigger this signal or call this slot, The function in parentheses is also called, which is equivalent to:

this.ConnectBlur(controller.Controller.blur)

or (as mentioned above, you can omit the function name)

this.ConnectBlur(controller.Controller)

Some usage rules for "--" and "<-"

We have mentioned in the last paragraph that you can omit the function name of the connection and the connected object in these two tags, and here are a few rules:

    1. The parentheses specify the global object, including the visible object in the imported package, as in the previous example controller.Controller .
    2. This refers to an instance of the current object (which can be understood as a C + + this , python self placeholder, or Golang receiver ).
    3. The bracketed content can also be this.StructField , that is, the field in the object
    4. For signal/slot that want to connect inheritance QObject and its derived classes, or other classes, it is only possible to use this.BaseClass.method the form (similar to auto), which the author says will be improved later.

"," and "<-" and "Auto"

All three need to be used with the signal/slot tag, they will automatically connect the signal and slot, but they are also many different.

    • First of all our daily use should try to use singal:"signalName,auto" instead auto(...) , -> or <- , if just to write less Connect* , then should not use the latter three, because unless you have a lot of Connect* need to write, otherwise easily affect code reading, Especially if the connection object is a member function of the current class instance.
    • ->and <- for interacting with different objects, Connect* declaring logical relationships in struct tags is easier to maintain than scattered calls.
    • ->and <- used to connect existing signals and slots, if you want to reuse the signal and slots of base classes or member variables, you need -> or <- replace them auto .
    • When interacting with QML, you should also use -> and <- connect the signals from QML.

Objectively these three can greatly simplify our implementation and use of signal/slot, so according to different scenarios, we need to choose the appropriate tags to simplify our development.

In the next article we will look constructor at the constructors in this tag,qt.
If you have any questions or suggestions for this article, please ask in the comments.
Have a good time!

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.