"Golang-gui development" QT Signal and Slots (ii)

Source: Internet
Author: User

In the previous article we introduced the use of signal in detail.

Today we will describe the use of slots. In Qt, slots and signal are very similar, this time we will implement a spinbox that can display 16 binary numbers, which inherits from Qspinbox and writes validate , valurFromText and textFromValue these three slots so that the original 10 binary integer value can be displayed as 16 binary. We'll show you the specific usage of the slots.

The statement of the slot

Let's take a look at the example:

type HexSpinbox struct {    widgets.QSpinBox        _ func() `constructor:"init"`        _ func(int) string                        `slot:"textFromValue,auto"`    _ func(string) int                        `slot:"valueFromText,auto"`    _ func(string, int) gui.QValidator__State `slot:"validate,auto"`        validator *gui.QRegExpValidator}

We see the same as signal, need to use struct tags to specify the slot and its name, the name will be strings.Title processed. In Qt, the slot is essentially a normal function, so it is allowed to have a return value.

We also see that slots can be specified auto , and yes for SLOT,QT will also be generated like signal Connect[slot name] , Disconnect[slot name] and [slot name] these three functions.

One thing to note here is that generating a slot function in Qt, like a signal, is an empty shell function that needs to be Connect[slot name] connected to a specific function so that the slot can be [slot name] used through the function. autoand the signal will automatically connect.

So far, our slots are no different from normal member functions, and signal can be connected to any function in Qt, so why do we have to specifically declare them as slots?

That's because we're rewriting the slots, and the readers familiar with QT may have found that the three slots in our example are qspinbox slots, and we've rewritten them. Because the MOC system is used, it is not valid to rewrite directly with the same name function in the struct, so we need to use slot tags. In our example, the class's derived class can also override the base class's slots by using the slot tags. This makes it more convenient and flexible to customize the components.

Use of Slots

As mentioned earlier, to use slots, you have to connect to it first, which is one of the important differences with QT:

 //Init initialization object, automatically called by Newhexspinbox func (H *hexspinbox) init () {h.setrange (0, 255)//We validate the input with regular, guaranteed only 16 input System digital RegExp: = Core. NEWQREGEXP2 ("[0-9a-fa-f]{1,8}", Core. Qt__casesensitive, Core. QREGEXP__REGEXP) H.validator = GUI. NewQRegExpValidator2 (RegExp, h)}//slots implementation, these slots are automatically called by the Spinbox//validate to validate the input, the content cannot be passed is not shown func (H *hexspinbox) Validate (input string, pos int) GUI. qvalidator__state {return h.validator.validate (input, POS)}//Textfromvalue converts the input or increment/decrease content into a string and displays the func (H *hexsp Inbox) Textfromvalue (value int) string {return StrConv. Formatint (Int64 (value), +)}//Valuefromtext converts the displayed or entered legal content to an integer intfunc (h *hexspinbox) valuefromtext (text string) int {V Alue, _: = StrConv. Atoi (text) return int (value)}//Newhexspinbox is a constructor generated by the MOC, and we will explain box: = Newhexspinbox (nil)//connect slots to make them available, If you specify auto, you do not need to connect manually, which is shown here as an example box. Connectvalidate (box.validate) box. Connecttextfromvalue (box.textfromvalue) box. Connectvaluefromtext (box.valuefromtext)  

When we're connected, we can call box.Validate , box.ValueFromText and box.TextFromValue .

// 就像signal,调用他们时连接的函数也会被调用// 一点区别在于slot拥有返回值,所以你也可以使用变量来接收slot返回的结果box.ValueFromText("ff") // -> 255box.TextFromValue("26") // -> "1a"

Of course, we rewrite these slots not to be called in the code, but to change the spinbox display behavior.

Here is the main function, and after the processing of the slots is complete, use our hexspinbox like a normal widget:

func main() {    widgets.NewQApplication(len(os.Args), os.Args)        hexSpin := NewHexSpinbox(nil)    hexSpin.Show()        widgets.QApplication_Exec()}

Display effect:

What, isn't it simple? QT is such a simple and flexible library, and we will conduct further research later. If there are comments and suggestions to be welcomed in the comments, it is also welcome to ask questions positively.

Have a good time!

Related Article

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.