天天看点

4.QT中进程操作,线程操作



qt中的线程操作

t19process.pro

sources

+=

\

main.cpp

config

c++11

多线程(可以通过movetothread(qthread *)的方法指定给指定的线程)

新建项目t20thread,项目代码如下:

t20thread.pro

headers

worker.h

mythread.h

worker.cpp

mythread.cpp

#ifndef

worker_h

#define

#include

<qobject>

<qthread> 

//要开启线程的时候需要使用头文件<qthread>

<qdebug>

class

worker

:

public

qobject

{

q_object

public:

explicit

worker(qobject

*parent

= 0);

qthread

_thread;

bool

event(qevent

*ev)

//通过qthread::currentthread()可以获得当前线程信息

qdebug()

<<

"event:"

qthread::currentthread();

return

qobject::event(ev);

}

signals:

slots:

void

dowork()

};

#endif

//

"worker.h"

worker::worker(qobject

*parent)

qobject(parent)

//this->movetothread(&_thread);

_thread.start();

connect(&_thread,

signal(finished()),

this,

slot(deletelater()));

mythread_h

<qthread>

mythread

mythread(qobject

foo()

run()

foo();

"thread

is

run";

"mythread.h"

mythread::mythread(qobject

qthread(parent)

<qcoreapplication>

<qtimer>

int

main(int

argc,

char*

argv[])

qcoreapplication

app(argc,

argv);

#if

thread;

thread.start();

thread.foo();

"main

thread

is"<<qthread::currentthread();

worker*

=

new

worker();

qtimer*

timer

qtimer;

//worker->movetothread(&thread);

qobject::connect(timer,

signal(timeout()),

worker,

slot(dowork()));

timer->setinterval(1000);

timer->start();

app.exec();

运行结果:

4.QT中进程操作,线程操作

继续阅读