简体   繁体   English

QT4没有这样的插槽错误

[英]QT4 no such slot error

I know there are many many questions that are just the same, but none of them helps me: 我知道有很多相同的问题,但是没有一个对我有帮助:

class Form1 : public QMainWindow {
    Q_OBJECT
public:
    Form1();
    virtual ~Form1();
public slots:
    void langChange(const char* lang_label);
private:
    Ui::Form1 widget;
    void setLangStrings();
};

From1 constructor: From1构造函数:

Form1::Form1() {
    widget.setupUi(this);
    connect(widget.btnL0, SIGNAL(clicked(bool)), this, SLOT(langChange("en")));
    connect(widget.btnL1, SIGNAL(clicked(bool)), this, SLOT(langChange("fr")));
    setLangStrings();
}

And I also have this langChange function implemented: 我还实现了这个langChange函数:

void Form1::langChange(const char* lang_label)
{
    GL_LANG = lang_label;
    setLangStrings();
}

I get this stupid error when the connect function is called: 调用connect函数时出现此愚蠢错误:

No such slot Form1::langChange("sl") in Form1.cpp:15

I'm using NetBeans with QDesigner for the UI. 我正在将NetBeans与QDesigner用于UI。 I must say this QT4 is very difficult to learn. 我必须说这个QT4很难学习。

You simply can't connect SIGNAL with bool as argument to SLOT with const char* as argument. 您根本无法将带布尔值的SIGNAL和以const char *作为参数的SLOT连接起来。 To do this kind of stuff you have to use QSignalMapper . 为此,您必须使用QSignalMapper You have an example how to use it inside documentation. 您在文档中有一个如何使用它的示例。 In your case, it's very simple, so you should handle it easly. 就您而言,这非常简单,因此您应该轻松进行处理。

The SLOT function must have the same signature than the SIGNAL function SLOT函数必须具有与SIGNAL函数相同的签名

Edit: From the official Qt documentation (http://qt-project.org/doc/qt-4.8/signalsandslots.html): 编辑:从官方的Qt文档(http://qt-project.org/doc/qt-4.8/signalsandslots.html):

The signature of a signal must match the signature of the receiving slot. 信号的签名必须与接收插槽的签名匹配。 (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) (实际上,插槽可能比接收到的信号具有更短的签名,因为它可以忽略额外的参数。)

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

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