Qtimer の使い方について助けが必要です。
私はQt 5.0.2で作業していますが、ここで私の問題:
タイマーを開発しようとしていますが、インターフェイスはシンプルです:
ボタンは 2 つだけです。タイマーを起動する「開始」ボタン、「一時停止」ボタン、および時間を表示する QtimeEdit です。
このスクリーンショットは、それがどのように見えるかを示しています: http://img834.imageshack.us/img834/1046/5ks6.png
ポーズ機能が効かないのが難点。ここで Qtimer に関するすべてのドキュメントを読みました: http://harmattan-dev.nokia.com/docs/library/html/qt4/qtimer.htmlとここ: qt.developpez.com/doc/5.0-snapshot/qtimer/ 、しかし、結果はありません。
これは私が持っているソースコードです:(必要なものだけを入れます)
// Creation of the Buttons and the time area
void MainWindow::createBottom()
{
bottom = new QWidget();
play = new QPushButton("Launch",this);
pause = new QPushButton("Pause",this);
play->setDisabled(false);
pause->setDisabled(true);
timeEdit = new QTimeEdit(this);
timeEdit->setDisplayFormat("mm:ss");
layout->addWidget(play);
layout->addWidget(pause);
layout->addWidget(timeEdit );
bottom->setLayout(layout);
connect(play, SIGNAL(clicked()), this, SLOT(startSimulation()));
connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
}
// to resume the timer where is was stopped
void MainWindow::resumeSimulation()
{
timer->blockSignals( false );
pause->setText("Pause");
pause->disconnect(SIGNAL(clicked()));
connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
paused = false;
timer->start();
int timeOfPause = time->restart();
int timeTotal = timeOfPause + timeElapsed;
time->addMSecs(-timeTotal);
}
// to Start the timer
void MainWindow::pauseSimulation()
{
timer->blockSignals(true);
pause->setText("Resume");
timer->stop();
play->setDisabled(false);
//pause->setDisabled(true);
pause->disconnect(SIGNAL(clicked()));
connect(pause, SIGNAL(clicked()), this, SLOT(resumeSimulation()));
paused = true;
}
// to Start the timer from zero.
void MainWindow::startSimulation()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this , SLOT(updateTime()));
timer->start(500);
play->setDisabled(true);
pause->setDisabled(false);
}
void MainWindow::updateTime()
{
if(time == NULL)
{
time = new QTime(0,0,0,0);
time->start();
}
//timeEdit->setTime(QTime::fromS(time->elapsed()));
//time = &(time->addMSecs(1000));
if(hasRestart)
{
time->restart();
time->addMSecs(-timeElapsed);
hasRestart = false;
}
else
{
timeElapsed =+ time->elapsed();
}
int seconds = 0;
int minutes = 0;
int hours = 0;
if(!paused)
{
seconds = (timeElapsed/1000)%60;
minutes = (timeElapsed/60000)%60;
hours = (timeElapsed/3600000)%24;
std::cout << "Test : " << hours << ":" << minutes << ":" << seconds << std::endl;
timeEdit->setTime(QTime(0,minutes,seconds,0));
timeEdit->update();
}
}
スタートボタンを押すとタイマーはうまくスタートするのですが、「一時停止」を押すとグラフィックインターフェースで一時停止するだけで、再開すると一時停止していないかのように現在の時刻が表示されます。
例えば :
私が開始。00:05 に一時停止します。明らかにタイマーをブロックします。10秒待ちます。タイマーを再開すると、00:06 ではなく 00:15 が表示されます
どうすれば修正できますか?
ありがとうございました !
編集:Kuba Oberに感謝しますが、投稿したコードについて説明していただけますか?
一時停止はどのように機能しますか?