简体   繁体   English

qt 5中的信号槽问题

[英]signal slot issue in qt 5

I have one c++ class and have one signal in it and want to connect that signal with slot another C++ class. 我有一个c ++类,其中有一个信号,并希望将该信号与插槽连接另一个C ++类。 Here is my code 这是我的代码

class Data : public QObject
{
Q_OBJECT

public:
static Data *instance(void);
 signals:
 void sendUserID(const QString& userId);

 private:
static Data *s_instance;
};

here is my Slot in another class 这是我在另一堂课中的老虎机

void DataDetails::seTUserID(const QString user_id)
{
 QAndroidJniObject user_id = QAndroidJniObject::fromString(user_id);
 QAndroidJniObject::callStaticMethod<void>("com/user/data/userActivity",
                                          "setUserID",
                                           "(Ljava/lang/String;)V",
                                           user_id.object<jstring>());
}

The idea is to access the value of user_id from Data class to DataDetails class 我们的想法是从Data类访问user_id的值到DataDetails类

The connection is trying is 连接正在尝试

 QObject::connect(&s_instance, SIGNAL(sendUserID(uid), this, SIGNAL(setUserID(uid))

any other id to get uid to other class is also fine .. 任何其他id来获取其他类的uid也没关系..

Generally speaking, when you encounter an issue with QObject::connect ... 一般来说,当你遇到QObject::connect的问题时......

Make sure you have your declarations in order: 确保按顺序声明您的声明:

  • Both classes need the Q_OBJECT macro in their declaration. 这两个类在声明中都需要Q_OBJECT宏。
  • Make sure your slot is actually declared as a slot (ie is part of a public slot: section). 确保您的插槽实际上被声明为插槽(即public slot:一部分public slot:部分)。

Because connecting signals and slots just uses character strings evaluated at run-time, it's possible to write absolute nonsense and have it compile. 因为连接信号和插槽只使用在运行时计算的字符串,所以可以编写绝对的废话并进行编译。 In my experience, errors caused by typos are pretty common. 根据我的经验,拼写错误导致的错误很常见。

  • Always test in an environment where you can see your application's console output. 始终在可以看到应用程序控制台输出的环境中进行测试。 Failed connect calls will usually trigger an error message printed to stderr. 连接调用失败通常会触发打印到stderr的错误消息。
  • Double-check your method names and signatures. 仔细检查您的方法名称和签名。 Code will still compile even if you've made a typo! 即使您输错了,代码仍会编译!
  • For debugging, use assertions (eg bool c = connect(...); Q_ASSERT(c); ) to catch missed connections early. 对于调试,使用断言(例如bool c = connect(...); Q_ASSERT(c); )来尽早捕获错过的连接。
  • Alternatively, you can use the QMetaMethod-based version of QObject::connect , introduced in Qt 4.8, to avoid some of these issues. 或者,您可以使用Qt 4.8中引入的基于QMetaMethod的QObject::connect版本来避免其中的一些问题。

In your particular case: 在您的特定情况下:

  1. You've got a typo in the function declaration: it's called seTUserID but you're using setUserID in the connect call. 你在函数声明中有一个拼写错误:它叫做seTUserID但你在connect调用中使用了setUserID
  2. You're using variable names, not function signatures, in your signal and slot names. 您在信号和插槽名称中使用变量名称而不是函数签名。 Qt expects to see QObject::connect(&s_instance, SIGNAL(sendUserID(const QString), this, SLOT(setUserID(const QString)) Qt期望看到QObject::connect(&s_instance, SIGNAL(sendUserID(const QString), this, SLOT(setUserID(const QString))
  3. You've got a signal connected to another signal , which is valid but doesn't do what you want (it's usually used to chain stuff like this: SomeChildWidget's signal -> MyClass1's signal -> MyClass2's slot). 你有一个信号连接到另一个信号 ,这是有效的,但没有做你想要的(它通常用于链接这样的东西:SomeChildWidget的信号 - > MyClass1的信号 - > MyClass2的插槽)。

Check that seTUserID definition is marked as slot. 检查seTUserID定义是否标记为slot。 You are probably calling connect in some Data method (because you are using private member directly). 您可能在某些Data方法中调用connect(因为您直接使用私有成员)。 Are you trying to use a ref to a pointer to s_instance O_o? 您是否尝试使用ref指向s_instance O_o的指针? Write sendUserID(const Qstring) (the signature of a signal) rather then sendUserID(uid). 写sendUserID(const Qstring)(信号的签名)而不是sendUserID(uid)。 The same situation with setUserID. 与setUserID相同的情况。 You are trying to connect to this pointer and want to send info to another class!? 您正在尝试连接到此指针,并希望将信息发送到另一个类!? Use new style of Qt connecting signals and slots (pointers to members), with was introduced in Qt 5. Check for setUserID has written right. 使用新的Qt连接信号和插槽(指向成员的指针),在Qt 5中引入。检查setUserID是否已写入权限。

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

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