0

我有一个关于 QEventLoop 的问题:如何设置 QEventLoop 以丢弃某些信号

class MyThread : public QThread
{
    Dialog *_dlg;
public:
    MyThread(Dialog* dlg)
        : _dlg(dlg)
    {

    }
    virtual void run()
    {
        QTimer* _timer;
        _timer = new QTimer(this);
        connect(_timer, SIGNAL(timeout()),
                _dlg, SLOT(timeout()), Qt::BlockingQueuedConnection);
        _timer->start(3000);

        QEventLoop loop;
        loop.exec(QEventLoop::WaitForMoreEvents);
    } 
};

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    connect(ui->pushButton, SIGNAL(clicked()),
            this, SLOT(click()));

    MyThread* thread = new MyThread(this);
    thread->start();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::click()
{
    QMessageBox msgBox(this);
    msgBox.setText("click.");
    msgBox.exec();
}

void Dialog::timeout()
{
    QMessageBox msgBox(this);
    msgBox.setText("timeout.");
    msgBox.exec();
}

这是我启动这个程序时的代码,每隔 5 秒就会显示一个超时 QMessageBox。但我想如果我按下按钮然后显示点击消息框并且不显示超时消息框..我找不到任何方法来做到这一点

在此处输入图像描述

4

0 回答 0