QT信号槽传递参数技巧
2017-12-24 21:26
309 查看
信号槽如何传递参数(或带参数的信号槽)
利用Qt进行程序开发时,有时需要信号槽来完成参数传递。带参数的信号槽在使用时,有几点需要注意的地方,下面结合实例进行介绍。
第一点:当信号与槽函数的参数数量相同时,它们参数类型要完全一致。
信号:
[cpp] view
plain
void iSignal(int b);
槽:
[cpp] view
plain
void MainWindow::iSlot(int b)
{
QString qString;
qDebug()<<qString.number(b);
}
信号槽绑定:
[cpp] view
plain
connect(this, SIGNAL(iSignal(int)), this, SLOT(iSlot(int)));
发送信号:
[cpp] view
plain
emit iSignal(5);
结果:
可以看出,参数已经成功传递。
第二点:当信号的参数与槽函数的参数数量不同时,只能是信号的参数数量多于槽函数的参数数量,且前面相同数量的参数类型应一致,信号中多余的参数会被忽略。
信号:
[html] view
plain
void iSignal(int a, float b);
槽:
[html] view
plain
void MainWindow::iSlot(int b)
{
QString qString;
qDebug()<<qString.number(b);
}
信号槽:
[html] view
plain
connect(this, SIGNAL(iSignal(int, float)), this, SLOT(iSlot(int)));
发送信号:
[html] view
plain
emit iSignal(5, 0.3);
结果:
此外,在不进行参数传递时,信号槽绑定时也是要求信号的参数数量大于等于槽函数的参数数量。这种情况一般是一个带参数的信号去绑定一个无参数的槽函数。如下例所示。
信号:
[cpp] view
plain
void iSignal(int a, float b);
槽函数:
[cpp] view
plain
void MainWindow::iSlot() //int b
{
QString qString = "I am lyc_daniel.";
qDebug()<<qString;
}
信号槽:
[html] view
plain
connect(this, SIGNAL(iSignal(int, float)), this, SLOT(iSlot()));
发送信号:
[html] view
plain
emit iSignal(5, 0.3);
结果:
愿以上内容能给你带去帮助!
版权声明:自由转载-非商用-非衍生-保持署名 | Creative
Commons BY-NC-ND 3.0
博客网址:http://blog.csdn.net/lyc_daniel/article/details/12047819
博 主: lyc_daniel
方法一:
connect(button1, SIGNAL(clicked()), this, SLOT(buttonClick()));
connect(button2, SIGNAL(clicked()), this, SLOT(buttonClick()));
button1.setObjectName("1");
button2.setObjectName("2");
void YourWidget::buttonClick()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
if(clickedButton != NULL)
{
if(clickedButton->objectName() == "1")
{
//button1
}
if(clickedButton->objectName() == "2")
{
//button2
}
}
}
利用Qt进行程序开发时,有时需要信号槽来完成参数传递。带参数的信号槽在使用时,有几点需要注意的地方,下面结合实例进行介绍。
第一点:当信号与槽函数的参数数量相同时,它们参数类型要完全一致。
信号:
[cpp] view
plain
void iSignal(int b);
槽:
[cpp] view
plain
void MainWindow::iSlot(int b)
{
QString qString;
qDebug()<<qString.number(b);
}
信号槽绑定:
[cpp] view
plain
connect(this, SIGNAL(iSignal(int)), this, SLOT(iSlot(int)));
发送信号:
[cpp] view
plain
emit iSignal(5);
结果:
可以看出,参数已经成功传递。
第二点:当信号的参数与槽函数的参数数量不同时,只能是信号的参数数量多于槽函数的参数数量,且前面相同数量的参数类型应一致,信号中多余的参数会被忽略。
信号:
[html] view
plain
void iSignal(int a, float b);
槽:
[html] view
plain
void MainWindow::iSlot(int b)
{
QString qString;
qDebug()<<qString.number(b);
}
信号槽:
[html] view
plain
connect(this, SIGNAL(iSignal(int, float)), this, SLOT(iSlot(int)));
发送信号:
[html] view
plain
emit iSignal(5, 0.3);
结果:
此外,在不进行参数传递时,信号槽绑定时也是要求信号的参数数量大于等于槽函数的参数数量。这种情况一般是一个带参数的信号去绑定一个无参数的槽函数。如下例所示。
信号:
[cpp] view
plain
void iSignal(int a, float b);
槽函数:
[cpp] view
plain
void MainWindow::iSlot() //int b
{
QString qString = "I am lyc_daniel.";
qDebug()<<qString;
}
信号槽:
[html] view
plain
connect(this, SIGNAL(iSignal(int, float)), this, SLOT(iSlot()));
发送信号:
[html] view
plain
emit iSignal(5, 0.3);
结果:
愿以上内容能给你带去帮助!
文档信息
版权声明:自由转载-非商用-非衍生-保持署名 | CreativeCommons BY-NC-ND 3.0
博客网址:http://blog.csdn.net/lyc_daniel/article/details/12047819
博 主: lyc_daniel
方法一:
connect(button1, SIGNAL(clicked()), this, SLOT(buttonClick()));
connect(button2, SIGNAL(clicked()), this, SLOT(buttonClick()));
button1.setObjectName("1");
button2.setObjectName("2");
void YourWidget::buttonClick()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
if(clickedButton != NULL)
{
if(clickedButton->objectName() == "1")
{
//button1
}
if(clickedButton->objectName() == "2")
{
//button2
}
}
}
相关文章推荐
- QT信号槽传递参数技巧
- QT信号槽传递参数技巧
- QT信号槽传递参数技巧
- Qt学习:线程间共享数据(使用信号槽传递数据,必须提前使用qRegisterMetaType来注册参数的类型)
- Qt信号与槽的参数传递原理
- Qt 信号槽传递自定义结构体参数 详解
- Qt的信号中没有足够的参数传递到槽中的实现方法
- Qt多线程间信号槽传递非QObject类型对象的参数
- Qt 信号槽如何传递参数(或带参数的信号槽)
- 《OpenCV》Part11 OpenCV3.1.0 Qt中的信号槽函数传递cv::Mat格式参数
- qt的Qt::ConnectionType详细以及信号参数传递
- Qt:信号槽机制传递复杂类型参数(窗口通信)
- Qt多线程间信号槽传递非QObject类型对象的参数
- 信号槽传递非Qt库类型参数时,出现QObject::connect: Cannot queue arguments of type 'QUuid'(Make sure 'string' is regi
- QT信号和槽参数传递
- QT信号和槽参数传递复杂结构的数据
- Qt中带有参数的信号传递出错--自定义类型的参数
- Qt多线程间信号槽传递非QObject类型对象的参数
- pyqt信号和槽传递额外参数
- javascript技巧:用闭包传递参数