我有一个带有菜单栏的主窗体。
我的要求
单击菜单栏上的特定QAction时,应打开一个模式窗口。模型窗口包含两个QLineEdit和一个QPushButton。当按下按钮时,一个QLineEdit的值将添加到comboBox(在主窗口中),另一个值应添加到主窗口的字段变量中。
我做了什么
// 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()));
并且被调用者可以接收返回的值并执行所需的操作。这样,对话框不会直接与主窗口交互。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句