qthread

PySide2 Qthread crash

╄→尐↘猪︶ㄣ 提交于 2020-01-24 17:55:12
问题 I want to use PySide2 Qtcore.Qthread because of Qtcore.Signal, but I end up with this error: Process finished with exit code -1073740791 from PySide2.QtCore import QThread class Thread(QThread): def run(self): print('task started') k = 0 for i in range(10000): for j in range(5000): k += 1 print('task finished') Thread().start() expect to have those prints but I have this error: Process finished with exit code -1073740791 Update: so, why this code also throw the same error? class Thread

Show progressbar in Qt with computationally heavy background process

扶醉桌前 提交于 2020-01-23 04:02:29
问题 I'm building an application that let's the user export his/her work. This is a computationally heavy process, lasting for a minute or so, during which I want to show a progress bar (and make the rest of the UI unresponsive). I've tried the implementation below, which works fine for a non-computationally expensive background process (e.g. waiting for 0.1 s). However, for a CPU heavy process, the UI becomes very laggy and unresponsive (but not completely unresponsive). Any idea how I can solve

QT多线程操作

家住魔仙堡 提交于 2020-01-22 13:47:38
QT的线程开启方式有俩种方法 方法一: 1.派生QThread类,重写父类的run函数.派生对象调用strat()函数; 二方法: 1.派生出一个QObject的子类,再创建一个QThread对象; 2.把需要在线程运行的函数写在QObject的子类的槽中; 3通过QObject的子类的对象moveToThread()移动到线程对象中去; 4.连接connect(线程对象,SIGNAL(strated()),QObject派生类对象,SLOT(func())); 5.线程对象调用strat()函数; 官网推荐第二种方法开启线程,这里需要注意的是一个QThread类的派生对象就是一条线程,当线程调用strat()函数,默认调用了run()函数,run函数又底层调用了exec()事件循环; 所以线程内函数运行结束了,需要回收资源的话,需要先调用quit()函数退出事件循环,再调用wait()来回收线程资源; 注:QObject派生类对象如果槽函数是死循环需要设置一个flag来终止,对象的资源回收需要线程来完成deleteLater(),不可以设置父类托管内存,因为该对象已经不在主线程中了; 示例: 新建一个窗口类,放入俩个LCD控件,一个按钮控件; widget.h 1 #ifndef WIDGET_H 2 #define WIDGET_H 3 4 #include <QWidget>

QThread blocking main application

匆匆过客 提交于 2020-01-20 22:14:06
问题 I have a simple form UI that has a slot for a button, starting a thread: void MainWindow::LoadImage() { aThread->run(); } And the run() method looks like this: void CameraThread::run() { qDebug("Staring Thread"); while(1) { qDebug("ping"); QThread::sleep(1); } } When I click the button that calls LoadImage(), the UI becomes unresponsive. I periodically see the "ping" message as the debug output but the UI hangs, does not respond to anything. Why is my thread not running separately?

QThread and QTimer

跟風遠走 提交于 2020-01-20 06:51:05
问题 I'm working on an application developed with Qt 4.6. I want to create a custom timer that counts in a separate thread. However, I want this timer to be able to send signals to the main thread. I subclassed QThread but it doesn't seem to work. Here is Timer.h: #ifndef TIMER_H #define TIMER_H #include <QtCore/QObject> #include <QtCore/QThread> #include <QtCore/QTimer> class Timer : public QThread { Q_OBJECT public: explicit Timer(QObject *parent = 0); ~Timer(); // true if the timer is active

用Python来做一个屏幕录制工具

谁说胖子不能爱 提交于 2020-01-18 13:58:33
一、写在前面 作为一名测试,有时候经常会遇到需要录屏记录自己操作,方便后续开发同学定位。以前都是用ScreenToGif来录屏制作成动态图,偶尔的机会看到python也能实现。那就赶紧学习下。 二、效果展示 三、知识串讲 这次要讲的东西可能比较多了,涉及到pyqt5 GUI软件的制作、QThread多线程的使用、Sikuli库的图形操作、win32库的模拟键盘操作、cv2库的写视频文件等。下面我们一点点来蚕食我这次写的代码。 1、GUI界面制作 这次我用的是现成的Pyqt5界面布局类,QVBoxLayout。这个类可以快速协助我完成按钮的垂直分布,而且按钮添加也更方便。 button1 = QPushButton("自定义录屏") layout.addWidget(button1) 两行代码就完成了按钮的命名和添加。我之前玩qt时,用的都是qt的UI界面,对应生成的组件代码也比较复杂。因此,在开发一些少量按钮、简单布局时可以用QVBoxLayout类。如果喜欢水平布局,可以用QHBoxLayout类,使用方法是一样的。 另外,在按钮点击关联的功能函数,即work()方法时,如果想带参数,可以通过lambda匿名函数来实现。这 也是个小技巧。 # 不带参数 button1.clicked.connect(self.work) # 带参数 button1.clicked.connect

QObject Cannot create children for a parent that is in a different thread

Deadly 提交于 2020-01-15 09:06:51
问题 Hi have need to do some socket communication from background, I used QtConcurrent::run to this, but giving me the warning. QObject: Cannot create children for a parent that is in a different thread. (Parent is MainWindow(0x7fff3e69f500), parent's thread is QThread(0x16f8070), current thread is QThread(0x17413d0) Here is the code, MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow){ ui->setupUi(this); QFuture<void> f2 = QtConcurrent::run(this,&MainWindow:

QT多线程的使用

风流意气都作罢 提交于 2020-01-15 04:45:22
Qt中提供了对于线程的支持,它提供了一些独立于平台的线程类,要进行多线程方法,可以有两种方式。 1. 第一种方式 qt提供QThread类,在QThread类中有一个virtual函数QThread::run()。 要创建一个新的线程,我们只需定义一个MyThread类,让其继承QThread,然后重新实现QThread::run()。 #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QObject *parent = 0); ~MyThread(); protected: //QThread的虚函数 //线程处理函数 //不能直接调用,通过start()间接调用 void run(); signals: void isDone(); //处理完成信号 signals: public slots: }; #endif // MYTHREAD_H }; 然后可以在run中写入要进行的操作,比如可以让其等待5秒。若不是多线程,在运行时我们单击窗口,窗口会出现无响应的状态。 那如何通知线程结束?这就可以用qt的信号和槽机制了,我们可以在操作完成时发出一个完成信号

creating QApplication in a different thread

我只是一个虾纸丫 提交于 2020-01-11 06:35:11
问题 I'm trying to create QApplication in a different thread, but found 2 main problems: 1- I can't interact with GUI 2- some warnings: WARNING: QApplication was not created in the main() thread. QObject::startTimer: timers cannot be started from another thread //happens when resizing widget QObject::killTimer: timers cannot be stopped from another thread here is the full code: (it may has some memory leaks but for testing purposes it fails) //main.cpp #include <QCoreApplication> #include "cthread

How to use QThread correctly in pyqt with moveToThread()?

对着背影说爱祢 提交于 2020-01-09 05:00:14
问题 i read this article How To Really, Truly Use QThreads; The Full Explanation, it says instead of subclass qthread, and reimplement run(), one should use moveToThread to push a QObject onto QThread instance using moveToThread(QThread*) here is the c++ example, but i don't know how to convert it to python code. class Worker : public QObject { Q_OBJECT QThread workerThread; public slots: void doWork(const QString &parameter) { // ... emit resultReady(result); } signals: void resultReady(const