我无法从 QThread 获取(打印)错误信息。我有一个工人(QThread):
class Worker(QThread):
def __init__(self, parent=None):
QThread.__init__(self, parent)
def run(self):
# do some heavy work here
print 1 # 1 is printed in my OutputWidget
print a # a is not defined and it should print an error
Run Code Online (Sandbox Code Playgroud)
print a如果我只运行新文件中的最后一行 ( )(a其中未定义),则会收到错误(这是正确的且符合预期):
Traceback (most recent call last):
File "C:\...\output_widget\output_from_qthread_V2.py", line 1, in run
print a
NameError: global name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
输出小部件:
import sys
from PyQt4 import QtGui
from PyQt4 import …Run Code Online (Sandbox Code Playgroud) 我正在研究 Qt 应用程序。在那里我使用了两个线程,一个用于 GUI,另一个用于处理。
我有一个工人类,它有 QTimer 作为成员类。
.h 文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <QThread>
class Worker : public QObject
{
Q_OBJECT
public:
Worker();
QTimer t;
public slots:
void process();
void startWorker();
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QThread workerThread;
Worker wt;
};
#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)
.cpp文件
#include "mainwindow.h"
#include <QDebug>
#include <iostream>
Worker::Worker() : t(this)
{
connect(&t, SIGNAL(timeout()), this, …Run Code Online (Sandbox Code Playgroud) 我有一个用 Qt 实现的应用程序,其中包含一些 c++ 和 python 集成模块(机器学习、信号处理等),具有以下基本特征:
QThread)QwtPlotZoomer),例如, QwtPlotMarker, QGraphicsRectItem, QwtPlotCurve。我的目标是通过具有多个用户观察者的Web 浏览器应用程序提供运行应用程序的服务器已实现的应用程序特征。我做了一些研究,并在以下链接(link、link、link和link )上发现了一些可能的解决方案(Qt WebGL、Qt for Assembly、 Wt),但作为一个没有经验的 Web 应用程序开发人员,我不完全确定其中哪一个或使用的替代和优选模块。C++
您能否根据成熟度、功能、易用性和成熟度(Qt 小部件的灵活性)等特征提供一些关于女巫 Web 开发库使用的建议和指导,同时也考虑到我的应用程序的特征?
先感谢您。
PS:我想提一下这是否有帮助,因为我在 C++ 和 python 方面经验丰富,而不是在 Java 和 Javascript 方面经验丰富。
如何在弹出窗口中实现一个进度条,该窗口通过QThread监视来自所谓的 Worker 类(即时间/CPU 消耗任务)的运行函数的进度?
我已经检查了无数示例和教程,但进度条显示在弹出窗口中的事实似乎使一切变得更加困难。我相信我想要的是一件相当简单的事情,但我一直在失败,而且我的想法也用完了。
我有一个我想要实现的例子,它基于这个答案:
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QProgressBar, QVBoxLayout
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Widget")
self.h_box = QHBoxLayout(self)
self.main_window_button = QPushButton("Start")
self.main_window_button.clicked.connect(PopUpProgressB)
self.h_box.addWidget(self.main_window_button)
self.setLayout(self.h_box)
self.show()
class Worker(QObject):
finished = pyqtSignal()
intReady = pyqtSignal(int)
@pyqtSlot()
def proc_counter(self): # A slot takes no params
for i in range(1, 100):
time.sleep(1)
self.intReady.emit(i)
self.finished.emit()
class PopUpProgressB(QWidget):
def …Run Code Online (Sandbox Code Playgroud) 我有两个类 - 一个在主线程中运行并执行GUI操作,另一个执行一些计算并发出网络请求.
// A member of the class that runs in the main thread
QThread thread;
Run Code Online (Sandbox Code Playgroud)
这是在主线程中运行的类的初始化方法的片段:
// Create the class that runs in the other thread and move it there
CServerThread * server = new CServerThread;
server->moveToThread(&thread);
// When the thread terminates, we want the object destroyed
connect(&thread, SIGNAL(finished()), server, SLOT(deleteLater()));
thread.start();
Run Code Online (Sandbox Code Playgroud)
在主线程中运行的类的析构函数中:
if(thread.isRunning())
{
thread.quit();
thread.wait();
}
Run Code Online (Sandbox Code Playgroud)
我期望发生的是线程终止并销毁CServerThread类的实例.但是,CServerThread不会调用该类的析构函数.
这是QThread的子类的run方法:
void crono::Controller::run() {
//initialise timer
int *i = & this->model->seconds_elapsed;
for (*i = 0; *i < this->model->seconds_total; (*i)++) {
//calculate current seconds/minutes/hours elapsed starting from seconds_elapsed (*i)
this->model->tick();
//should display in the form the time elapsed
this->vista->showTime();
sleep(1000);
}
Beep(1000, 500); //beep when all is over
}
Run Code Online (Sandbox Code Playgroud)
控制器更新模型值.
QT表单在启动时打开,我猜在主应用程序线程中.
问题是对debug*i = 0和seconds_total = X> 0的鄙视,循环只执行一次,在第一次调试停止(它没有结束)之后,表单弹出但没有任何反应.
我唯一可以猜到的是,Controller Thread失去了优先级,再也没有获得cpu.
怎么能避免这个?
编辑 我正在尝试使用QTimer,运气不好.
我将更新声明为公共插槽,然后实现如下:
void crono::Controller::update() {
this->modello->tick();
this->vista->showTime();
//eventually stop at some point (pointer to timer and timer->stop()?
//...
//Beep(1000, 500); …Run Code Online (Sandbox Code Playgroud) 我以为我已经阅读了有关 Qt 中线程的所有内容,但显然我弄错了。我已经被这些东西困扰了一段时间了,所以我非常感谢你的帮助。
因此,我创建了一个在其 __init__ 方法中启动线程的类:
class MyClass(object):
def __init__(self):
(...)
self.thread = QtCore.QThread(parent)
worker = Worker()
QtCore.QObject.connect(self.thread, QtCore.SIGNAL('started()'), worker,
QtCore.SLOT('doWork()'))
QtCore.QObject.connect(worker, QtCore.SIGNAL('finished()'), self.thread,
QtCore.SLOT('quit()'))
QtCore.QObject.connect(worker, QtCore.SIGNAL('finished()'), worker,
QtCore.SLOT('deleteLater()'))
QtCore.QObject.connect(self.thread, QtCore.SIGNAL('finished()'),
self.thread, QtCore.SLOT('deleteLater()'))
worker.moveToThread(self.thread)
self.thread.start()
Run Code Online (Sandbox Code Playgroud)
工人阶级看起来像这样:
class Worker(QtCore.QObject):
(some signals)
def doWork(self):
print "doing my work"
Run Code Online (Sandbox Code Playgroud)
问题是,尽管线程正在运行,但我的 doWork 插槽从未执行。
创建 MyClass 实例后,假设: obj = MyClass()
我可以调用: obj.thread.isRunning()
返回 True。
我假设信号“started”没有发出,因为立即退出创建线程的方法(当我在启动线程后添加睡眠时,它进入 doWork 槽)。我想知道这样的情况应该如何妥善处理。
如果我没有清楚地解释我的问题,请告诉我。提前感谢大家的帮助。
当我的应用程序被销毁时,我想终止(完成)QThread.因此,我调用terminate()和quit()内destructor从'我的QThread的派生类.安全吗?
class Session : public QThread
{
Q_OBJECT
private:
CheckerAdapter *inst;
public:
explicit Session(CheckerAdapter *inst, QObject *parent = 0);
void run();
~Session(){
terminate();
quit();
}
};
Run Code Online (Sandbox Code Playgroud) 答案代码位于此处(/sf/answers/3538533001/):
https://github.com/eyllanesc/stackoverflow/tree/master/50550089
答案比下面的代码更简单 - 上面的代码使用 QPropertyAnimation 而不是像下面这样使用带有 QThread 的 for 循环 - 这节省了代码中的大量空间并且效率更高。
原问题如下:
我正在用 Qt 编写应用程序,但在关闭应用程序和线程时遇到问题。
基本上,应用程序窗口会关闭,但该进程仍保留在后台并且永远不会关闭。
我的主要标题(因为有很多包含,所以只包含了这个类):
class ChatUI : public QWidget
{
Q_OBJECT
public:
explicit ChatUI(QWidget *parent = 0);
~ChatUI();
private:
// The UI itself
Ui::ChatUI * ui;
// Current appliation startup directory
QString applicationStartupDir = QDir::currentPath() + "/";
// Typing indicator stuff
QFrame * typingIndicator = nullptr;
QImage circleImage;
ThreadController * typingIndicatorThread = new ThreadController(false);
bool currentlyFadingTypingIndicator = false;
// The calm before the …Run Code Online (Sandbox Code Playgroud) 我需要在QSpinBox中显示CPU具有的核心数或线程数.问题是:
QThread cpuInfo(this); //get CPU info
ui->spnBx_nmb_nodes->setValue(cpuInfo.idealThreadCount()); //get thread count
Run Code Online (Sandbox Code Playgroud)
这总是返回"2".我试过一个"2核/ 4线程"笔记本; "4核/ 8线程"计算机和"12核/ 24线程"服务器.在所有情况下,这将返回"2"作为理想的线程数.
有人可以,给我一些启示吗?
我需要在生成文件时获取文件大小.
我已经尝试过使用QThread和QFileInfo::size一个while带有标志的标志,该标志表示完成的文件,但值永远不会改变.
所以想知道你是否可以在生成文件时获取文件大小.