本文介绍了如何知道哪些QLineEdit发出的editingFinished()里面的信号处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想对几个类似的 QLineEdit 对象的用户输入实现自定义响应。我想创建一个 editingFinished()或 textChanged()信号的共同处理程序,并将其分配给所有 QLineEdit s。但是,响应需要信号发送者的知识 - 例如,它必须用不同的颜色突出显示输入的文本。 如何知道发送者 您可以通过调用 QObject :: sender(),然后将此指针转换为QLineEdit。类似 void MyClass :: onTextChanged(const QString& text) { QLineEdit * edit = qobject_cast< QLineEdit *>(sender()); if(edit) { //使用QLineEdit执行操作} else { //只是为了确保没有错误连接信号} } I want to implement a custom response to user input for several similar QLineEdit objects. I want to create a common handler of editingFinished() or textChanged() signal and assign it to all the QLineEdits. However, the response requires the knowledge of the sender of the signal - for example, it must highlight the entered text with different colors.How do I know the sender of the signal inside it's handler? 解决方案 You can get pointer to sender with call to QObject::sender() and then cast this pointer to QLineEdit. Something likevoid MyClass::onTextChanged(const QString& text){ QLineEdit* edit = qobject_cast<QLineEdit*>(sender()); if (edit) { // Do something with QLineEdit } else { // Just to make sure that you have not make mistake with connecting signals }} 这篇关于如何知道哪些QLineEdit发出的editingFinished()里面的信号处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 14:35