请教关于QlineEdit的textchanged信号问题

Trianglewave 2014-07-31 09:42:41
我最近在开发一个软件,软件中有一个功能
我有一个QDoubleSpinBox控件和一个QlineEdit控件
现在想要实现当QDoubleSpinBox的值或者QLineEdit的值改变的时候,另外一个也相应的根据一个公式发生改变。我现在的做法是写两个槽函数,跟QDoubleSpinBox的valuechanged信号绑在一起,另一个与QlineEdit的textchanged信号绑在一起,来实现一边的值改变的时候,另外一边也跟着变。
但是现在出现了一个问题,例如我在QLineEdit在输入一个值,当我值还没有完全输入完成的时候,textchanged信号好像就已经发出了,它使槽函数更改了QDoubleSpinBox的值,在QDoubleSpinBox的时候,又发出了valuechanged的信号,使得我还没有输完QlineEdit的值就被程序自己改掉了,改掉的也不是我想要的值

具体的例子:
QDoubelSpinBox的值为2.5,QlineEdit的值为400
如果我想要输入QlineEdit的值为100,这时QDoubelSpinBox的值会变成30

但是我刚输入一个1,这时QDoubleSpinBox的值就自己变成99.99(上限),然后因为QDoubleSpinBox的值变了,QLineEdit的值就变成11.01,不是我想要的值

我想问一下各位有没有什么其他的办法解决这个问题?
...全文
561 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Trianglewave 2014-07-31
  • 打赏
  • 举报
回复
引用 3 楼 soulfy 的回复:
void QLineEdit::editingFinished() [signal] This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.
非常感谢,用了这个信号之后感觉好多了,qt的文档我没有认真去看
soulfy 2014-07-31
  • 打赏
  • 举报
回复
void QLineEdit::editingFinished() [signal] This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.
Trianglewave 2014-07-31
  • 打赏
  • 举报
回复
引用 1 楼 dbzhang800 的回复:
从你的描述看,你需要的并不是QLineEdit的textchanged信号,对吧? 所以,你可以用其他信号,或者直接加一个确认按钮都可以
我把它改成returnpressed信号了,按回车才会响应 但是这样客户用起来如果没按回车,我程序里面的值也不会更改 最后计算出来的结果就有误了 看来还是像你说的,添加一个确认按钮好了
dbzhang800 2014-07-31
  • 打赏
  • 举报
回复
从你的描述看,你需要的并不是QLineEdit的textchanged信号,对吧? 所以,你可以用其他信号,或者直接加一个确认按钮都可以

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧