2016-10-21 15 views
0

を残している:ボタンならば、それはタイムアウトし、QMessageBoxを続行」 はなぜQtのイベントループは、私は次の操作を実行する小さなプログラムをしたい

  • を示しているとき

      は、シングルショットQTimer
    1. を開始
    2. "、クリックするとボックスが閉じられ、タイマーボタン場合
    3. を再起動し、『停止』をクリックすると、ボックスが閉じ、アプリケーションが問題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); 
    } 
    
  • 答えて

    0

    作成されたタイマーがあることです既にtimeoutと呼ばれているsingleshotと設定されています。

    あなたはstart()への呼び出しのためにQTimer source codeを見れば、あなたはそれを述べて見ることができます: -

    SINGLESHOTがtrueの場合、タイマーは一度だけ有効になります。

    あなたはクラスの機能を使って答えをQTimer::singleShot

    +0

    おかげで、コードを修正し、簡素化することができます。私は同じタイマーの原則に基づいてコンソールプログラムを実行できるので、問題はシングルショットタイマーではないと信じています。 「続行」ボタンを押すと、タイマーが再開され、プログラムの実行が継続されることが期待されます。それは何もしません。 – Dams

    関連する問題