我已经将 QDialog 子类化以实现类似于 QMessageBox 的功能(我需要它来允许自定义).它有一条短信和确定"、取消"按钮.我正在使用 exec() 显示对话框以使其阻塞.现在,当用户单击确定/取消时,我如何返回真/假值?
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
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.
几点:
setResult(),不如使用 QDialog::accept() 和 QDialog::reject().onOKButtonClicked 和 onCancelButtonClicked 是不必要的.showYourself().只需调用 exec 和事件信息会流动.setResult() yourself, use QDialog::accept() and QDialog::reject(). onOKButtonClicked and onCancelButtonClicked are unnecessary.showYourself(). Just call exec and with the events
information will flow.您需要在显示对话框之前添加此代码(this 假设它在对话框方法中):
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()));
在调用者对象中你有
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() 并获取结果值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)