C++ QDialog exec() 并获取结果值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12470806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:18:10  来源:igfitidea点击:

QDialog exec() and getting result value

c++qtqdialog

提问by go4sri

I have subclassed QDialogto 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?

我已经子类化QDialog以实现类似于QMessageBox(我需要它来允许自定义)的功能。它有一条短信和“确定”、“取消”按钮。我正在显示exec()用于使其阻塞的对话框。现在,当用户单击确定/取消时,我如何返回真/假值?

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

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

  1. Clicking the buttons does not close the dialog box
  2. the return value is incorrect. Following is the code I have written. I think I am wrong in the exec/result part - but I am not sure how to fix it.
  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());
    }
};

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

用户将实例化类并调用showYourself()预期返回值并关闭(和删除)对话框。

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

我已经发布了部分代码。如果您需要更多,请告诉我,我会发布完整版本。

回答by UmNyobe

Some points :

几点:

  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 onOKButtonClickedand onCancelButtonClickedare unnecessary.
  5. With my fix you don't need showYourself(). Just call execand with the events information will flow.
  1. 与其使用setResult()自己,不如使用QDialog::accept()QDialog::r​​eject()
  2. 看来您没有充分利用信号和插槽。您需要创建对话(或另一个)的对象来收听对话的信号。
  3. 在您的代码中,您也没有将信号连接到插槽。
  4. 用我的修复onOKButtonClickedonCancelButtonClicked是不必要的。
  5. 有了我的修复,你就不需要了showYourself()。只需打电话exec,事件信息就会流动。

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

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

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
}

回答by Jens A. Koch

Another solution:

另一种解决方案:

    // set signal and slot for "Buttons"
    connect(YesButton, SIGNAL(clicked()), dlg, SLOT(accept()));
    connect(NoButton, SIGNAL(clicked()), dlg, SLOT(reject()));

    // show modal window event loop and wait for button clicks
    int dialogCode = dlg->exec();

    // act on dialog return code
    if(dialogCode == QDialog::Accepted) { // YesButton clicked }
    if(dialogCode == QDialog::Rejected) { // NoButton clicked }

回答by ScarCode

Case 1Clicking the buttons does not close the dialog box.

情况 1单击按钮不会关闭对话框。

For this you have to close the dialog on respective SLOTS, so Use

为此,您必须关闭相应的对话框SLOTS,因此请使用

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

Note: Only after you have clicked the Ok button or Cancel button in a standard QMessageBox, setResult() function is triggered and the status is changed. It's not the same effect when done vice versa.

注意:只有在标准的 QMessageBox 中点击 Ok 按钮或 Cancel 按钮后,才会触发 setResult() 函数并更改状态。反之,则效果不同。

Case 2The return value is incorrect.

情况2返回值不正确。

I think only after your dialog gets closed, you will have the result available in result()function. So I guess it will be solved, after you have made the changes specified in Case 1.

我认为只有在您的对话框关闭后,您才能在result()函数中获得结果。所以我想它会在您进行案例 1 中指定的更改后解决。

If it still persists, use your own private member function to resolve it.

如果它仍然存在,请使用您自己的私有成员函数来解决它。