c++ - Qt how to connecto Line Edit to progress bar
问题描述
I am very new to QT and C++, I am trying to connect Line edit to progress bar so when I enter some integer value to the Line Edit, progress bar will show it. I could not achieve it. This is what I was trying:
connect(ui->batterycapacity,&QLineEdit::textChanged, ui->progressBar,ui->progressBar->setValue(ui->batterycapacity->text().toInt()));
or this:
connect(ui->batterycapacity,&QLineEdit::textChanged, ui->progressBar,ui->progressBar->&QProgressBar::setValue(ui->batterycapacity->text().toInt()));
batterycapacity is my Line Edit. How can I connect those 2 together? Thanks beforehand.
解决方案
你很近。由于信号的参数与槽的参数不同,您需要对其进行调整toInt
,但您不能简单地将任意代码粘贴在参数中并期望 C++ 在信号更改时执行它。
您需要将代码段包装在lambda 表达式中:
connect(ui->batterycapacity, &QLineEdit::textChanged, ui->progressBar,
[=](const QString& text) {
ui->progressBar->setValue(text.toInt()));
});
lambda 将接收textChanged
信号的参数并将其传递给setValue
方法。[=]
前面的位告诉编译器按值捕获 的值,ui
以便在 lambda 内部可以访问它。
推荐阅读
- xcode - 如何在命令行 xcode 构建中排除特定架构
- flexbox - Quasar Grid 左对齐最后一行的项目
- amazon-web-services - 从 AWS RDS 实例访问从 AWS RDS 快照导出到 S3 的数据库数据
- google-analytics - 为什么带有包含“gclid”的查询字符串的传入服务器请求格式错误?
- vue.js - Vuetify - 使用 URL 参数同步分页
- java - Kafka No Acknowledgement 可用作参数
- python - PyDrake:从机器人中提取 AutoDiff 梯度(通过微分逆运动学控制)
- php - Laravel PHP以特定方式返回数组
- c# - MVC SPA 应用程序
- .net - 在 Azure 中发布:不匹配依赖项错误。是什么导致它以及在通过管道使用发布时如何解决它?