当前位置:网站首页>Binding method of multiple sub control signal slots under QT

Binding method of multiple sub control signal slots under QT

2022-06-26 15:34:00 A fat yard makes a strong man

In some software interface development , You need to use the same batch of controls , For example, there are dozens of settings interfaces lineedit Input box , Or many checkbox Control , As shown in the figure below (qt Setting interface of ):
 Insert picture description here
Which set , As long as a control makes changes, it needs to refresh and save parameters in real time , What's the plan ?

1、 If the control has rules , For example, the code uses for loop new The object of , We can do that new Add your slot function to the next loop .

2、 The second situation is separation new The object of , Does every object new All the time coonnect once ? If it is qt designed In the design interface, you can drag in the settings interface ?

In this case, we can use qt Of findChildren function , As shown in the following code :

  QList<QCheckBox *> checkboxList = this->findChildren<QCheckBox *>();
    for (int i = 0; i < checkboxList.size(); i++) 
    {
    
        connect(checkboxList[i], &QCheckBox::clicked, this, [=]()
        {
    
        // The slot function you need to execute 
        }
    }

Use the function to find all such control objects under the pop-up window , And put it in a pointer linked list , In this way, unified management can be done , For example, the signal slot connection we want .

原网站

版权声明
本文为[A fat yard makes a strong man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261411143459.html