QDialog exec()并获取结果值 [英] QDialog exec() and getting result value

查看:347
本文介绍了QDialog exec()并获取结果值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将 QDialog 子类化以实现类似于 QMessageBox 的功能(我需要使用它来进行自定义)。它具有一条短信和确定,取消按钮。我正在显示使用 exec()进行阻止的对话框。现在,当用户单击确定 /取消时,如何返回true / false值?

I have subclassed QDialog to implement functionality similar to QMessageBox ( I needed this to allow for customization). It has a text message and OK, Cancel buttons. I am showing the dialog using exec() to make it blocking. Now, how do I return values of true/false when the user clicks on OK/Cancel?

我尝试将按钮连接到 setResult() ,然后单击时返回结果值,但

I tried connecting the buttons to setResult() and then, return the result value when clicked, but


  1. 单击按钮不会关闭对话框

  2. 返回值不正确。
    以下是我编写的代码。我认为我在exec / result部分上错了-但是我不确定如何解决它。



class MyMessageBox : public QDialog {
    Q_OBJECT

private slots:

    void onOKButtonClicked() { this->setResult(QDialog::Accepted); }
    void onCancelButtonClicked() { this->setResult(QDialog::Rejected); }

public:
    MyMessageBox(QMessageBox::Icon icon, const QString& title,
        const QString& text, bool showCancelButton = true,
        QWidget* parent = 0);

    virtual void resizeEvent(QResizeEvent* e);

    QDialog::DialogCode showYourself()
    {
        this->setWindowModality(Qt::ApplicationModal);
        this->exec();
        return static_cast<QDialog::DialogCode>(this->result());
    }
};

用户将实例化该类并调用 showYourself()可以返回值并关闭(并删除)对话框。

The user will instantiate the class and call showYourself() which is expected to return the value and also close(and delete) the dialog.

我已经发布了部分代码。让我知道是否需要更多信息,我将发布完整版本。

I have posted partial code. Let me know if you need more and I will post the complete version.

推荐答案

一些要点:


  1. 而不是自己使用 setResult(),而是使用 QDialog :: accept() QDialog :: reject()

  2. 似乎您没有充分利用信号和插槽。您需要创建对话框(或另一个对话框)的对象来监听对话框的信号。

  3. 在您的代码中,您也没有将信号连接到插槽。

  4. 使用我的修复程序 onOKButtonClicked onCancelButtonClicked 都是不必要的。

  5. 使用我的修复程序,您不需要 showYourself()。只需调用 exec ,事件
    就会流动。

  1. Rather than using setResult() yourself, use QDialog::accept() and QDialog::reject().
  2. It seems you are not taking full advantage of the signals and slots. You need the object which create the dialog (or another one) to listen to the signals of the dialog.
  3. In your code you are not connecting signals to slots either.
  4. With my fix onOKButtonClicked and onCancelButtonClicked are unnecessary.
  5. With my fix you don't need showYourself(). Just call exec and with the events information will flow.

您需要在显示对话框之前添加此代码(假定它在对话框方法中):

You need to add this code before showing the dialog (this assume it is in a dialog method):

QObject::connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
QObject::connect(rejectButton, SIGNAL(clicked()), this, SLOT(reject()));

在调用方对象中,您拥有

In the caller object you have

void someInitFunctionOrConstructor(){
   QObject::connect(mydialog, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int)));
}

void dialogIsFinished(int){ //this is a slot
   if(result == QDialog::Accepted){
       //do something
       return
   }
   //do another thing
}

这篇关于QDialog exec()并获取结果值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆