QT在运行时更新Linedit
QT update Linedit at runtime
我试图更新我的linedit上的文本每次我得到一个帧,但我的程序崩溃。我试着用循环做同样的事情,但是窗口只在循环完成后显示。setH()是我的插槽,在调试模式下它运行完美,问题是当程序运行时(主窗口在屏幕上)试图更新LineEdit中的文本。谢谢你
void MainWindow::updatehand(){
if (controller.isConnected()){
int hc =frame.hands().count();
QString hndc= QString::number(hc);
emit hChanged(hndc);
}
void MainWindow::setH(const QString hndc){
handsRead->setText(hndc);
updatehand();
}
这就是崩溃的原因:
connect(this, SIGNAL(hChanged(const QString)), this, SLOT(setH(const QString)));
这种连接实际上是一个直接的函数调用。调用函数setH()
代替emit hChanged(hndc);
。然后从setH()
调用updatehand()
函数。
这是一个无限循环,堆栈溢出崩溃。
如果你想每秒调用updatehand()
60次,可以使用QTimer
调用,例如使用QTimer
static member:
void MainWindow::setH(const QString hndc){
handsRead->setText(hndc);
QTimer::singleShot(1000 / 60, this, SLOT(updatehand()));
}
这里updatehand()
也是一个槽。
在这种情况下,偶数循环在从setH()
返回后继续调度UI消息。
大约16毫秒后,计时器将调用updatehand()
。
上述解决方案在技术上打破了无限交叉引用循环。然而,它可以做得更好。存在外部调用者多次触发setH()
的风险。在这种情况下,许多计时器将被激活。看起来您只需要一个QTimer
实例来周期性地独立于setH()
调用updatehand()
。因此,updatehand()
可以与给定时间段的更新数据合用。它可以直接调用setH()
, setH()
函数只设置QLineEdit
文本:
void MainWindow::setH(const QString hndc){
handsRead->setText(hndc);
}
相关文章:
- 从C++本机插件更新Vector3数组
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- cmake更新缓存的变量
- 更新到莫哈韦后出现cmath错误
- OpenMP:并行更新数组总是需要减少数组吗
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 指针没有更新它在void函数内部指向的值
- 如何在c++中获取要更新的值
- 已修改的LinkedList未在文本文件本身中更新
- Qt:当QListView获得新条目时,如何更新QStringList
- 更新的矢量元素不打印
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- 如何在ECS框架中更新组件数据和通知系统
- 从VS 2015更新3更新到VS2015更新3 d后浮点计算行为不同的原因
- 如果我想在没有更新编译器的情况下使用新功能,该怎么办?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 在 emscripten 网页汇编正在运行期间更新进度条?
- 在运行时使用范围更新结果
- QT在运行时更新Linedit