0
を残している:ボタンならば、それはタイムアウトし、QMessageBoxを続行」 はなぜQtのイベントループは、私は次の操作を実行する小さなプログラムをしたい
-
は、シングルショットQTimer
- を開始
- "、クリックするとボックスが閉じられ、タイマーボタン場合
- を再起動し、『停止』をクリックすると、ボックスが閉じ、アプリケーションが問題Iヘクタール
を終了します私はメッセージボックスを隠すとすぐにイベントループが残っているということです。ボックスは1回だけ表示されます。私はコンソール版でプログラムを再現し、期待通りに動作します。ここに私のコードです。あなたの助けを前にありがとう。
main.cの
#include <QtGui/QApplication>
#include "TimedDialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TimedDialog dialog(&app, SLOT(quit()));
dialog.run();
return app.exec();
}
TimedDialog.h
#ifndef TIMED_DIALOG_H
#define TIMED_DIALOG_H
#include <QObject>
#include <QTimer>
class QMessageBox;
class QPushButton;
class QAbstractButton;
class TimedDialog : public QObject
{
Q_OBJECT
public:
TimedDialog(
QObject const * receiver,
char const * method);
~TimedDialog();
void run(void);
signals:
void stop(void);
private:
QMessageBox* _box;
QPushButton* _buttonContinue;
QPushButton* _buttonStop;
QTimer _timer;
static int const DELAY = 2 * 1000; // [ms]
private slots:
void onTimeout(void);
void onButtonClicked(QAbstractButton * button);
};
#endif
TimedDialog.cpp
#include <assert.h>
#include <QMessageBox>
#include <QPushButton>
#include "TimedDialog.h"
TimedDialog::TimedDialog(
QObject const * receiver,
char const * method)
: QObject(),
_box(0),
_buttonContinue(0),
_buttonStop(0),
_timer()
{
_box = new QMessageBox();
_box->setWindowModality(Qt::NonModal);
_box->setText("Here is my message!");
_buttonContinue = new QPushButton("Continue");
_box->addButton(_buttonContinue, QMessageBox::AcceptRole);
_buttonStop = new QPushButton("Stop");
_box->addButton(_buttonStop, QMessageBox::RejectRole);
_timer.setSingleShot(true);
assert(connect(&_timer, SIGNAL(timeout()), this, SLOT(onTimeout())));
assert(connect(_box, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(onButtonClicked(QAbstractButton *))));
assert(connect(this, SIGNAL(stop()), receiver, method));
}
TimedDialog::~TimedDialog()
{
delete _box;
}
void TimedDialog::onTimeout(void)
{
_box->show();
}
void TimedDialog::onButtonClicked(QAbstractButton * button)
{
_box->hide();
if (button == _buttonContinue)
{
_timer.start(DELAY);
}
else
{
emit stop();
}
}
void TimedDialog::run(void)
{
_timer.start(DELAY);
}
おかげで、コードを修正し、簡素化することができます。私は同じタイマーの原則に基づいてコンソールプログラムを実行できるので、問題はシングルショットタイマーではないと信じています。 「続行」ボタンを押すと、タイマーが再開され、プログラムの実行が継続されることが期待されます。それは何もしません。 – Dams