让我举个例子.
class B : public QThread {
public:
void run() {
}
};
class A : public QThread {
public:
void run() {
b1.start(); b2.start();
}
protected:
B b1, b2;
};
Run Code Online (Sandbox Code Playgroud)
我希望A :: b1和A :: b2作为完全独立的线程运行,而不是共享父线程(A)的资源.有没有办法将主线程指定为b1和b2的父线程?
我也看过QThreadPool和QRunnable,我不明白,怎样才能管理所有runnables(例如,停止其中一个,然后再次运行)threadpool.
我开始了解使用线程的机制,但我想我被困住了,
\n\n如果我理解,我必须创建自己的类,释放run()方法,然后创建线程。
问题是我的线程必须从 gui(主线程)读取一些变量,并且使用它们,它将创建一些其他变量,主窗口将读取并绘制这些变量。
\n\n问题是 I\xe2\x80\x99m 接收蓝牙连接,该连接必须始终在线程中处于活动状态,但 gui 必须绘制从该线程读取的值。
\n\n这是需要位于单独线程上的函数:
\n\n// Listen to the device for data\nvoid gui::listen_device()\n{\n unsigned char buf[10];\n unsigned char crcval;\n fd_set readmask;\n struct timeval tv;\n\n tv.tv_sec = 0;\n tv.tv_usec = 28000;\n\n memset (buf, 0, 10);\n\n int v = 0, v1 = 0, v2 = 0;\n\n while(1)\n {\n int i;\n FD_ZERO (&readmask);\n FD_SET (sock, &readmask);\n if (select (255, &readmask, NULL, NULL, &tv) > 0)\n {\n if (FD_ISSET (sock, &readmask))\n {\n …Run Code Online (Sandbox Code Playgroud) 我正在浏览链接
\n\n\n\n我对一些说法感到困惑。在第一个链接中,它说
\n\n\n\n\nQThread 中的所有函数都是编写的,旨在从创建线程调用,而不是从 QThread 启动的线程调用。
\n
虽然它建议使用moveToThread将对象移动到新线程,而不是子类化QThread。我的问题是:
run方法调用的默认实现exec,会创建一个事件循环,当使用 改变对象的线程关联性moveToThread时,所有的操作slots都会在新线程中执行,而不是在创建线程上执行,这与前面提到的预期用途相矛盾相矛盾。我错过了什么吗?
第二个问题:
\n\n在第三个链接中说
\n\n\n\n\n事件队列属于线程而不是事件循环,并且它\xe2\x80\x99被该线程中运行的所有事件循环共享。
\n
我的问题是单个线程中如何可以有多个事件循环?我的理解是,事件循环通过事件队列循环,直到exit/terminate被调用,并处理每个event到达该队列的事件。如果这是真的,一个循环将永远不会结束(除非 exit/terminate被调用),另一个循环如何开始?任何演示它的示例代码都将受到高度赞赏。
我正在尝试实现QT Qthread的sleep函数,所以我在头文件中声明为 -
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
static void sleep(unsigned long secs){QThread::sleep(secs);}
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_clicked();
};
Run Code Online (Sandbox Code Playgroud)
在我的源代码中我正在做的是在连接到数据库之后,我想要一个标签来改变背景颜色(有点像发光效果),所以我尝试从一个while(true)循环内部调用sleep函数.
while(db.open())
{
MainWindow::sleep(13);
qDebug()<<"Success ";
ui->glow_label->setStyleSheet("QLabel {background-color: rgb(0, 255, 0);}");
MainWindow::sleep(5);
ui->glow_label->setStyleSheet("QLabel {background-color: rgb(0, 85, 255);}");
}
Run Code Online (Sandbox Code Playgroud)
但它在构建时显示错误 - >
/usr/local/Trolltech/Qt-4.8.4/include/QtCore/qthread.h:115:错误:'static void QThread :: sleep(long unsigned int)'受保护/ home/aj/MY_QT_WORK/timer_test/mainwindow .h:22:错误:在这种情况下
我做错了什么想法?
我想知道如何暂停 QThread,然后在收到信号时恢复。我已阅读并知道我可以做这样的事情:
def run(self):
...
self.ready=False
while not self.ready:
self.sleep(1)
...
...
@QtCore.Slot()
def set_ready(self):
self.ready = True
Run Code Online (Sandbox Code Playgroud)
但是,我想要做的是避免线程中的轮询。我不想将睡眠时间设置得很短并继续检查。我想睡觉,直到我从主线程收到继续的信号。
我在我的线程中正在做的是这样的:
(pseudo code)
with open file:
read a block of data
while data:
sendThread = send the block via UDP
read the next block of data
while not ready or sendThread.isRunning:
sleep(0.1)
Run Code Online (Sandbox Code Playgroud)
在我的主线程中,我设置了一个QtNetwork.QUdpSocket将readyRead连接到一个方法来处理传入的数据报并对其进行解码。当它收到我正在等待的响应时,它会向插槽发送一个信号,set_ready告诉线程发送另一个数据报。我并不总是知道其他系统需要多长时间才能响应,尽管我可能会有 30 秒左右的较长超时值。
有没有办法中断线程的睡眠?所以我可以做这样的事情:
sleep(30)
if not ready:
Timeout occurred stop processing
else:
Continue processing.
Run Code Online (Sandbox Code Playgroud) 我有以下代码,但它抱怨我无法从我的线程访问 UI 数据。在下面的示例代码中,访问该userInputString值以便线程可以运行的最佳方法是什么?
self.nameField 是一个 PyQt QLineEdit。
QObject::setParent:无法设置父级,新父级在不同的线程中
QPixmap:在 GUI 线程之外使用像素图是不安全的
QWidget::repaint:检测到递归重绘
import myUI
class MainUIClass(QtGui.QMainWindow, myUI.Ui_MainWindow):
def __init__(self, parent=None):
super(MainUIClass, self).__init__(parent)
self.setupUi(self)
self.startbutton.clicked.connect(self.do_work)
self.workerThread = WorkerThread()
self.connect(self.workerThread, SIGNAL("myThreading()"), self.myThreading, Qt.DirectConnection)
def do_work(self):
self.userInputString = self.nameField.Text()
self.workerThread.start()
def myThreading(self):
if userInputString is not None:
#Do something
class WorkerThread(QThread):
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
def run(self):
self.emit(SIGNAL("myThreading()"))
if __name__ == '__main__':
a = QtGui.QApplication(sys.argv)
app = MainUIClass()
app.show()
a.exec_()
Run Code Online (Sandbox Code Playgroud) 我一直在编写一个在服务器上运行远程脚本的程序。因此,我需要用一个栏来显示进度,但不知何故,当我运行代码时,GUI 开始冻结。我已经使用了 QThread 和 SIGNAL 但不幸的是无法成功。
下面是我的代码;
class dumpThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def sendEstablismentCommands(self, connection):
# Commands are sending sequently with proper delay-timers #
connection.sendShell("telnet localhost 21000")
time.sleep(0.5)
connection.sendShell("admin")
time.sleep(0.5)
connection.sendShell("admin")
time.sleep(0.5)
connection.sendShell("cd imdb")
time.sleep(0.5)
connection.sendShell("dump subscriber")
command = input('$ ')
def run(self):
# your logic here
# self.emit(QtCore.SIGNAL('THREAD_VALUE'), maxVal)
self.sendEstablismentCommands(connection)
class progressThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
# your logic here
while 1:
maxVal = 100
self.emit(SIGNAL('PROGRESS'), maxVal)
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self) …Run Code Online (Sandbox Code Playgroud) 我正在使用 QThread 在单独的线程中进行一些计算。通过单击按钮启动线程,启动函数 StartMeasurement()。Thread 可以自己完成这个过程(在完成计算之后)并发出 PyQT 信号完成。或者用户可以通过单击 stopBtn 来停止线程。
terminate() 函数正在工作,但是当我再次尝试启动线程时遇到了很多麻烦。
在这里使用 movetoThread() 方法是否值得推荐?或者我如何确保正确停止线程以启用正确的重启。(意思是,开始新的!)
def StartMeasurement(self):
self.thread = measure.CMeasurementThread(self.osziObj, self.genObj, self.measSetup)
self.thread.newSample.connect(self.plotNewSample)
self.thread.finished.connect(self.Done)
self.stopBtn.clicked.connect(self.thread.terminate)
self.stopBtn.clicked.connect(self.Stop)
self.thread.start()
Run Code Online (Sandbox Code Playgroud) 问题描述
我正在尝试制作一个应用程序来收集数据、处理数据、显示数据和一些驱动(打开/关闭阀门等)。作为对我有一些更严格时间限制的未来应用程序的实践,我想在单独的线程中运行数据捕获和处理。
我当前的问题是它告诉我我无法从另一个线程启动计时器。
当前代码进度
import sys
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QThread, pyqtSignal
# This is our window from QtCreator
import mainwindow_auto
#thread to capture the process data
class DataCaptureThread(QThread):
def collectProcessData():
print ("Collecting Process Data")
#declaring the timer
dataCollectionTimer = PyQt5.QtCore.QTimer()
dataCollectionTimer.timeout.connect(collectProcessData)
def __init__(self):
QThread.__init__(self)
def run(self):
self.dataCollectionTimer.start(1000);
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # gets defined in the UI file
self.btnStart.clicked.connect(self.pressedStartBtn)
self.btnStop.clicked.connect(self.pressedStopBtn)
def pressedStartBtn(self):
self.lblAction.setText("STARTED")
self.dataCollectionThread = DataCaptureThread()
self.dataCollectionThread.start()
def pressedStopBtn(self):
self.lblAction.setText("STOPPED") …Run Code Online (Sandbox Code Playgroud) 我有一个 python 定义的工作器QObject,它有一个work()由 QML UI 调用的慢速插槽(在我的实际 UI 中,FolderListModel当用户浏览列表时,该方法在每个项目上动态调用,但对于他的示例代码我'我只是在窗口完成时调用它作为示例)。
我想work异步运行慢速以防止 UI 阻塞。我想通过在 QThread 上移动 Worker 实例并在那里调用插槽来做到这一点,但这不起作用,因为 UI 仍然被阻塞,等待结果的work()到来。
这是我迄今为止尝试的代码:
mcve.qml:
import QtQuick 2.13
import QtQuick.Window 2.13
Window {
id: window
visible: true
width: 800
height: 600
title: qsTr("Main Window")
Component.onCompleted: console.log(worker.work("I'm done!")) // not the actual usage, see note in the question
}
Run Code Online (Sandbox Code Playgroud)
mcve.py:
import QtQuick 2.13
import QtQuick.Window 2.13
Window {
id: window
visible: true
width: 800
height: 600
title: …Run Code Online (Sandbox Code Playgroud)