Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用QThread和QTimer运行methode_C++_Qt - Fatal编程技术网

C++ 使用QThread和QTimer运行methode

C++ 使用QThread和QTimer运行methode,c++,qt,C++,Qt,嗨,我有一个类,它的函数根据QTimer运行(例如,每30毫秒运行一次) 但是我希望我的DoSomthing()函数在单独的线程中运行,这意味着在单独的线程中生成DoSomthing()函数,并使用计时器控制该函数(每隔一段时间在线程中运行我的函数) 我怎么能做到 class TestClass : public QObject { Q_OBJECT public Q_SLOTS: void doSomething(); }; int main(int argv,

嗨,我有一个类,它的函数根据
QTimer
运行(例如,每30毫秒运行一次)

但是我希望我的
DoSomthing()
函数在单独的线程中运行,这意味着在单独的线程中生成
DoSomthing()
函数,并使用计时器控制该函数(每隔一段时间在线程中运行我的函数)

我怎么能做到

class TestClass : public QObject
{
    Q_OBJECT

  public Q_SLOTS:
      void doSomething();
};


int main(int argv, char** args)
{
  QApplication app(argv, args);

  QTimer *timer = new QTimer();

  // Thread
  TestClass test;
  QThread t;
  test.moveToThread(&t);
  t.start();

  // Connections
  QObject::connect(timer, SIGNAL(timeout()), &test, SLOT(doSomething()));

  return app.exec();
}

关于注释,您也可以通过以下方式直接将QThread子类化:

class MyThreadedClass : public QThread
{
  MyThreadClass(){}

  void doSomething(){
      }

  void run()
  {
    doSomething();
  }
};

int main()
{
  MyThreadClass thread();
  thread.start(); // MyThreadClass is now running

  return 0;
}

关于注释,您也可以通过以下方式直接将QThread子类化:

class MyThreadedClass : public QThread
{
  MyThreadClass(){}

  void doSomething(){
      }

  void run()
  {
    doSomething();
  }
};

int main()
{
  MyThreadClass thread();
  thread.start(); // MyThreadClass is now running

  return 0;
}

我建议使用QtConcurrent。直接使用QThread比直接使用QThread省事多了

#include <QtConcurrent/QtConcurrent>

void SomeClass::timerSlot() {
    QtConcurrent::run(this, &SomeClass::SomePrivateMethodToRunInThread);
}

void SomeClass::SomePrivateMethodToRunInThread() {
    doSomething();
    emit someSlot();
}
#包括
void SomeClass::timerSlot(){
QtConcurrent::run(this,&SomeClass::someprivateMethodorUninThread);
}
void SomeClass::somePrivateMethodorUninThread(){
doSomething();
发射someSlot();
}

如果只传递by值(或常量引用)并使用
Qt::AutoConnection
(默认值)或
Qt::QueuedConnection
,则发出信号是线程安全的。我建议使用QtConcurrent。直接使用QThread比直接使用QThread省事多了

#include <QtConcurrent/QtConcurrent>

void SomeClass::timerSlot() {
    QtConcurrent::run(this, &SomeClass::SomePrivateMethodToRunInThread);
}

void SomeClass::SomePrivateMethodToRunInThread() {
    doSomething();
    emit someSlot();
}
#包括
void SomeClass::timerSlot(){
QtConcurrent::run(this,&SomeClass::someprivateMethodorUninThread);
}
void SomeClass::somePrivateMethodorUninThread(){
doSomething();
发射someSlot();
}

如果您只传递一个by值(或常量引用)并使用
Qt::AutoConnection
(默认值)或
Qt::QueuedConnection

连接它,则发出信号是线程安全的。感谢您的回复,但您的类中没有slot函数,有没有可能只在一个单独的线程中运行一个函数?你说的“只放一个函数”是什么意思?在另一个线程中运行一个函数?我的意思是只运行函数而不是所有的类那么你需要qthread吗?可能只是普通POSIX线程或您的平台等效线程等待回复,但您的类中没有slot函数,是否可以在单独的线程中只运行a函数?您所说的“justput a function”是什么意思?在另一个线程中运行一个函数?我的意思是只运行函数而不是所有的类那么你需要qthread吗?可能只是普通POSIX线程或您的等效平台您可以解释什么是timerSlotsome插槽连接到计时器您可以解释什么是timerSlotsome插槽连接到计时器吗