QObject::startTimer: Timers can only be used with threads started with QThread

写Qt定时器时,老是报如下错误,

QObject::startTimer: Timers can only be used with threads started with QThread

我自己代码如下:


class LiveStream : public QObject
{
    Q_OBJECT
public:
    LiveStream(QObject *parent = 0);
    ~LiveStream() ;

private slots:
    void timerDelMedia() ;
private:
    QTimer *delMediaTimer;

} ;


LiveStream::LiveStream(QObject *parent)
    :QObject(parent)
    ,delMediaTimer(NULL)
{
  
    delMediaTimer = new QTimer(this) ;
    delMediaTimer->setInterval(30*60*1000);
    connect(delMediaTimer , SIGNAL(timeout()) , this , SLOT(timerDelMedia())) ;
    delMediaTimer->start();
}

仔细检查了几遍,都没有发现问题。
最后网上查找资料,一看是我改了main函数

int main(int argc, char *argv[])
{
   LiveStream stream ;
   stream.startLiveStream();

  while(1)
  	sleep(1);
}

把main函数改回去,就好了

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

   LiveStream stream ;
   stream.startLiveStream();

   return a.exec();
}

你可能感兴趣的:(Qt)