Qt使用connect函数时向slot传递参数

设想这样一种场景:共有5个button,button1~button5,点击button1时输出“button 1 clicked”,点击button2时输出“button 2 clicked”,该如何实现呢?
最粗暴的实现是写5个slot,然后将button与slot分别connect,但这明显会增加许多重复代码,不是一种好的实现。
一种比较好的方式是使用QSignalMapper,它可以接收无参数的signal,然后重新发射出有参数的signal,这些参数的类型可以是int、QString、QWidget *或QObject *:

QSignalMapper *signalMapper = new QSignalMapper(this);

connect(button1, SIGNAL(triggered()), signalMapper, SLOT(map()));
connect(button2, SIGNAL(triggered()), signalMapper, SLOT(map()));
connect(button3, SIGNAL(triggered()), signalMapper, SLOT(map()));
connect(button4, SIGNAL(triggered()), signalMapper, SLOT(map()));
connect(button5, SIGNAL(triggered()), signalMapper, SLOT(map()));

signalMapper->setMapping(button1, 1);
signalMapper->setMapping(button2, 2);
signalMapper->setMapping(button3, 3);
signalMapper->setMapping(button4, 4);
signalMapper->setMapping(button5, 5);

connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(printMsg(int)));

然后在printMsg(int)中根据不同的输入参数输出信息即可。
可参考https://stackoverflow.com/questions/5153157/passing-an-argument-to-a-slot


转发请注明来源http://blog.csdn.net/imred

程序逻辑很简单,就是让一个独立线程发送一个信号,UI线程的槽响应,但是运行报错居然是找不到信号 No such signal ,但是编译可以通过,moc文件也有这个信号。大侠们指点一下子,谢谢 #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); connect (&thread,SIGNAL(sendStr(QString)),this,SLOT(MsgStr(QString)),Qt::DirectConnection); // } Dialog::~Dialog() { delete ui; } // 启动线程按钮 void Dialog::on_startButton_clicked() { thread.start(); ui->startButton->setEnabled(false); ui->stopButton->setEnabled(true); } // 终止线程按钮 void Dialog::on_stopButton_clicked() { qDebug ()<<"Button"; if (thread.isRunning()) { thread.stop(); ui->startButton->setEnabled(true); ui->stopButton->setEnabled(false); } } void Dialog::MsgStr(QString &str) { qDebug ()<<str; } ///////////////////////////////////// #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include "mythread.h" #include <QDebug> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private slots: void on_startButton_clicked(); void on_stopButton_clicked(); void MsgStr(QString &str); private: Ui::Dialog *ui; CanThread thread; }; #endif // DIALOG_H //////////////////////////////////////////////////////// #include "mythread.h" #include <QDebug> CanThread::CanThread(QObject *parent) : QThread(parent) { stopped = false; } void CanThread::run() { qreal i = 0; exec (); while (!stopped) { // qDebug() << QString("in CanThread: %1").arg(i); msleep(1000); QString str = QString("in CanThread: %1").arg(i); emit sendStr(str); // exportFun(); i++; } stopped = false; } void CanThread::exportFun() { msleep(1000); } void CanThread::stop() { stopped = true; qDebug ()<<"start 1"; // qDebug ()<<"start 2"; } //////////////////////////////////////// #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> class CanThread : public QThread { Q_OBJECT public: explicit CanThread(QObject *parent = 0); void stop(); void exportFun(); protected: void run(); private: volatile bool stopped; signals: void sendStr(QString &); public slots: }; #endif // MYTHREAD_H
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页