创建一个向主窗体返回值的模态窗口
creating a modal window that returns a value to the main form
我有一个带菜单栏的主表单。
<<p> 我的要求/strong>在菜单栏上点击一个特定的QAction,会打开一个模态窗口。模型窗口包含两个QLineEdit和一个QPushButton。当按钮被按下时,其中一个QLineEdit的值被添加到一个组合框(在主窗口中),另一个值应该被添加到主窗口的字段变量中。
What I have Done
// Defines Action
addrecord = new QAction("Add Record", this);
recordaction->addAction(addrecord);
// COnnect it to the addRecord
connect(addrecord, SIGNAL(triggered()), &dialog1, SLOT(addRecord()));
//dialog class is derived from QDialog....should i change it??
void dialog::addRecord(){
this->setWindowTitle("Add Server");
QLineEdit *edit1 = new QLineEdit(this);
QLineEdit *edit2 = new QLineEdit(this);
QPushButton *ok = new QPushButton("Ok",this);
edit1->move(120, 50);
edit2->move(120, 100);
ok->move(135,150);
this->setMinimumSize(300,200);
this->setWindowFlags(Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
this->setModal(true);
this->show();
}
我现在该怎么做
您可以返回带有响应的结构体,如:
// on your dialog
Response addRecord()
{
...
this->exec(); // will block until you close the dialog
...
Response r;
r.a = edit1->text();
r.b = edit2->text();
return r;
}
// on mainwindow. doAddRecord() must be declared as a slot on mainwindow.h!
void doAddRecord()
{
Response r = dialog->addRecord();
// use the response r
}
connect(addrecord, SIGNAL(triggered()), this, SLOT(doAddRecord()));
被调用方可以接收返回的值并执行所需的操作。这样,对话框就不会直接与主窗口交互。
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 对fread的返回值感到困惑
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 使用 std::p air 进行返回值优化
- 创建一个向主窗体返回值的模态窗口