我遇到了一些我似乎无法解决的问题。这可能是一些愚蠢的错误。在方法触发信号后,我有一个类在另一个QThread 5000毫秒内运行另一个类的方法。至少这是想法,但这种情况不会发生。
#ifndef MOVEME_MPOINTER_HPP
#define MOVEME_MPOINTER_HPP
#include <Windows.h>
#include <WindowsX.h>
#include <QSystemTrayIcon>
#include <QDesktopWidget>
#include <QApplication>
#include <QMainWindow>
#include <QMessageBox>
#include <QByteArray>
#include <QAction>
#include <QCursor>
#include <QThread>
#include <QTimer>
#include <QDebug>
#include <QMenu>
#include <QIcon>
#include <QRect>
#include "worker.hpp"
namespace Ui {
class MoveMe_MPointer;
}
class MoveMe_MPointer : public QMainWindow
{
Q_OBJECT
public:
explicit MoveMe_MPointer(QWidget *parent = 0);
~MoveMe_MPointer();
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result);
public slots:
void resetTimer();
void closeApplication();
private:
QSharedPointer<QIcon> icon;
QSharedPointer<QSystemTrayIcon> systemtray;
QSharedPointer<QThread> workerThread;
QSharedPointer<Worker> workerObject;
QSharedPointer<QTimer> workerTimer;
QSharedPointer<QCursor> cursor;
};
#endif // MOVEME_MPOINTER_HPP
相关方法的实施如下:
MoveMe_MPointer::MoveMe_MPointer(QWidget *parent) : QMainWindow(parent) {
icon = QSharedPointer<QIcon>(new QIcon("://appicon"));
systemtray = QSharedPointer<QSystemTrayIcon>(new QSystemTrayIcon);
// Create cursor
cursor = QSharedPointer<QCursor>(new QCursor(Qt::ForbiddenCursor));
systemtray->setIcon(*icon.data());
systemtray->setVisible(true);
workerThread = QSharedPointer<QThread>(new QThread());
workerObject = QSharedPointer<Worker> (new Worker ());
workerObject->setX(100);
workerObject->setY(100);
workerObject->moveToThread(workerThread.data());
workerTimer = QSharedPointer<QTimer>(new QTimer());
QObject::connect(workerThread.data(), SIGNAL(started()), workerObject.data(), SLOT(Work()));
QObject::connect(workerTimer.data(), SIGNAL(timeout()), workerObject.data(), SLOT(Work()));
QObject::connect(workerObject.data(), SIGNAL(Done()), this, SLOT(resetTimer()));
RegisterHotKey((HWND) winId(), 0, MOD_CONTROL, 0x51);
RegisterHotKey((HWND) winId(), 1, MOD_CONTROL | MOD_SHIFT, 0x4D);
}
void MoveMe_MPointer::resetTimer() {
qDebug() << "START";
workerTimer->start(5000);
qDebug() << "STOP";
}
基础课程:
#ifndef WORKER_HPP
#define WORKER_HPP
#include <Windows.h>
#include <QSharedPointer>
#include <QMouseEvent>
#include <QObject>
#include <QCursor>
#include <QThread>
#include <QDebug>
class Worker : public QObject
{
Q_OBJECT
int x;
int y;
bool stop;
public:
explicit Worker(QObject *parent = 0);
void setX(const int x);
void setY(const int y);
void Stop();
signals:
void Done();
public slots:
void Work();
private:
void doWork();
};
#endif // WORKER_HPP
实施如下:
#include "worker.hpp"
Worker::Worker(QObject *parent) : QObject(parent) {
stop = false;
}
void Worker::setX(const int x) {
this->x = x;
}
void Worker::setY(const int y) {
this->y = y;
}
void Worker::Stop() {
stop = true;
}
void Worker::Work() {
doWork();
}
void Worker::doWork() {
int _x = 50;
INPUT input;
ZeroMemory(&input, sizeof(input));
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dwFlags = MOUSEEVENTF_MOVE;
for (int dx = 0; dx < _x && !stop; ++dx) {
input.mi.dx = +1;
input.mi.dy = +1;
SendInput(1, &input, sizeof(input));
QThread::msleep(10);
}
QThread::msleep(100);
for (int dx = 0; dx < _x && !stop; ++dx) {
input.mi.dx = -1;
input.mi.dy = -1;
SendInput(1, &input, sizeof(input));
QThread::msleep(10);
}
QThread::msleep(100);
emit Done();
}
每当我的应用程序收到完成信号时,我的resetTimer插槽就会被执行,但有趣的部分是。它会立即超时,甚至不会等待一秒钟。我错过了什么?