自定义槽 No such slot
诸位好。最近在做qt,自定义槽的时候,出现如下错误。先把代码贴出了,请各位老师,过目。
头文件:mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui/QLineEdit>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
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
#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);
}
}
#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();
}
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;
for(int i=0;i<lines.size();i++)
{
QLineEdit* edit = lines.at(i);
connect(edit,SIGNAL(editingFinished()), edit ,SLOT(this->inputFinish(edit->text(),i)));
}