问题
我有一个PyQt GUI,用户按下按钮启动后台线程(workerThread从中进行子类化QThread).我想有一个计时器显示(以a的形式QLabel)来显示自workerThread启动以来已经过了多少时间,我希望这个计时器在workerThread退出后立即停止.
可能解决方案
我已经考虑过创建另一个独立的线程(timerThread),它使用a QTimer向一个槽发送一个信号来更新QLabel主GUI线程中的每1秒经过的时间.这timerThread会从收到终止信号后立即退出workerThread.
但是,我不得不开始timerThread在同一时间WorkerThread,我不知道怎么了这一点.
题
有更简单的方法吗?是QTimer甚至开始用正确的方法呢?
我正在研究用Qt 4.6开发的应用程序.
我想创建一个在单独的线程中计数的自定义计时器.但是,我希望这个计时器能够向主线程发送信号.
我将QThread子类化,但它似乎不起作用.
这是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
bool isCounting();
// start the timer with a number of seconds
void startCounting(int value = 300);
void stopCounting();
// the number of seconds to reach
int maximum();
// the current value of the timer
int value();
// elapsed time since the timer has started …Run Code Online (Sandbox Code Playgroud) 我正在使用pyside,但(我认为)是一个通用的Qt问题.
我知道QThread实现调用._exec()方法,所以我们应该在启动的QThread上有一个事件循环.这样我们就可以在那个线程上使用QTimer(我已经完成了这个并且它完美地工作).我的问题是当QWaitCondition也被使用时,我想要一个带有无限循环的"消费者"线程等待在QWaitCondition上通知(来自生产者).我遇到的问题是,使用此设计我不能在消费者线程中使用QTimer.
这是我试图解释的场景的片段:
from PySide import QtGui
from PySide import QtCore
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.button = QtGui.QPushButton(self)
self.button.setText("Periodical")
self.button.clicked.connect(self.periodical_call)
self.thread = QtCore.QThread()
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.loop)
self.thread.start()
def closeEvent(self, x):
self.worker.stop()
self.thread.quit()
self.thread.wait()
def periodical_call(self):
self.worker.do_stuff("main window") # this works
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.do_stuff) # this also works
self.timer.start(2000)
def do_stuff(self):
self.worker.do_stuff("timer main window")
class Worker(QtCore.QObject):
def do_stuff_timer(self):
do_stuff("timer worker")
def do_stuff(self, origin):
self.origin = origin
self.wait.wakeOne()
def stop(self):
self._exit = True
self.wait.wakeAll()
def …Run Code Online (Sandbox Code Playgroud) 我试图通过将一些阻塞代码移动到单独的 QThread 来使我的 PyQt4 程序响应更快。由于它不起作用,我创建了这个最小的示例作为演示:
import sys
import time
from PyQt4 import QtCore, QtGui
class Sleeper(QtCore.QObject):
def __init__(self):
super(Sleeper, self).__init__()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.sleep)
self.timer.start(1000)
def sleep(self):
time.sleep(1)
class MyGUI(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyGUI, self).__init__(parent)
self.button = QtGui.QPushButton("hello", self)
self.sleeper = Sleeper()
self.thread = QtCore.QThread()
self.sleeper.moveToThread(self.thread)
self.thread.start()
if __name__ == "__main__":
qapp = QtGui.QApplication(sys.argv)
my_gui = MyGUI()
my_gui.show()
qapp.exec_()
Run Code Online (Sandbox Code Playgroud)
这段代码的问题是 sleep 命令仍然阻塞用户界面。我发现当我在 Sleeper 类之外创建、连接和运行 QTimer 时,它按预期工作,但我不明白为什么。
我想根据 15 FPS 的帧速率更新 Qtimer - 所以我的 def update(): 每 0,06 秒接收一个信号。你能帮助我吗?我在下面附加了一个代码示例,其中我的 setInterval 输入是 1/15,但我不知道这是否是正确的方法。谢谢。
from PyQt5 import QtCore
def update():
print('hey')
fps = 15
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.setInterval(1/fps)
timer.start()
Run Code Online (Sandbox Code Playgroud) 我有这个 PySide 应用程序,我想每 1 秒运行一次函数 pp,但是当我运行该应用程序时,它只运行了 1 次。
import sys
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6 import QTimer
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
###
self.timer = QTimer()
self.timer.timeout.connect(self.pp())
self.timer.start(1000)
print(self.timer.isActive())
def pp(self):
print("LOL")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
#app.show()
sys.exit(app.exec())
Run Code Online (Sandbox Code Playgroud)
控制台输出:
LOL
True
Run Code Online (Sandbox Code Playgroud)
我搜索了Qt 文档但没有找到任何结果
我正在写一个游戏.通过滴答计时器应该工作这个插槽.
void game_process::animate_cell(MainWindow* m, const std::string& s, double x,double y, size_t i, size_t j, const std::string& step)
{
painter.begin(m);
std::string ss("C:\\Users\\Vardan\\GAmes_lines\\res\\red_" + step + ".png");
ss += s;
const char* p = ss.c_str();
QImage image(p);
RECT temp = cal
culate_cell_rect(i, j);
QRectF target(x, y, image.width(), image.height());
painter.drawImage(target, image);
painter.end();
m->update(x + temp.x0, y + temp.y0, 60, 60);
}
, that's it,
Run Code Online (Sandbox Code Playgroud)
QTimer*timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(render_cell(MainWindow*m,const std :: string&s,double x,double y,size_t i,size_t j,const std :: string&step)) ); timer-> start();
但是你可以看到插槽比信号更多的参数,因此信号和插槽机制不起作用.该怎么办?这里鳕鱼 …
完成后,我更新倒计时计时器,timerEvent(QTimerEvent *e)我调用了 ,killTimer(timerID)但timerEvent()仍在调用中。那么杀死它的正确方法是什么?
编码:
void MainWindow::timerEvent(QTimerEvent *e)
{
Q_UNUSED(e);
static uint16_t u16RemTime = MAX_WARMUP_TIME_IN_SECS;
if((true == isWarmUpStarted) && (u16RemTime > 0))
{
u16RemTime--;
objptrSplashScreen->SetTime(u16RemTime);
}
else
{
//Still running
qWarning("\n\n\n\n\n WARM UP TIMER RUNNING \n\n\n\n\n");
killTimer(warmUpTimerID);
}
}
Run Code Online (Sandbox Code Playgroud)
如果有帮助。我有两个这样的计时器在同一个 GUI 线程中的两个不同类中运行。我将如何去杀死它?
close,destroy 和 deletelater 之间有什么区别。我想在超时后杀死/删除一个对象。
QTimer::singleShot(tim*1000, &qPopup, &QLabel::hide)
Run Code Online (Sandbox Code Playgroud)
这里我使用hide隐藏对象。如果我使用close而不是hide,它会在超时后删除对象吗?摧毁呢?
我正在研究 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)