class UserEvent : public QCustomEvent   //用户自定义的事件类 { public:  UserEvent(QString s) : QCustomEvent(346798), sz(s) { ; }  QString str() const { return sz; } private:  QString sz;     }; 
  UserThread类是由QThread类继承而来的子类,在该类中除了定义有关的变量和线程控制函数外,最主要的是定义线程的启动函数UserThread::run(),在该函数中创建了一个用户自定义事件UserEvent,并利用QThread类的postEvent函数提交该事件给相应的接收对象。 
class UserThread : public QThread      //用户定义的线程类 { public:  UserThread(QObject *r, QMutex *m, QWaitCondition *c);  QObject *receiver; }    void UserThread::run()     //线程类启动函数,在该函数中创建了一个用户自定义事件 {  UserEvent *re = new UserEvent(resultstring);  QThread::postEvent(receiver, re);  } 
  UserWidget类是用户定义的用于接收自定义事件的QWidget类的子类,该类利用slotGo()函数创建了一个新的线程recv(UserThread类),当收到相应的自定义事件(即id为346798)时,利用customEvent函数对事件进行处理。 
void UserWidget::slotGo()    //用户定义控件的成员函数 {   mutex.lock();        if (! recv)   recv = new UserThread(this, &mutex, &condition);      recv->start();  mutex.unlock(); }    void UserWidget::customEvent(QCustomEvent *e)   //用户自定义事件处理函数 {   if (e->type()==346798)   {   UserEvent *re = (UserEvent *) e;   newstring = re->str();  } } 
  在这个例子中,UserWidget对象中创建了新的线程UserThread,用户可以利用这个线程实现一些周期性的处理(如接收底层发来的消息等),一旦满足特定条件就提交一个用户自定义的事件,当UserWidget对象收到该事件时,可以按需求做出相应的处理,而一般情况下,UserWidget对象可以正常地执行某些例行处理,而完全不受底层消息的影响。 
		    
                       
		      
		      
		   |