qthread

Qt moveToThread() vs calling new thread when do we use each

末鹿安然 提交于 2019-12-30 04:45:49
问题 When do we use each of this function calls in a threaded application. given two functions fun1() and fun2() defined in the same class dealing with read/write of data into buffers(queue operation). to achieve multi-threading to these. we would have to run the two functions in a separate thread. now lets say the first function read is called at the start of its thread. is it better to use moveTothread ( second thread)for function write at the start of the first functions thread Or define the

what is the correct way to implement a QThread… (example please…)

梦想与她 提交于 2019-12-27 10:32:49
问题 The Qt documentation for QThread says to create a class from QThread, and to implement the run method. Below is taken from the 4.7 Qthread documentation... To create your own threads, subclass QThread and reimplement run(). For example: class MyThread : public QThread { public: void run(); }; void MyThread::run() { QTcpSocket socket; // connect QTcpSocket's signals somewhere meaningful ... socket.connectToHost(hostName, portNumber); exec(); } So in every single thread I've created, I've done

Qt 多线程使用moveToThread

北城以北 提交于 2019-12-26 05:32:51
Qt有两种多线程的方法,其中一种是继承QThread的run函数, 另外一种是把一个继承于QObject的类用moveToThread函数转移到一个Thread里。 Qt4.8之前都是使用继承QThread的run这种方法,但是Qt4.8之后,Qt官方建议使用第二种方法。 具体的使用步骤如下: 1.从QObject派生一个类,将耗时的工作写在该类的 槽函数中。 2.将派生类对象移动到一个QThread中,该线程需要start。(这一步使用moveToThread) 3.通过信号连接派生类的槽函数,并通过信号触发槽函数。(槽函数在子线程中执行) //tes.h #ifndef TES_H #define TES_H #include <QCoreApplication> #include <QDebug> #include <QThread> #include <QString> #include <QObject> class Worker:public QObject { Q_OBJECT public: explicit Worker(QObject *parent=0); ~Worker(); signals: void sig_finish(); public slots: void slot_dowork(); }; #endif // TES_H //tes.cpp

QThread is running as Dummy Thread even after the Thread object is deleted

不打扰是莪最后的温柔 提交于 2019-12-25 09:28:29
问题 The QThread seems to be running in the background always even after the Thread object is deleted. I took this example to demonstrate the problem I'm facing. The thread is in active state all the time [When it's Running, Finished and Deleted] Refer the code below from PyQt4.QtCore import QThread, QTimer,QObject, pyqtSignal, pyqtSlot, Qt from PyQt4.QtGui import QApplication, QLabel, QWidget, QGridLayout import sys import time import threading import sip def activeThreads(): currentThread =

QT Threading issues… something is stalling GUI response

陌路散爱 提交于 2019-12-25 03:55:44
问题 I'm having issues with QT threading somehow stalling the main GUI. To answer the first question... yes, QThread is not subclassed, and is done the "right way". The thread is run long-term (not a "do x then exit"), and it's main loop does have delays/sleeps. The threads main purpose is to collect data from elsewhere, compose an image, and transmit that image over a COM port. There are coding areas that i think could be creating the problem. Im using my own "sleep" function as msleep is not

QT之深入理解QThread

耗尽温柔 提交于 2019-12-24 22:53:18
QT之深入理解QThread 理解QThread之前需要了解下QThread类,QThread拥有的资源如下(摘录于QT 5.1 帮助文档): 在以上资源中,本文重点关注槽:start();信号:started()、finished();受保护的方法:run()、exec(); 理解QThread QThread与通常所熟知的线程(thread)有很大出入,在面向过程的语言中,我们建立一个线程的同时会传入一个函数名,这个函数名代表该线程要执行的具体代码(如图 1 所示)。 图 1. 我们通常所理解的线程 但是QThread里并没有线程的具体代码,QThread只是一个接口而已,目的是为操作系统提供一个用于线程调度的“句柄”。这个“句柄”即是QThread的入口(如图 2 所示)。 图 2. QThread是“面向对象的” QThread的入口多种多样,可以是槽函数,也可能是某个事件处理函数,但是由于是由系统调度的,因此这些函数的“准确”执行时刻是无法预知的。 QThread的出口是finished()信号。 作为线程,QThread会毫不犹豫的为自己创建一个运行空间,一个单独的执行线索,一个新的线程,但是翻阅QThread所拥有的资源,我们找不到传入函数名的地方,因此我们仿佛无法为这个新创建的线程提供具体的执行代码。 很多人因此想到了run()方法,因而继承QThread函数

QThread with QTimer and QSerial - parenting

China☆狼群 提交于 2019-12-24 19:25:46
问题 I'm trying to make a "self contained" communication object allocated in an different Thread, to keep it isolated from the GUI processing delay. So, in an "AppCore" object creation I create one "CommCore" without parent: cAppCore::cAppCore(QObject *parent) : QObject(parent) { .... CommCore = new cCommCore; (here I do signal-slot connections between CommCore and AppCore) .... } and in the CommCore constructor I do the following: cCommCore::cCommCore(QObject *parent) : QObject(parent) {

c++ qthread starting 2 threads concurrently

情到浓时终转凉″ 提交于 2019-12-24 12:02:04
问题 I have two threads One and Two. defined by their respective classes in the header file.I want to start the second thread when the first thread is started. creating and starting the second thread in the constructor of the first produced unexpected result. my header file "header.h" #ifndef HEADER #define HEADER #include <QtGui> class One:public QThread { public: One(); void run(); }; class Two:public QThread { public: Two(); void run(); }; #endif my class file "main.cpp" #include "header.h"

Restart QThread with GUI

别来无恙 提交于 2019-12-24 03:12:39
问题 I am using QThread to do some calculations in a seperate Thread. The Thread get's started by a button click, witch launches the function StartMeasurement(). The Thread can finish the process by itself (after finished the calculations) and emits the PyQT Signal finished. Or the thread can be stopped by the User by the stopBtn click. The terminate() function is working, but I get a lot of troubles when I try to start the thread again. Is it recommendable to use the movetoThread() approach here?

Can I create a new style pyqt signal that isn't a field member of a class?

痞子三分冷 提交于 2019-12-24 01:52:42
问题 So for the only way that I can see to create a style signal with PyQt4 is as follows: class MyCustomClass(QtCore.QThread): custom_signal = QtCore.pyqtSignal(str) My beef is if I declare that signal anywhere else, pyqt throws an error at me about how custom_signal doesn't have a connect() function. I would like to create a helper function to help remove the boilerplate/repeated code when I want to do something as simple as: starting a new thread, doing work in that thread, sending the result