标签: qtimer

如何在QThread中运行计时器?

我想在QThread中运行一个计时器.我写了一些代码,其中我在运行时遇到了一些错误.请引导我走向正确的方向.我究竟做错了什么?

(Parent is QThread(0x1498d10), parent's thread is QThread(0x11272b0), current thread is QThread(0x1498d10)
Run Code Online (Sandbox Code Playgroud)

mainwindow.h //主.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "mythread.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    MyThread *myt;

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp //主.cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    myt=new MyThread();
    myt->start();
    MainWindow w;
}

MainWindow::~MainWindow()
{
    delete ui;
}
Run Code Online (Sandbox Code Playgroud)

mythread.h //线程类

#ifndef MYTHREAD_H …
Run Code Online (Sandbox Code Playgroud)

c++ qt qt-creator qthread qtimer

4
推荐指数
2
解决办法
1万
查看次数

为什么在 QThread(事件循环)之外调用 QTimer::start() 不会失败?

文档

在多线程应用程序中,您可以在任何具有事件循环的线程中使用 QTimer。要从非 GUI 线程启动事件循环,请使用 QThread::exec()。Qt 使用计时器的线程关联来确定哪个线程将发出 timeout() 信号。因此,您必须在其线程中启动和停止计时器;不可能从另一个线程启动计时器。

所以我期望这段代码......

int main(int argc, char *argv[])
{
  QCoreApplication app(argc, argv);
  QTimer timer;
  timer.start(1000);

  app.exec();

}
Run Code Online (Sandbox Code Playgroud)

...失败,因为我调用的主线程start不是QThreadandTimers can only be used with threads started with QThread

问题

为什么这不会失败?

qt multithreading qt5 qtimer

4
推荐指数
1
解决办法
871
查看次数

如何在QT上暂停和重启Qtimer

我有一个Ubuntu和我'与IDE工作QTC++.我将暂停并恢复Qtimer,例如:

void Ordonnancer_les_taches::on_pushButton_clicked()
{

    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(l_odonnancement()));
    dataTimer.start(5000);
}
Run Code Online (Sandbox Code Playgroud)

怎么Pause样怎么样Restart?给我一个例子

c++ qt signals-slots qtimer

3
推荐指数
1
解决办法
7501
查看次数

删除对象后触发QTimer :: SingleShot

// Example class
class A : public QObject
{
   Q_OBJECT
   void fun() {
       Timer::SingleShot(10, timerSlot); //rough code
   }
   public slot:
   void timerSlot();
}

auto a = SharedPointer<A>(new A);
a->fun();
a->reset(); // a deleted
Run Code Online (Sandbox Code Playgroud)

在这种情况下删除a并触发计时器后,它会执行timerSlot()吗?我得到了一次非常罕见的崩溃,并且不确定是不是因为这个逻辑中有些可疑.

c++ qt qeventloop qt5 qtimer

3
推荐指数
1
解决办法
1484
查看次数

PyQt5 QTimer 计数直到特定秒

我正在用 python 创建一个程序,我正在使用 pyqt。我目前正在使用 QTimer,我想每秒钟打印一次“计时器工作”并在 5 秒后停止打印。这是我的代码:

timers = []
def thread_func():
    print("Thread works")
    timer = QtCore.QTimer()
    timer.timeout.connect(timer_func)
    timer.start(1000)
    print(timer.remainingTime())
    print(timer.isActive())
    timers.append(timer)

def timer_func():
    print("Timer works")
Run Code Online (Sandbox Code Playgroud)

python pyqt qtimer pyqt5

3
推荐指数
1
解决办法
1万
查看次数

PyQt5:线程中的计时器

问题描述

我正在尝试制作一个应用程序来收集数据、处理数据、显示数据和一些驱动(打开/关闭阀门等)。作为对我有一些更严格时间限制的未来应用程序的实践,我想在单独的线程中运行数据捕获和处理。

我当前的问题是它告诉我我无法从另一个线程启动计时器。

当前代码进度

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 pyqt qthread qtimer pyqt5

3
推荐指数
1
解决办法
6601
查看次数

使用 std 或 boost 库的 C++ 中的 Qtimer 相当于什么?

我必须每 5 秒执行一次任务,直到程序退出。我不想在这里使用线程。

在 QT 我可以这样做

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
Run Code Online (Sandbox Code Playgroud)

但是我如何使用 std 或 boost 库在 C++ 中做到这一点?

谢谢

c++ boost timer std qtimer

3
推荐指数
1
解决办法
1558
查看次数

Qthread 上的 QTimer

我有一个 GUI,我需要使用 Qtimer 不断更新,因为我使用工作 Qthread,这是我的代码:

from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
from PyQt5.QtCore import QThread, QTimer
import sys
import threading


class WorkerThread(QThread):
    def run(self):
        print("thread started from :" + str(threading.get_ident()))
        timer = QTimer(self)
        timer.timeout.connect(self.work)
        timer.start(5000)
        self.exec_()

    def work(self):
        print("working from :" + str(threading.get_ident()))
        QThread.sleep(5)


class MyGui(QWidget):

    worker = WorkerThread()

    def __init__(self):
        super().__init__()
        self.initUi()
        print("Starting worker from :" + str(threading.get_ident()))
        self.worker.start()

    def initUi(self):
        self.setGeometry(500, 500, 300, 300)
        self.pb = QPushButton("Button", self)
        self.pb.move(50, 50)
        self.show()


app = QApplication(sys.argv)
gui = MyGui()
app.exec_() …
Run Code Online (Sandbox Code Playgroud)

python pyqt qthread qtimer pyqt5

3
推荐指数
1
解决办法
6916
查看次数

QThread 与 QTimer 连接问题

这有什么问题吗?它给了我奇怪的编译错误:

候选函数不可行:第二个参数没有从“void (QThread::*)(QThread::QPrivateSignal)”到“const char *”的已知转换

QTimer timer;
timer.setInterval(3000);
connect(&timer, &QTimer::timeout, this, &ThisClass::runConnectionCheck);
QThread connectionThread;
timer.moveToThread(&connectionThread);
connect(&connectionThread, &QThread::started, &timer, &QTimer::start);
connectionThread.start();
Run Code Online (Sandbox Code Playgroud)

c++ qt qthread qt5 qtimer

3
推荐指数
1
解决办法
775
查看次数

计时器是从另一个线程启动的吗?

QThread文档建议了两种使代码在单独的线程中运行的方法。如果我子类化 QThread 并重新实现 run(),那么我得到

QBasicTimer::start: Timers cannot be started from another thread  
Run Code Online (Sandbox Code Playgroud)

-

#include <QWidget>
#include <QThread>
#include <QBasicTimer>
#include <QDebug>
#include <QEvent>
#include <QCoreApplication>

class Worker : public QThread
{
    Q_OBJECT
    int id;
    bool m_abort = false;
    bool compute = false;
public:
    Worker() {}

protected:
    void timerEvent(QTimerEvent *event) override {
        if (event->timerId() == id) {
            compute = true;
        } else {
            QObject::timerEvent(event);
        }
    }
public slots:
    void abort() {m_abort = true;}
    void run() {
        qDebug() << QThread::currentThreadId();
        QBasicTimer …
Run Code Online (Sandbox Code Playgroud)

c++ qt multithreading qthread qtimer

3
推荐指数
1
解决办法
1158
查看次数

标签 统计

qtimer ×10

c++ ×6

qt ×6

qthread ×5

pyqt ×3

pyqt5 ×3

python ×3

qt5 ×3

multithreading ×2

boost ×1

qeventloop ×1

qt-creator ×1

signals-slots ×1

std ×1

timer ×1