QtConcurrent::run 与虚拟 class 成员
QtConcurrent::run with a virtual class member
所以我正在尝试封装一个计时器 class,它将处理多线程和计时器的所有细节。
这是我的代码:
TimedEvent.h
class TimedEvent : public QObject
{
Q_OBJECT
public:
explicit TimedEvent(QObject *parent = 0);
TimedEvent(const int intervalInMsecs);
virtual void TimeoutWorkProcedure() = 0;
private slots:
void TimeoutWorkThread();
protected:
QTimer *myTimer;
};
TimedEvent.cpp
TimedEvent::TimedEvent(QObject *parent) :
QObject(parent)
{
}
TimedEvent::TimedEvent(const int intervalInMsecs)
{
// Create timer
//
myTimer = new QTimer(this);
// Connect the timeout signal to our virtual callback function
//
connect(myTimer, SIGNAL(timeout()), this, SLOT(TimeoutWorkThread()));
myTimer->start(intervalInMsecs);
}
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure());
}
我的想法是 TimedEvent 是一个基础 class,我可以很容易地创建派生的 classes。
class MyClass : public TimedEvent
{
public:
MyClass( const int timeoutInMsecs );
TimeoutWorkProcedure(){ do some background stuff };
};
问题是我不知道要将什么传递给 QtConcurrent::run 调用。不确定这是否可能。我可以将 QTConcurrent::run 调用移动到派生的 class,但我预计会有几个这样的派生 classes。
如有任何想法,我们将不胜感激。
K.
此代码:
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure);
}
非常好,会如您所愿。它将调用 TimeoutWorkProcedure
.
的覆盖版本
所以我正在尝试封装一个计时器 class,它将处理多线程和计时器的所有细节。
这是我的代码: TimedEvent.h
class TimedEvent : public QObject
{
Q_OBJECT
public:
explicit TimedEvent(QObject *parent = 0);
TimedEvent(const int intervalInMsecs);
virtual void TimeoutWorkProcedure() = 0;
private slots:
void TimeoutWorkThread();
protected:
QTimer *myTimer;
};
TimedEvent.cpp
TimedEvent::TimedEvent(QObject *parent) :
QObject(parent)
{
}
TimedEvent::TimedEvent(const int intervalInMsecs)
{
// Create timer
//
myTimer = new QTimer(this);
// Connect the timeout signal to our virtual callback function
//
connect(myTimer, SIGNAL(timeout()), this, SLOT(TimeoutWorkThread()));
myTimer->start(intervalInMsecs);
}
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure());
}
我的想法是 TimedEvent 是一个基础 class,我可以很容易地创建派生的 classes。
class MyClass : public TimedEvent
{
public:
MyClass( const int timeoutInMsecs );
TimeoutWorkProcedure(){ do some background stuff };
};
问题是我不知道要将什么传递给 QtConcurrent::run 调用。不确定这是否可能。我可以将 QTConcurrent::run 调用移动到派生的 class,但我预计会有几个这样的派生 classes。
如有任何想法,我们将不胜感激。
K.
此代码:
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure);
}
非常好,会如您所愿。它将调用 TimeoutWorkProcedure
.