代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QPushButton *> Sensor_list;
Sensor_list.append(ui->pushButton_3);
Sensor_list.append(ui->pushButton_4);
Sensor_list.append(ui->pushButton_5);
Sensor_list.append(ui->pushButton_8);
QString text;
for(int i=0;i<4;i++)
{ text=Sensor_list[i]->text();
connect(Sensor_list[i],SIGNAL(clicked()),this,SLOT(pushButton_clicked(text)));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::pushButton_clicked(QString text)
{
qDebug()<<text;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void pushButton_clicked(QString text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
运行是出现:
Object::connect: No such slot MainWindow::pushButton_clicked(text)
Object::connect: (sender name: 'pushButton_3')
Object::connect: (receiver name: 'MainWindow')
Object::connect: No such slot MainWindow::pushButton_clicked(text)
Object::connect: (sender name: 'pushButton_4')
Object::connect: (receiver name: 'MainWindow')
Object::connect: No such slot MainWindow::pushButton_clicked(text)
Object::connect: (sender name: 'pushButton_5')
Object::connect: (receiver name: 'MainWindow')
Object::connect: No such slot MainWindow::pushButton_clicked(text)
Object::connect: (sender name: 'pushButton_8')
Object::connect: (receiver name: 'MainWindow')
这是为何???我明明有MainWindow::pushButton_clicked(text)?
signal SLOT
------解决方案--------------------
connect(Sensor_list[i],SIGNAL(clicked()),this,SLOT(pushButton_clicked(text)))这句怎么能带参数呢,后面的SLOT应该是参数的类型啊,connect(Sensor_list[i],SIGNAL(clicked()),this,SLOT(pushButton_clicked(QString)));
并且signal和slot参数不匹配啊,这也是个问题啊
------解决方案--------------------