QDialog 关闭时如何终止 QStateMachine?
How can I terminate QStateMachine when QDialog is closed?
我有一个 QDialog 和 QStateMachine。当所有应用程序关闭时,此循环终止,但我想在 Qdialog 关闭时终止循环。我该怎么做?
QStateMachine sm;
QState s1(&sm), s2(&sm);
sm.setInitialState(&s1);
QEventTransition transition(dialog, QEvent::Close);
s2.addTransition(&transition);
QEventLoop loop;
QObject::connect(&s2, &QState::entered, &loop, &QEventLoop::quit);
sm.start();
dialog->show();
loop.exec();
例如使用QFinalState
class for this. At docs example shown usage QPushButton
with exit. You just need to connect it with finished()
信号。
UPD
一些用法示例:
MainWinow.h
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QDialog *dialog;
private slots:
void on_pushButton_clicked();
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
dialog = new QDialog;
}
void MainWindow::on_pushButton_clicked()
{
// Open dialog after push button was clicked
if (!dialog)
dialog = new QDialog;
QEventLoop loop;
QStateMachine machine;
QState *s1 = new QState();
QFinalState *s2 = new QFinalState();
s1->addTransition(dialog, SIGNAL(finished(int)), s2);
connect(&machine, QStateMachine::finished, [&loop]{
qDebug() << "Finished";
loop.quit();
});
machine.addState(s1);
machine.addState(s2);
machine.setInitialState(s1);
machine.start();
dialog->show();
loop.exec();
qDebug() << "Really finished";
}
所以我们创建 QEventLoop
、QStateMachine
并添加状态(初始状态和最终状态)。然后将 QDialog::finished()
信号连接到最终状态,并将 QStateMachine::finished()
信号连接到事件循环将停止的插槽。然后显示QDialog
并开始QStateMachine
。
在控制台将打印:
Finished
Realy finished
我有一个 QDialog 和 QStateMachine。当所有应用程序关闭时,此循环终止,但我想在 Qdialog 关闭时终止循环。我该怎么做?
QStateMachine sm;
QState s1(&sm), s2(&sm);
sm.setInitialState(&s1);
QEventTransition transition(dialog, QEvent::Close);
s2.addTransition(&transition);
QEventLoop loop;
QObject::connect(&s2, &QState::entered, &loop, &QEventLoop::quit);
sm.start();
dialog->show();
loop.exec();
例如使用QFinalState
class for this. At docs example shown usage QPushButton
with exit. You just need to connect it with finished()
信号。
UPD
一些用法示例:
MainWinow.h
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QDialog *dialog;
private slots:
void on_pushButton_clicked();
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
dialog = new QDialog;
}
void MainWindow::on_pushButton_clicked()
{
// Open dialog after push button was clicked
if (!dialog)
dialog = new QDialog;
QEventLoop loop;
QStateMachine machine;
QState *s1 = new QState();
QFinalState *s2 = new QFinalState();
s1->addTransition(dialog, SIGNAL(finished(int)), s2);
connect(&machine, QStateMachine::finished, [&loop]{
qDebug() << "Finished";
loop.quit();
});
machine.addState(s1);
machine.addState(s2);
machine.setInitialState(s1);
machine.start();
dialog->show();
loop.exec();
qDebug() << "Really finished";
}
所以我们创建 QEventLoop
、QStateMachine
并添加状态(初始状态和最终状态)。然后将 QDialog::finished()
信号连接到最终状态,并将 QStateMachine::finished()
信号连接到事件循环将停止的插槽。然后显示QDialog
并开始QStateMachine
。
在控制台将打印:
Finished
Realy finished