• 9084阅读
  • 7回复

【提问】如何 在一个线程中emit 一个信号到主程序接收 [复制链接]

上一主题 下一主题
离线wjydlut
 

只看楼主 倒序阅读 楼主  发表于: 2006-02-25
我的程序代码如下,那位帮忙看看错在哪里.先谢了!
#ifndef THR1_H
#define THR1_H
#include <qthread.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qapplication.h>
#include <qtimer.h>
#include <qdatetime.h>
#include <qsignal.h>
#include <qobject.h>

class MyThreadA : public QThread
{

Q_OBJECT

  public:
    MyThreadA(QLabel*);
    //~MyThreadA();
    virtual void run();
    QLabel *label;
    //void emitSignal() ;
  signals:
    void Signal(const QString &) ;
  private:
      QTime tim ;
      QTimer *timer ;
      int str ;
  };

#endif

#include <qlabel.h>
#include <qvariant.h>
//#include "temp.moc"
#include "thr1.h"
int i =0 ;

MyThreadA::MyThreadA(QLabel* lab) :label(lab)
{

}
void MyThreadA::run()
{
  while(1)
  {
  sleep(2);
  tim = QTime::currentTime();
  str=tim.second();  
  char str1[10]={ } ;
  sprintf(str1, "%d", str);
 
  if(str<3)
  {
    emit Signal("开" ) ;
    // emitSignal( ) ;
    qDebug( "mmmm") ;
    }
  if(str>57)
  {
    emit Signal("关") ;
    //emitSignal() ;
    qDebug( "cccccc");
    }
 
  label->setText(str1);  
 
  }
  }
connect(labelF3,SIGNAL(Signal(const QString &)),this,SLOT(FFF(const QString &)));
[ 此贴被XChinux在2006-02-25 14:37重新编辑 ]
离线wjydlut

只看该作者 1楼 发表于: 2006-02-25
编译错误如下:
.moc/moc_thr1.cpp: In static member function `static QMetaObject*
  MyThreadA::staticMetaObject()':
.moc/moc_thr1.cpp:51: no method `QThread::staticMetaObject'
.moc/moc_thr1.cpp: In member function `virtual void* MyThreadA::qt_cast(const
  char*)':
.moc/moc_thr1.cpp:76: no method `QThread::qt_cast'
.moc/moc_thr1.cpp: In member function `void MyThreadA::Signal(const QString&)':
.moc/moc_thr1.cpp:82: `activate_signal' undeclared (first use this function)
.moc/moc_thr1.cpp:82: (Each undeclared identifier is reported only once for
  each function it appears in.)
.moc/moc_thr1.cpp: In member function `virtual bool MyThreadA::qt_invoke(int,
  QUObject*)':
.moc/moc_thr1.cpp:87: no method `QThread::qt_invoke'
.moc/moc_thr1.cpp: In member function `virtual bool MyThreadA::qt_emit(int,
  QUObject*)':
.moc/moc_thr1.cpp:95: no method `QThread::qt_emit'
.moc/moc_thr1.cpp: In member function `virtual bool MyThreadA::qt_property(int,
  int, QVariant*)':
.moc/moc_thr1.cpp:103: no method `QThread::qt_property'
make: *** [.obj/moc_thr1.o] Error 1
离线goldroc

只看该作者 2楼 发表于: 2006-02-26
首先,有自定义的signal 和slot 的类必须moc
然后,signal的触发不能够跨线程,如果想跨线程,必须用PostEvent (QApplication)。
离线nanhu_007

只看该作者 3楼 发表于: 2006-02-26
或者用QCop机制
离线wjydlut

只看该作者 4楼 发表于: 2006-02-28
利用postEvent还是出错 , 那位知道错 在 哪里给指点一下.
class Transaction
{
public:
  Transaction(QString ss);

  QString messageStr();
private :
  QString string ;

};

class Userevent : public QCustomEvent 
 {
  public :
    Userevent( );
     QString message ;
  };

class MyThreadA : public QThread
{
  public:
     MyThreadA(QLabel*);
     void setTargetWidget(QWidget *widget);
    virtual void run();
     QLabel *label;
  private:
        QTime tim ;
        QTimer *timer ;
        int str ;
        QWidget *targetWidget;
  };

#include <qlabel.h>
#include <qapplication.h>
#include "thr1.h"
Transaction::Transaction(QString ss)
{
  string =ss;
}
QString Transaction::messageStr()
{
  return QObject::tr("Ccc");
  //return sz ;
}

Userevent::Userevent() :QCustomEvent(346798)
{

}

MyThreadA::MyThreadA(QLabel* lab) :label(lab)
{

}
void MyThreadA::setTargetWidget(QWidget *widget)
{
  targetWidget = widget;
}
void MyThreadA::run()
{
// MainWindow xx ;
  //sz="AAA" ;
  Transaction *transact;
  Userevent *re = new Userevent ;
  //UserEvent *re = new UserEvent("开");
  while(1)
  {
  sleep(2);
  tim = QTime::currentTime();
  str=tim.second();  
  char str1[10]={ } ;
  sprintf(str1, "%d", str);
 
  if(str<3)
  {
    re->message = transact->messageStr();
    QApplication::postEvent(targetWidget, re);
    qDebug( "mmmm") ;
    }
  if(str>57)
  {
    re->message = transact->messageStr();
     QApplication::postEvent(targetWidget, re);
    qDebug( "cccccc");
    }
 
  label->setText(str1);  
 
  }
  }
void MainWindow::customEvent(QCustomEvent *e)  //用户自定义事件处理函数
  { if ((int)e->type()==346798)
   {
   UserEvent *re = (UserEvent *) e;
    labelF3->setText(re->message) ;
    FFF();
    }
  }
离线wjydlut

只看该作者 5楼 发表于: 2006-02-28
出错如下 :
In file included from thr1.cpp:5:
thr1.h:23: stray '\241' in program
thr1.h:23: stray '\241' in program
thr1.h:24: stray '\241' in program
thr1.h:24: stray '\241' in program
thr1.h:25: stray '\241' in program
thr1.h:25: stray '\241' in program
thr1.h:25: stray '\241' in program
thr1.h:25: stray '\241' in program
thr1.h:27: stray '\241' in program
thr1.h:27: stray '\241' in program
thr1.h:27: stray '\241' in program
thr1.h:27: stray '\241' in program
thr1.h:28: stray '\241' in program
thr1.h:28: stray '\241' in program
thr1.h:28: stray '\241' in program
thr1.h:28: stray '\241' in program
make: *** [.obj/thr1.o] Error 1
我 实在是弄不明白 ,谢谢帮助!
离线mog
只看该作者 6楼 发表于: 2006-03-07
下面是引用goldroc于2006-02-25 19:02发表的:
首先,有自定义的signal 和slot 的类必须moc
然后,signal的触发不能够跨线程,如果想跨线程,必须用PostEvent (QApplication)。

signal的触发不能够跨线程?
那demo里的Mandelbrot是怎么做的?
另外如果用PostEvent 怎么获得主线程的QObject * receiver?
离线sinl

只看该作者 7楼 发表于: 2006-04-04
引用第6楼mog2006-03-07 23:27发表的“”:
signal的触发不能够跨线程?
那demo里的Mandelbrot是怎么做的?
另外如果用PostEvent 怎么获得主线程的QObject * receiver?




以前的版本QTSINGAL/SLOT是不能跨线程的,只是到了QT4才可以了。
快速回复
限100 字节
 
上一个 下一个