Main(函数main在那里)我的程序的线程保留用于非GUI任务.它调用了许多冗长的计算功能.所有已实现的GUI都在一个单独的线程中完成它们的工作.
我现在要使用Qt实现一个GUI.Qt文档说所有GUI相关的任务都应该在主线程中完成.在我的例子中,在主线程中偶尔插入QCoreApplication :: processEvents()调用几乎没用,因为它们之间有很大的延迟.
有没有办法克服Qt的这种约束?是不可能在Qt程序的主线程中做一些非GUI相关的事情?
我想一步一步地更新我的QMainWindow.我使用睡眠方法,但我看不到变化.我想每隔3秒看一次变化.
void MainWindow::updateScreen()
{
ui->pushButton1->show();
QThread::sleep(3);
ui->pushButton2->show();
QThread::sleep(3);
ui->pushButton3->show();
QThread::sleep(3);
}
Run Code Online (Sandbox Code Playgroud)
但在9秒后,所有更改立即生效.
我用PyQt制作端口扫描程序,但是当我激活循环时,Gui冻结.我怎么能解决这个问题?我添加了time.sleep()函数,但它仍然冻结.这是它冻结的功能.谢谢.
try:
time.sleep(1)
hostname=self.adres.text()
hostip=socket.gethostbyname(hostname)
uyari1="Scanning remote host, {}\n".format(hostip)
self.durum.append(uyari1)
print(uyari1)
for port in range(1,1025):
time.sleep(0.1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((hostip, port))
if result == 0:
time.sleep(0.01)
print ("Port {}: \t Open".format(port))
self.durum.append("Port {}: \t Open\n".format(port))
sock.close()
Run Code Online (Sandbox Code Playgroud)
完整代码:
import socket,os,time
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig) …Run Code Online (Sandbox Code Playgroud)