QT5 in the signal and groove with the function of C + + 11

Source: Internet
Author: User

Recently the Company's interface development Library from WX to QT, with the signal and slot, originally we use WX, in order to use the signal, we are using the Boost Signal2 library, to QT with a signal slot, this is not necessary

However, the signals and slots of QT are not placeholder, that is, placeholders, and the signal and slot declarations can only be strictly the same as in the previous period, such as

void slot (int  val); void int val);

But in SIGNALS2, we can use placeholder, a timely function declaration is not the same, can also be used to connect

void slot (int  val); void int val); void slot1 (int val,double x,double  y); void Slot2 (double x,int val);


So we can write a lot less code to the data binding, the group of other people in order to achieve this effect, so either declare a class, and then declare a heap of data that may be used in the class, then send, some send multiple signals, very egg pain

I wanted to change the QT signal to boost the signal, then the eldest brother disagreed, the reason is to increase the cost of learning, although I always feel that there is no cost

However, after writing the code to find QT inside Connect has multiple statements, I looked under, a statement is made by a similar boost to the use of the template, I tried to use the next, found that the use of anonymous functions is really available

Connect (Sender,&signal,[&] (void) {lambdafunctionbody;});

However, when using bind, the dead and alive can not connect, once connect will compile the error

Auto Fnbind = Boost::bind (&class:: Function, this ); Connect (sender,&slot,fnbind);

It took an afternoon to find out that QT Connect is using a template, he does not automatically convert Fnbind to function, so at compile time, naturally because does not conform to the function syntax and compile error, and Auto is

The Fnbind is deduced into the boost::bind type, so it can be written manually

Boost::function<void (void) > Fnbind = Boost::bind (&class:: Function, this ), connect ( sender,&slot,fnbind);

Compile through, don't write the code that hurts so much.

QT5 in the signal and groove with the function of C + + 11

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.