QObject :: connect与connect方法之间的区别 [英] Difference between QObject::connect vs connect methods

查看:214
本文介绍了QObject :: connect与connect方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt的新手.大多数时候,Qt开发人员需要使用信号和插槽进行对象通信.到目前为止,我已经看到了两种连接信号和插槽的方法.

I am a newbie with Qt. Most of the times Qt developers need to use signals and slots for object communication. I have seen two ways of connecting signals and slots so far.

1)QObject::connect(scrollBar, SIGNAL(valueChanged(int)),label,  SLOT(setNum(int)));

2)connect(scrollBar, SIGNAL(valueChanged(int)),label,  SLOT(setNum(int)));

两者之间的确切区别是什么?为什么在第一种方法中必须在QObject前面加上前缀?

What is the exact difference between both of them? Why do we have to prefix QObject in the first method?

推荐答案

在上述两种情况下,您都调用静态版本,其签名如下:

You call the static version in both aforementioned cases, the signature of which is as follows:

QMetaObject :: Connection QObject :: connect(const QObject *发送者,const char *信号,const QObject *接收者,const char *方法,Qt :: ConnectionType类型= Qt :: AutoConnection)[静态]

QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection) [static]

当您不在QObject子类内部进行连接时,将需要分别使用范围变体,因为您将没有合适的对象来调用它.在这里,您可以看到一些代表差异的代码.

When you are not connecting inside a QObject subclass, you will need to use the scoped variant, respectively, because you will not have an object in place to call it on. Here you can see some code representing the difference.

class MyClass : public QObject
{
    Q_OBJECT
    public:
        MyClass(QObject *parent) : QObject(parent) {
            connect(this, SIGNAL(mySignal()), SLOT(mySlot()));
        }

    public signals:
        void mySignal();

    public slots:
        void mySlot();
};

范围

int main(int argc, char **argv)
{
    QCoreApplication a(argc, argv);
    MyClass myObject;
    QObject::connect(&myObject, SIGNAL(mySignal()), &myObject, SLOT(mySlot()));
    return a.exec();
}

请注意,如果您尝试在接收器对象中进行此连接,则为方便起见甚至可以跳过第三个参数(即减少键入),因为非静态const版本将根据文档自动处理此问题:

Please note that if you are trying to do this connection within the receiver object, you could even skip the third argument for convenience (i.e. less typing) because the non-static const version will take care of this automatically as per documentation:

QMetaObject :: Connection QObject :: connect(const QObject *发送者,const char *信号,const char *方法,Qt :: ConnectionType类型= Qt :: AutoConnection)const

此函数将重载connect().

This function overloads connect().

将信号从发送者对象连接到该对象的方法.

Connects signal from the sender object to this object's method.

相当于connect(发送者,信号,this,方法,类型).

Equivalent to connect(sender, signal, this, method, type).

您建立的每个连接都会发出一个信号,因此重复的连接会发出两个信号.您可以使用disconnect()断开连接.

Every connection you make emits a signal, so duplicate connections emit two signals. You can break a connection using disconnect().

这篇关于QObject :: connect与connect方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆