Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. 新线程中使用QTimer出现Timers can only be used with threads started with QThread
Forum Updated to NodeBB v4.3 + New Features

新线程中使用QTimer出现Timers can only be used with threads started with QThread

Scheduled Pinned Locked Moved Unsolved Chinese
2 Posts 2 Posters 711 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    verycuteman
    wrote on last edited by
    #1

    void Widget::start_time()
    {
    timethread =new QThread;
    time2=new QTimer;
    time2->setInterval(2000);
    time2->moveToThread(timethread);
    connect(timethread,&QThread::finished,time2,&QTimer::deleteLater);
    connect(time2,&QTimer::timeout,[=]{
    Widget::ontime_1();
    });
    timethread->start();
    time2->start();

    }
    time2启动不了

    M 1 Reply Last reply
    0
    • V verycuteman

      void Widget::start_time()
      {
      timethread =new QThread;
      time2=new QTimer;
      time2->setInterval(2000);
      time2->moveToThread(timethread);
      connect(timethread,&QThread::finished,time2,&QTimer::deleteLater);
      connect(time2,&QTimer::timeout,[=]{
      Widget::ontime_1();
      });
      timethread->start();
      time2->start();

      }
      time2启动不了

      M Offline
      M Offline
      MaxQi
      wrote on last edited by
      #2

      @verycuteman QTimer的创建和启动必须在同一线程。
      你这里是在主线程创建了定时器然后在子线程启动它,这是不允许的。
      可以试试继承QThread并在其子类的run里启动你的定时器。

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved