This is a thread that pastes data into QPlainTextEdit
Prepare data: 7+ million characters
python code
import time from PyQt5.QtCore import pyqtSignal, QMutex, QThread from collections import defaultdict from typing import Union from re import findall, sub from PublicSourceSection.PublicRecordLogger.Logger import LoggerRecord class ToolsStickForData(QThread): stick_process = pyqtSignal(int) stick_value = pyqtSignal(str) stick_state = pyqtSignal(bool) stick_mutex = QMutex() step=40000 def __init__(self, wtpipedict:Union[dict, defaultdict]): super(ToolsStickForData, self).__init__() startstick_time = time.time() self.WtpipeDict = wtpipedict self.stick_logger = LoggerRecord.Logger_Record self.stick_logger.debug(f'StartStickTime: {time.time() - startstick_time}') def run(self): try: self.stick_mutex.lock() sourcedata = sub('\ ', '', self.WtpipeDict["ClipboardData"]) clipboardlist = findall('.{%s}' % self.step, sourcedata) clipboardlist.append(sourcedata[-(len(sourcedata) % self.step):]) data_lenght = len(clipboardlist) self.stick_state.emit(False) endstick_time = time.time() for index, clipboard_data in enumerate(clipboardlist): try: if self.WtpipeDict['ThreadState'] == 'Destroyed': self.stick_mutex.unlock() self.stick_state.emit(True) self.quit() return elif self.WtpipeDict['ThreadState'] == 'Pause': while True: if self.WtpipeDict['ThreadState'] == 'Pause': QThread.msleep(30) continue elif self.WtpipeDict['ThreadState'] == 'Destroyed': break else: break else: self.stick_value.emit(clipboard_data) self.stick_process.emit(round(((index + 1) / data_lenght) * 100)) QThread.msleep(30) except BaseException as stickError: self.stick_logger.error(stickError) break self.stick_logger.debug(f'EndStickTime: {time.time() - endstick_time}') self.stick_mutex.unlock() self.stick_state.emit(True) self.stick_logger.debug(f"ToolsStickForData data stick funish") except Exception as StickThreadError: self.stick_logger.error(StickThreadError) finally: self.quit()
Running results: It takes 6 seconds, the interface is smooth and smooth without any lag, and the system can fully perform other operations without any pressure.
QT
#include "Thr_Engine/Thr_Stick.h" StickTData::StickTData(QMap<QString, QString>RdoMap){ Thr_Map.unite(RdoMap); } void StickTData::run(){ ST_Mutex.lock(); QTime TDB; TDB.start(); QString ClipboorData = QString(stream.GetDTIClip().c_str()).simplified(); ClipboorData = ClipboorData.split("\ ").join(""); double Size = ClipboorData.size(); double AddCount = 0; int Step = 20000; if (Size < Step) Step = Size; QString PushValue; QString RegTxt = ".{%1} + "; QRegularExpressionMatchIterator Iter = QRegularExpression(RegTxt.arg(Step)).globalMatch(ClipboorData); while (Iter.hasNext()) { QStringList MatchResult = Iter.next().capturedTexts(); if (MatchResult.size() >= 1) PushValue = MatchResult[0]; if (!PushValue.isEmpty()) { emit ST_Value(PushValue); emit ST_Proccess((AddCount / Size) * 100); AddCount + = (double)PushValue.size(); qDebug() << AddCount << Size << AddCount / Size; } QThread::msleep(30); } if (AddCount < Size) { QString LastChar = ClipboorData.mid(AddCount, -1); AddCount + = LastChar.size(); emit ST_Value(LastChar); emit ST_Proccess((AddCount / Size) * 100); } qDebug() <<tdB.elapsed() / 1000; ST_Mutex.unlock(); }
The step size cannot be set to 40000 like python. Even setting python to 50000 is no problem and the interface is still smooth. At this time, it is reduced to 20000, and the step interval is still 30 milliseconds.
operation result:
15 seconds, after all, the step size has been reduced, so what if I increase it to the same as python?
It took 7 seconds, but the interface could no longer be moved. The focus shifted to the mouse position after moving the mouse in the listwidget list for a few seconds.
python changes the step size to 50000,
Still silky smooth, takes 5 seconds
python connection function
def top_stickvalue_paste(self, value): self.Centra_Value_Plainedit.appendPlainText(value) value_plaintedit_cursor = self.Centra_Value_Plainedit.textCursor() value_plaintedit_cursor.movePosition(QTextCursor.End) self.Centra_Value_Plainedit.setTextCursor(value_plaintedit_cursor)
QT connection function
connect(STK, & amp;StickTData::ST_Value, DT_scdtEdit, & amp;QPlainTextEdit::appendPlainText); connect(STK, & amp;StickTData::ST_Proccess, DT_anlyGbar, & amp;QProgressBar::setValue);
They all use appendPlainText with the best performance. Is it a limitation of Qt Creator? The speed will be restored after packaging? , can anyone help me with the answer? I’m very confused.
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeDesktop application developmentPyQT384034 people are learning the system