【无标题】

考虑将QT窗口作为阻塞线程,加入到主线程,其余线程detach掉


class InteractiveX : public Interactive {
 public:
  InteractiveX() : dt_handler(nullptr), p_gui(nullptr), state(eIdle) {}
  virtual ~InteractiveX() { std::cout << "InteractiveX quit" << std::endl; }
...

 public:
  void Run(DataHandler* data_handler) override {
    dt_handler = data_handler;
    state = eInput;
    std::thread output(&InteractiveX::Output, this);
    output.detach();
    std::thread input(&InteractiveX::Input, this);
    input.detach();
    std::thread gui(&InteractiveX::gui_start, this);
    gui.join();
  }

 public:
  void Input() {
  ...
  }
  void Output() {
  ...
  }

 private:
  void gui_start() {
    p_gui = new GuiIntf;
    p_gui->thread_start();
    delete p_gui;
    p_gui = nullptr;
  }

  void gui_stop() {
    // 使用invokeMethod模拟connect信号到槽的连接
    QMetaObject::invokeMethod(p_gui, "stop", Qt::DirectConnection);
  }

 private:
  DataHandler* dt_handler;
  GuiIntf* p_gui;
  int state;
};

Interactive::~Interactive() {}
InteractiveX interactive_instance;
Interactive* Interactive::GetInstance() { return &interactive_instance; }

在 gui线程,创建窗口:


class GuiIntf : public QObject {
  Q_OBJECT
 public:
  GuiIntf() : p_app(nullptr), p_win(nullptr) {}

 public:
  virtual ~GuiIntf() {
    delete p_win;
    delete p_app;
  }
  
  void thread_start() {
    int argc = 0;
    char* argv = "";
    p_app = new QApplication(argc, &argv);
    p_win = new MainWindow;
    p_win->show();

    p_app->exec();
  }

  void thread_stop() {
    if (nullptr != p_win) {
      p_win->close();
    }
    if (nullptr != p_app) {
      p_app->quit();
    }
  }

 public slots:
  void stop() { thread_stop(); }

 private:
  QApplication* p_app;
  MainWindow* p_win;
};

通过 QMetaObject::invokeMethod 来进行QT线程通信

  void gui_stop() {
    // 使用invokeMethod模拟connect信号到槽的连接
    QMetaObject::invokeMethod(p_gui, "stop", Qt::DirectConnection);
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值