好的,所以我在QTimer中完全迷失了.问题是:我有多线程应用程序,我需要在QTimer的超时上做一些工作.我这样做了:
QTimer* timer = new QTimer();
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), someObject, SLOT(work()));
Run Code Online (Sandbox Code Playgroud)
这没用.有时,work()根本没有被调用,有时它在我关闭程序时被调用,有时候看起来都很正常.
所以我开始想到计时器需要线程.提供MCV示例:
class Tester : public QObject
{
Q_OBJECT
public:
Tester(QObject* par = 0) : QObject(par)
{
}
public slots:
void greet()
{
qDebug()<<"hello";
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer* timer1 = new QTimer();
QThread* thread = new QThread();
Tester* tester = new Tester();
timer1->setInterval(500);
timer1->setSingleShot(false);
timer1->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), timer1, SLOT(start()));
QObject::connect(timer1, SIGNAL(timeout()), tester, SLOT(greet()));
QObject::connect(timer1, SIGNAL(timeout()), timer1, SLOT(deleteLater()));
QObject::connect(timer1, SIGNAL(destroyed()), thread, SLOT(quit())); …Run Code Online (Sandbox Code Playgroud) 使用计时器我checkBookings()反复调用插槽.我能够编译并运行程序,但在执行上述FOR循环时崩溃了.
错误:"在QList :: at中ASSERT失败:"索引超出范围",文件../../../../Qt/2010.05/qt/include/QtCore/../../src/corelib /tools/qlist.h,第455行传递给C运行时函数的参数无效.传递给C运行时函数的参数无效."
我的代码是:
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(checkBookings()));
timer->start(500000);
void Canvas::checkBookings()
{
QString dateStr;
for(int i= 0;i<=qlist.count();i++)
{
dateStr = qList.at(i).at(6);
}
}
Run Code Online (Sandbox Code Playgroud) 假设这timer是一个对象QTimer,定时器的间隔是iInterval和定时器的超时信号连接到一个插槽sltTimeout().
我只是在想如果iInterval小于sltTimeout()运行时间会发生什么.结果会运行多个线程sltTimeout()吗?如果是这样,似乎可能导致对对象的非同步访问出现问题.
任何人都可以澄清一下吗?
简而言之:这段代码位于main函数中,这很有效.此代码在两个图形元素上执行交换. 
QTimer timer;
timer.setTimerType(Qt::PreciseTimer);
timer.setInterval(1000.0/30.0);
timer.setSingleShot(false);
// ====================== MOVE 4 in POS of 1 ==========================
QPointF centre(QLineF(button1->pos(), button4->pos()).pointAt(0.5));
QPointF positionBut4 = button4->pos();
MyPointF centreSwap4(0, &positionBut4, ¢re);
button4->myPointF = ¢reSwap4;
QObject::connect(&timer, SIGNAL(timeout()), ¢reSwap4, SLOT(updateDownRight()));
QObject::connect(¢reSwap4, SIGNAL(positionChanged()), button4, SLOT(slotMoveCircle()));
// ====================== MOVE 4 in POS of 1 ==========================
// ====================== MOVE 1 in POS of 4 ==========================
QPointF positionBut1 = button1->pos();
MyPointF centreSwap1(0, &positionBut1, ¢re);
button1->myPointF = ¢reSwap1;
QObject::connect(&timer, SIGNAL(timeout()), ¢reSwap1, SLOT(updateUpLeft()));
QObject::connect(¢reSwap1, SIGNAL(positionChanged()), button1, SLOT(slotMoveCircle()));
// ====================== MOVE 1 in POS …Run Code Online (Sandbox Code Playgroud) 我正在尝试将“某些文本”打印到 QTextBrowser,连续“n”次。其中“n”是整数。为此,我使用 QTimer::SingleShot 进行计时。一旦超时被触发,一个 FLAG 被设置为 false 并且这个“FLAG”在 while 循环中被监视以在 FLAG 为 false 时中断,并且它应该插入文本直到 FLAG 被设置为 FALSE。FLAG 的初始值为真。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
FLAG = true;
}
void MainWindow::on_pushButton_clicked()
{
ui->pushButton->setEnabled(false);
RunTheTimer();
int counter = 0;
do
{
ui->textBrowser->insertPlainText(QString("Inside While loop %1 \n").arg(counter++));
counter++;
}while(FLAG);
FLAG = true;
}
void MainWindow::RunTheTimer()
{
ui->textBrowser-> insertPlainText("Timer Started");
QTimer::singleShot(60000, this, SLOT(Update()));// One Minute
}
void MainWindow::Update()
{
ui->textBrowser-> insertPlainText("Timeout signal triggered");
ui->pushButton->setEnabled(true);
FLAG = …Run Code Online (Sandbox Code Playgroud) 我试图QTimer在我的窗口小部件上使用一个简单的对象,以便我可以计算一个方法完成所用的时间.但令我惊讶的是,计时器被阻止,直到该方法完成执行!即当有问题的方法结束时,计时器开始滴答作响!
这是一个示例代码,用于演示我写的内容:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btnTest_clicked();
void OnTimerTick();
private:
Ui::MainWindow *ui;
ulong seconds;
};
#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)
这是cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
#include <QTimer>
#include <QtCore>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnTest_clicked()
{
QTimer * timer …Run Code Online (Sandbox Code Playgroud)