首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

自定义槽 No such slot解决方法

2012-06-08 
自定义槽 No such slot诸位好。最近在做qt,自定义槽的时候,出现如下错误。先把代码贴出了,请各位老师,过目。

自定义槽 No such slot
诸位好。最近在做qt,自定义槽的时候,出现如下错误。先把代码贴出了,请各位老师,过目。

头文件:mainwindow.h 

C/C++ code
    #ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtGui/QLineEdit>namespace Ui {    class MainWindow;}class MainWindow : public QMainWindow{     Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();private:    Ui::MainWindow *ui;    QVector<QLineEdit*> lines;    QStringList   paths;public slots:    void upload2Ftp();    void inputFinish(QString,int);  };#endif // MAINWINDOW_H


源文件:mainwindow.cpp
C/C++ code
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>#include <QObject>MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow) {    ui->setupUi(this);    lines.append(ui->lineEdit_0);    lines.append(ui->lineEdit_1);    lines.append(ui->lineEdit_2);    lines.append(ui->lineEdit_3);    lines.append(ui->lineEdit_4);    ui->progressBar->setRange(0,100);    ui->progressBar->setValue(0);    for(int i=0;i<lines.size();i++){       QLineEdit* edit = lines.at(i);       connect(edit,SIGNAL(editingFinished()),this->centralWidget(),SLOT(this->inputFinish(edit->text(),i)));    }      connect(ui->pushButton_0,SIGNAL(clicked()),this->centralWidget(),SLOT(this->upload2Ftp()));}MainWindow::~MainWindow() {    delete ui;}void MainWindow::inputFinish(QString path,int index) {    paths.replace(index,path);}void MainWindow::upload2Ftp(){    for(int i=0;i<lines.size();i++){        qDebug()<<paths.at(i);    }}


main.cpp文件

C/C++ code
#include <QtGui/QApplication>#include "mainwindow.h"#include <QtGui/QPushButton>#include <QtGui/QLineEdit>int main(int argc, char *argv[]) {    QApplication a(argc, argv);    MainWindow w;    w.show();    return a.exec();}



运行能出现界面,但是提示如下信息:
D:\ifnp\ftpGui\ftpGui-build-desktop\debug\ftpGui.exe 启动中...
Object::connect: No such slot QWidget::this->inputFinish(edit->text(),i) in ..\ftpGui\mainwindow.cpp:23
Object::connect: (sender name: 'lineEdit_0')
Object::connect: (receiver name: 'centralWidget')
Object::connect: No such slot QWidget::this->inputFinish(edit->text(),i) in ..\ftpGui\mainwindow.cpp:23
Object::connect: (sender name: 'lineEdit_1')
Object::connect: (receiver name: 'centralWidget')
Object::connect: No such slot QWidget::this->inputFinish(edit->text(),i) in ..\ftpGui\mainwindow.cpp:23
Object::connect: (sender name: 'lineEdit_2')
Object::connect: (receiver name: 'centralWidget')
Object::connect: No such slot QWidget::this->inputFinish(edit->text(),i) in ..\ftpGui\mainwindow.cpp:23
Object::connect: (sender name: 'lineEdit_3')
Object::connect: (receiver name: 'centralWidget')
Object::connect: No such slot QWidget::this->inputFinish(edit->text(),i) in ..\ftpGui\mainwindow.cpp:23
Object::connect: (sender name: 'lineEdit_4')


请诸位老师,错在哪里?

[解决办法]
必然出错!!!
你没有理解好this
connect(edit,SIGNAL(editingFinished()),this,SLOT(this->inputFinish(edit->text(),i)));

你的槽函数是定义在MainWindow 这个类中,所以接受者应该是this

而且我没有看错的话,你写的是centralWidget(),是个函数啊这个函数我在MainWindow 里面也没有找到,这么写有点莫名其妙,接受者不可以是函数
------解决方案--------------------


老湿……直译过来就是 没有相关槽函数的定义, this->centralWidget()这个返回的是QMainWindow的中间Widget,没看到LZ setCentralWidget ( QWidget * widget )?LZ应该把各个信号的接受者连起来 而不是QMainWindow的CentralWidget;

C/C++ code
   for(int i=0;i<lines.size();i++){       QLineEdit* edit = lines.at(i);       connect(edit,SIGNAL(editingFinished()), [color=#FF0000]edit[/color] ,SLOT(this->inputFinish(edit->text(),i)));    }
[解决办法]
void QLineEdit::textChanged ( const QString & text ) [signal]
问题在于,你写的信号,QLineEdit里没有,只有上面的那个,区别是有一个参数。

热点排行