简体   繁体   English

Qt插槽和信号语法

[英]Qt Slots and Signals Syntax

I have searched numerous help discussions and read multiple tutorials, but I still don't understand the correct syntax for Qt signals and slots, using Qt Core 5.0 I created a very simple program with two objects to try and understand this Syntax. 我搜索了许多帮助讨论并阅读了多个教程,但是我仍然不了解Qt信号和插槽的正确语法,使用Qt Core 5.0我创建了一个非常简单的程序,其中包含两个对象以尝试理解此语法。 (Shown below). (如下所示)。 Every time I compile this code, I get the following error: 每次编译此代码时,都会出现以下错误:

expected primary-expression before 'int' 预期在“ int”之前的主要表达式

Please help me with the following answers: 请为我提供以下答案:

  1. What is the problem with the code I wrote? 我编写的代码有什么问题?

  2. Does the Qt connect function expect pointers for the object references (&mySig) instead of the objects directly? Qt connect函数是否期望对象引用(&mySig)的指针而不是对象的指针?

  3. When I use slots and signals that include parameters, in the connection function, do I need to supply variables for those parameters, or merely state the data type as shown in my code below? 当我在连接功能中使用包含参数的插槽和信号时,是否需要为这些参数提供变量,或者仅声明数据类型,如下面的代码所示?

  4. Eventually, I want to use slots and signals to pass data between objects in a program I'm writing. 最终,我想使用插槽和信号在我正在编写的程序中的对象之间传递数据。 Do slots and signals allow me to pass other objects, which are derived from QObject? 插槽和信号是否允许我传递从QObject派生的其他对象? Or do I need to do something extra? 还是我需要做些额外的事情?

  5. I see many references to a format of the connect statement which uses 我看到许多对使用的connect语句格式的引用

    QObject::contect(&mySig, SIGNAL(sig_1(int)), &mySlot, SLOT(slot1(int))); QObject :: contect(&mySig,SIGNAL(sig_1(int)),&mySlot,SLOT(slot1(int)));

    Is this format still valid under the Qt 5.0 Core? 这种格式在Qt 5.0 Core下仍然有效吗?

Many thanks for all the help! 非常感谢您的所有帮助! Code of simple program follows below. 简单程序的代码如下。

#include <QCoreApplication>
#include <QObject>
#include <iostream>

using namespace std;

//================================================================================
class testSig : public QObject
{
    Q_OBJECT
public:
    explicit testSig(QObject *parent = 0) :
        QObject(parent)
    {
    }

    void getNum()
    {
        int t;
        cout << endl << endl << "Please Enter Number:  ";
        cin >> t;
        emit sig_1(t);
    }

signals:
    void sig_1(int i );
};

//================================================================================
class testSlot : public QObject
{
    Q_OBJECT
public:
    explicit testSlot(QObject *parent = 0) :
        QObject(parent)
    {
    }

public slots:
    void slot1(int i)
    {
        cout << "New Value is:  " << i << endl;
    }
};

//=================================================================================
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    testSig mySig;
    testSlot mySlot;

    QObject::connect(&mySig, testSig::sig_1(int), &mySlot, testSlot::slot1(int));

    for( ; ; )
    {
        mySig.getNum();
    }
    return a.exec();
}

1. What is the problem with the code I wrote? 1.我编写的代码有什么问题?

This line: 这行:

QObject::connect(&mySig, testSig::sig_1(int), &mySlot, testSlot::slot1(int));

This is illegal C++. 这是非法的C ++。 If you want to use the new signal/slot syntax, you need to take the pointers of the signals and slots functions: 如果要使用新的信号/插槽语法,则需要使用信号和插槽功能的指针:

QObject::connect(&mySig, &testSig::sig_1, &mySlot, &testSlot::slot1);

2. Does the Qt connect function expect pointers for the object references (&mySig) instead of the objects directly? 2. Qt connect函数是否期望对象引用(&mySig)的指针而不是对象的指针?

Yes, see the docs . 是的, 请参阅文档

3. When I use slots and signals that include parameters, in the connection function, do I need to supply variables for those parameters, or merely state the data type as shown in my code below? 3.当我在连接功能中使用包含参数的插槽和信号时,是否需要为这些参数提供变量,或者仅声明数据类型,如下面的代码所示?

With the "old" syntax, yes. 使用“旧”语法,是的。 With the new syntax, no (see above: no arguments specified). 使用新语法时,请使用no(请参见上文:未指定任何参数)。 But you can find yourself in trouble if your signal or slot have overloads . 但是,如果您的信号或插槽过载,您可能会遇到麻烦

4. Eventually, I want to use slots and signals to pass data between objects in a program I'm writing. 4.最终,我想使用插槽和信号在我正在编写的程序中的对象之间传递数据。 Do slots and signals allow me to pass other objects, which are derived from QObject? 插槽和信号是否允许我传递从QObject派生的其他对象? Or do I need to do something extra? 还是我需要做些额外的事情?

Of course, you can pass any data type. 当然,您可以传递任何数据类型。 Restrictions apply if you use queued connections, but let's leave that as an advanced topic. 如果您使用排队连接,则存在限制,但我们将其保留为高级主题。

5. I see many references to a format of the connect statement which uses 5.我看到了很多关于connect语句格式的引用,该语句使用

 QObject::contect(&mySig, SIGNAL(sig_1(int)), &mySlot, SLOT(slot1(int))); 

Is this format still valid under the Qt 5.0 Core? 这种格式在Qt 5.0 Core下仍然有效吗?

Yes, absolutely. 是的,一点没错。 We do want old code to compile! 我们确实希望编译旧代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM