2 개의 버튼과 2 개의 기능을 연결하는 QMainWindow
이 있습니다. StartBTNClick
기능으로 QTimer를 시작하고 StopBTNClick
기능으로 QTimer를 중지하고 싶습니다. 내 문제는 QTimer가 StopBTNClick
에 정의되어 있지 않다는 것입니다. 그래서 공개 QTimer를 정의하는 방법을 알고 싶습니다.QTimer를 공개로 설정하십시오. (Qt, C++)
MyClass::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
statusBar()->showMessage("Status: Idle.");
connect(ui.StartBTN, SIGNAL(clicked()), this, SLOT(StartBTNClick()));
connect(ui.StopBTN, SIGNAL(clicked()), this, SLOT(StopBTNClick()));
}
void MyClass::StartBTNClick()
{
QTimer *RunTimer = new QTimer(this);
connect(RunTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
RunTimer->start(5000);
statusBar()->showMessage("Status: Running.");
}
void MyClass::StopBTNClick()
{
RunTimer->stop(); // Not working. Says it's not defined.
disconnect(RunTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
statusBar()->showMessage("Status: Idle.");
}
void MyClass::TimerHandler()
{
// I set QMessageBox to test if it's working.
QMessageBox::information(this, "lala", "nomnom");
}
부적처럼 작동합니다. 감사합니다. :) QTimer가 실행 중인지 확인하기 위해'StopBTNClick'에서 새로운 bool과 if 명령을 만들었습니다. ^^ – HitomiTenshi
1. bool이 필요 없으므로 대신 runTimer-> isActive()를 사용할 수 있습니다. 2. 매회 시그널/슬롯 연결을 끊을 필요가 없습니다. 당신은 클래스 생성자에서 타이머를 만들고 연결해야합니다. 3. 시작 후 시작 버튼을 사용 중지하고 중지 버튼을 사용 중지 할 수 있습니다. –
Kamil이 정확합니다.이 방법이 더 좋습니다. 당신이하고있는 방식은 효과가있을 수 있으며 특수한 경우에는 필요할 수도 있지만 귀하의 경우에는 그렇지 않을 수도 있습니다. – tmpearce