本文介绍了将重载指针传递给成员函数信号作为QObject :: connect的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Qt中,如果信号未过载,则可以将其传递给connect方法。

In Qt if the signal is not overloaded it can be passed to the connect method like this.

QObject::connect(comboBox, &QComboBox::currentTextChanged, [&]()-> void {});

但是,如果信号过载,则可以分两步完成。

But if the signal is overloaded then it can be done in two steps.

在Qt的QComboBox类中,突出显示的方法已重载

In Qt's QComboBox class the highlighted method is overloaded

void QComboBox::highlighted(int index)
void QComboBox::highlighted(const QString & text)

使用QObject :: connect时,我们可以声明指向成员函数变量的指针,然后使用它需要2个步骤。

When using the QObject::connect we can declare the pointer to member function variable and then use it which requires 2 steps.

       void (QComboBox::*fptr) (int) = &QComboBox::highlighted;
            QObject::connect(comboBox, fptr, [&]()-> void {
                                 insertWidgetToMapAndSend(listView);
                             });

是否可以在不声明 ftptr 的情况下通过重载方法?

Is it possible to pass the overloaded method without the declaration of ftptr ?

推荐答案

您可以内联强制转换:

QObject::connect(comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::highlighted), [&]()-> void {
                             insertWidgetToMapAndSend(listView);
                         });

但是从Qt 5.7开始,使用:

But starting with Qt 5.7, use qOverload:

QObject::connect(comboBox, qOverload<int>(&QComboBox::highlighted), [&]()-> void {
                             insertWidgetToMapAndSend(listView);
                         });

QOverload

QObject::connect(comboBox, QOverload<int>::of(&QComboBox::highlighted), [&]()-> void {
                             insertWidgetToMapAndSend(listView);
                         });

这篇关于将重载指针传递给成员函数信号作为QObject :: connect的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:20