Qt - 当我在qApp-> processEvents()

时间:2015-07-28 01:19:22

标签: c++ qt

我的应用程序为图像绘制和标注。当窗口在等待输入时关闭时,应用程序仍然在后台运行。以下是我捕获标签的功能示例。我错过了一些让程序正常退出的东西吗?

 void CustomMainWindow::CaptureLabel(void){

     connect(measurementLabel,
             SIGNAL(mousePressSignal()),
             this,
             SLOT(mouseLabel()),
             Qt::DirectConnection
             );

     m_MouseCount = 0;
     m_CurrentLabel = cv::Point2f(0.0, 0.0);

     while(m_MouseCount < 1){
         qApp->processEvents();

     }

     disconnect(measurementLabel,
             SIGNAL(mousePressSignal()),
             this,
             SLOT(mouseLabel())
             );
 }

2 个答案:

答案 0 :(得分:2)

您可以轻松使用状态机来指导您完成状态更改。这将消除你需要的可怕的伪同步黑客的需要。

class CustomMainWindow : ... {
  QStateMachine m_CaptureLabelMachine;
  ...
};

void CustomMainWindow::CaptureLabel(void){
  if (!m_CaptureLabelMachine.findChild<QFinalState*>()) {
    // build the state machine
    auto machine = &m_CaptureLabelMachine;
    auto s1 = new QState(machine), s2 = new QFinalState(machine);
    machine->setInitialState(s1);
    s1->addTransition(measurementLabel, SIGNAL(mousePressSignal()), s2);
    connect(s1, &QState::entered, [this]{
      connect(measurementLabel, SIGNAL(mousePressSignal()), SLOT(mouseLabel()));
      m_MouseCount = 0;
      m_CurrentLabel = cv::Point2f(0.0, 0.0);
    });
    connect(s2, &QState::entered, [this]{
      disconnect(measurementLabel, SIGNAL(mousePressSignal()), SLOT(mouseLabel()));
    });
  }
  m_CaptureLabelMachine.start();
}

答案 1 :(得分:-1)

我能够使用close事件和while循环中的标志来解决这个问题。

 void CustomMainWindow::closeEvent(QCloseEvent *)
 {
     m_cancelFlag = true;
 }

 void CustomMainWindow::CaptureLabel(void){

     connect(measurementLabel,
             SIGNAL(mousePressSignal()),
             this,
             SLOT(mouseLabel()),
             Qt::DirectConnection
             );

     m_MouseCount = 0;
     m_CurrentLabel = cv::Point2f(0.0, 0.0);

     while(m_MouseCount < 1){
     qApp->processEvents();
     if(m_cancelFlag){
         disconnect(measurementLabel,
                 SIGNAL(mousePressSignal()),
                 this,
                 SLOT(mouseLabel())
                 );
         return;
     }
 }
disconnect(measurementLabel,
        SIGNAL(mousePressSignal()),
        this,
        SLOT(mouseLabel())
        );
}