2016-06-09 35 views
-1

我可以运行该程序,但按钮无法访问发送功能。我得到这个提示:QObject :: connect:没有这样的插槽(Qt,C++)

的QObject ::连接:没有这样的插槽中的Mail ::发送(emailInput,pwdInput)

有人知道什么是我的错?

mail.h:

#ifndef MAIL_H 
#define MAIL_H 

#include <QWidget> 

namespace Ui { 
class Mail; 
} 

class Mail : public QWidget 
{ 
    Q_OBJECT 

public: 
    explicit Mail(QWidget *parent = 0); 
    ~Mail(); 

public slots: 
    void send(std::string email, std::string pwd); 

private: 
    Ui::Mail *ui; 
}; 

#endif // MAIL_H 

mail.cpp:

Mail::Mail(QWidget *parent) : 
    QWidget(parent) 
{ 

    QLineEdit *edt1 = new QLineEdit(this); 
    grid->addWidget(edt1, 0, 1, 1, 1); 
    std::string emailInput = edt1->text().toStdString(); 
    ... 

    QObject::connect(acc, SIGNAL(clicked()),this, SLOT(send(emailInput, pwdInput))); 
} 


void Mail::send(std::string email, std::string pwd){ 
    ... 
} 
+2

您的语法错误。你想'SLOT(发送(std :: string,std :: string))' –

+0

http://stackoverflow.com/a/26422155/1421332 – Silicomancer

回答

0

这取决于你想要做什么:

如果emailInputpwdInput来自窗口小部件,你必须写一个中间的插槽,将获得值和呼叫发送。

如果它们是局部变量,最简单的可能是使用lambda。

+0

刚刚编辑帖子。我想将文本保存在QLineEdit中,然后使用变量调用send。 – Erwin11313

+0

然后,你必须写第一个插槽,不接受任何参数,并连接到你的按钮的单击信号,并调用发送实际值(或在lambda中执行,但它可以凌乱得相当快^^) –

+0

ty我检查拉姆达,它现在工作。 – Erwin11313

0

应该

QObject::connect(acc, SIGNAL(clicked()),this, SLOT(send(std::string, std::string))); 

SIGNALSLOT想到方法的作为参数(S)的签名。

此外,你可以连接一个信号到一个较不稳定的插槽,反之亦然。在这里,QObject不会简单地知道什么应该替换槽的参数。您可以使用connect接受任意Functor过载(匿名封闭,最有可能的),作为插槽:

QObject::connect(acc, SIGNAL(clicked()), [=](){ send(std::string(), std::string()); }); 

第三,是你使用QString而不是std::string,你不会有那么重的副本开销时传递价值。

1

事实上,你有2次失误代码:

  1. 的SLOT宏接受的参数的类型作为参数不是自己的名字,那么代码应该是:SLOT(send(std::string, std::string))
  2. 您试图连接一个信号,该信号的参数比SLOT的参数少,这是不可能的。

为了避免这些问题,你可以使用新的信号/槽语法(如果你使用的是QT5):

QObject::connect(acc, &QLineEdit::clicked, this, &Mail::onClicked); 

我也邀请您使用QString类,而不是性病: :在使用Qt时的字符串,它更容易。

相关问题