Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在长时间运行的点击按钮功能(插槽)中更新QLabel(多次)_C++_Qt - Fatal编程技术网

C++ 在长时间运行的点击按钮功能(插槽)中更新QLabel(多次)

C++ 在长时间运行的点击按钮功能(插槽)中更新QLabel(多次),c++,qt,C++,Qt,我正在创建一个控制台程序的图形界面 程序执行命令并定期更新QLabel。 问题是我无法让它动态更新,函数正确执行,但是直到执行结束,QLabel才更新 void MainWindow::on_pushButton_10_clicked() { estado_busqueda(2); // call function to change label, GUI should be updated immidiately system("sleep 2"); //

我正在创建一个控制台程序的图形界面

程序执行命令并定期更新
QLabel
。 问题是我无法让它动态更新,函数正确执行,但是直到执行结束,
QLabel
才更新

void MainWindow::on_pushButton_10_clicked()
{
    estado_busqueda(2); // call function to change label, GUI should be updated immidiately
    system("sleep 2"); // some long running system call
    estado_busqueda(1); // call function to change label again 

}

void *MainWindow::estado_busqueda(int salida){
    if(salida==1){
        ui->face->setStyleSheet("image: url(:/media/sad.png);");
        ui->song_info->setText("no puede encontrar datos de tu enlace");
        ui->estado->setStyleSheet("image: url(:/media/search.png);");
    }
    else if (salida==2){
        ui->informacion->setText("Obteniendo data del enlace");
        ui->estado->setStyleSheet("image: url(:/media/esperando.png);");
    }
    else{

    }
}
问题是我无法让它动态更新,函数正确执行,但QLabel直到执行结束才更新

小部件在同一个线程中执行。这意味着当你调用sleep时,你冻结了图形线程。这就解释了为什么什么都没发生

正如评论中所说,estado_busqueda需要一个插槽,您应该启动一个计时器,该计时器将在按下按钮时执行下一次更新。基本上,@eyllanesc建议应该有效

QTimer::singleShot(2000, this, [this](){estado_busqueda(1); };

你好我测试但是,/home/static/c++/c-/youtube/mainwindow.cpp:25:错误:没有从(lambda at/home/static/c++/c-/youtube/mainwindow.cpp:25:34)到“const char*”QTimer::singleShot(2000,this,[this](){estado_busqueda(1);})的可行转换;Opps,更改
系统(“睡眠2”);estado_busqueda(1)
QTimer::singleShot(2000,this,[this](){estado_busqueda(1);};
编译器说预期的”;“我添加了QTimer::singleShot(2000,this,[this](){estado_busqueda(1);});但没有继续可行的转换:(…这个完整的errro msg/home/static/c++/c-/youtube/mainwindow.cpp:25:错误:没有匹配函数用于调用'QTimer::singleShot(int,mainwindow*,mainwindow::on_button_10_clicked():)'QTimer::singleShot(2000,this,[this](){estado_busqueda(1);})^为什么estado_busqueda返回指针?,将
void*主窗口::estado_busqueda(int-salida){
更改为
void主窗口::estado_busqueda(int-salida){
i更改,但仍显示不可更改的转换:/