Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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
Qt 为什么gui线程对在插槽中调用QLabel控件的setText方法反应缓慢? 问题_Qt_Qthread_Ui Thread_Qlabel_Slot - Fatal编程技术网

Qt 为什么gui线程对在插槽中调用QLabel控件的setText方法反应缓慢? 问题

Qt 为什么gui线程对在插槽中调用QLabel控件的setText方法反应缓慢? 问题,qt,qthread,ui-thread,qlabel,slot,Qt,Qthread,Ui Thread,Qlabel,Slot,我正在用QT开发一个程序。最近我发现,当我在线程中发出信号时,有时在插槽函数中调用QLabel控件的repaint方法需要两到三秒钟。而有时只需要一毫秒。当我在线程的run函数中让它睡眠不同的秒数时,会有不同。这是我的密码: main.cpp 但是,在我再次编辑testthread.cpp中的run方法并使其休眠200毫秒后,在执行程序后,我会得到以下结果: "2021-05-26 00:15:31:641" :【MainWindow::onTestSlot】start re

我正在用QT开发一个程序。最近我发现,当我在线程中发出信号时,有时在插槽函数中调用QLabel控件的repaint方法需要两到三秒钟。而有时只需要一毫秒。当我在线程的run函数中让它睡眠不同的秒数时,会有不同。这是我的密码:

main.cpp 但是,在我再次编辑testthread.cpp中的run方法并使其休眠200毫秒后,在执行程序后,我会得到以下结果:

"2021-05-26 00:15:31:641" :【MainWindow::onTestSlot】start repaint
"2021-05-26 00:15:34:605" :【MainWindow::onTestSlot】end repaint
"2021-05-26 00:14:55:954" :【MainWindow::onTestSlot】start repaint
"2021-05-26 00:14:55:970" :【MainWindow::onTestSlot】end repaint

我不知道为什么gui线程对重新绘制的响应如此之慢。是否有任何解决方案使其快速响应?感谢您的帮助。

如果您希望立即更新ui,可以将连接模式更改为“Qt::BlockingQueueConnection”。并且在“setData”方法之后是否需要调用“repaint()”?
祝你好运~

QMainWindow
中使用
QLabel
作为中心小部件(在缺少
ui
文件的情况下),我无法复制。如果将
QThread::msleep(500)放在;发射sigTest()while(true){…
循环中的code>语句?对
QLabel::repaint
的所有调用都比预期的时间长还是仅第一次调用?Qt是一个信号驱动的框架。
QThread::msleep()的用法
和类似的方法在某些情况下不是最好的方法。此外,在多线程环境中,您应该在信号和插槽之间使用排队连接。@G.M.谢谢您的回答。我已经再次编辑了我的问题,并在
主窗口.ui
中添加了代码。我也像您所说的那样进行了尝试,并将
QThread::msleep(500);emit sigTest();
语句在
while(true){…
循环中,然后似乎第一次调用所用的时间比预期的长(大约四秒),而任何其他调用只需要预期的一毫秒。@NoobNoob感谢您的回答。我使用了
QThread::msleep()
这里是为了替换执行时可能需要很长时间的其他代码。这样我可以使代码简单易懂,便于读者帮助我分析代码。
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_testThread = new TestThread();
    connect(m_testThread, &TestThread::sigTest, this, &MainWindow::onTestSlot);
    m_testThread->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onTestSlot()
{
    ui->label_resultSimilarity->setText("test");
    qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss:zzz") << ":【MainWindow::onTestSlot】start repaint";
    ui->label_resultSimilarity->repaint();
    qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss:zzz") << ":【MainWindow::onTestSlot】end repaint";
}
#ifndef FACERECOGNIZETHREAD_H
#define FACERECOGNIZETHREAD_H
#include <QThread>
#include <QImage>
#include <QDebug>
#include <QMainWindow>

class TestThread: public QThread
{
    Q_OBJECT
public:
    TestThread();
protected:
    void run();

signals:
    void sigTest();
};

#endif // FACERECOGNIZETHREAD_H
#include "testthread.h"
#include <QApplication>

TestThread::TestThread()
{

}

void TestThread::run()
{
    //QThread::msleep(200);
    QThread::msleep(500);
    emit sigTest();
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>817</width>
    <height>478</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="label_preview">
    <property name="geometry">
     <rect>
      <x>93</x>
      <y>9</y>
      <width>571</width>
      <height>401</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <property name="layoutDirection">
     <enum>Qt::LeftToRight</enum>
    </property>
    <property name="frameShape">
     <enum>QFrame::Box</enum>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="scaledContents">
     <bool>false</bool>
    </property>
   </widget>
   <widget class="QWidget" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>679</x>
      <y>10</y>
      <width>131</width>
      <height>381</height>
     </rect>
    </property>
    <widget class="QLabel" name="label_resultImage">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>20</y>
       <width>111</width>
       <height>151</height>
      </rect>
     </property>
     <property name="frameShape">
      <enum>QFrame::Box</enum>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
    <widget class="QLabel" name="label_resultSimilarity">
     <property name="geometry">
      <rect>
       <x>20</x>
       <y>210</y>
       <width>91</width>
       <height>20</height>
      </rect>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>817</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

"2021-05-26 00:15:31:641" :【MainWindow::onTestSlot】start repaint
"2021-05-26 00:15:34:605" :【MainWindow::onTestSlot】end repaint
"2021-05-26 00:14:55:954" :【MainWindow::onTestSlot】start repaint
"2021-05-26 00:14:55:970" :【MainWindow::onTestSlot】end repaint