[英]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: 确保按顺序声明您的声明:
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.
根据我的经验,拼写错误导致的错误很常见。
bool c = connect(...); Q_ASSERT(c);
) to catch missed connections early. bool c = connect(...); Q_ASSERT(c);
)来尽早捕获错过的连接。 QObject::connect
, introduced in Qt 4.8, to avoid some of these issues. QObject::connect
版本来避免其中的一些问题。 In your particular case: 在您的特定情况下:
seTUserID
but you're using setUserID
in the connect call. seTUserID
但你在connect调用中使用了setUserID
。 QObject::connect(&s_instance, SIGNAL(sendUserID(const QString), this, SLOT(setUserID(const QString))
QObject::connect(&s_instance, SIGNAL(sendUserID(const QString), this, SLOT(setUserID(const QString))
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.