Ayush choubey
Ayush choubey

Reputation: 616

creating a modal window that returns a value to the main form

I have a main form with menu bar.

My Requirement

On clicking a particular QAction on menubar, a modal window should open. The model window contains two QLineEdit and a QPushButton. When the button is pushed the value of one of the QLineEdit is added to a comboBox(in the main window) and the other value should be added in the field variable of the mainwindow.

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();
}

How should i proceed now

Upvotes: 1

Views: 357

Answers (1)

Kknd
Kknd

Reputation: 3217

You could return a struct with the responde, like:

// 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()));

And the called could receive the returned values and perform the required actions. This way, the dialog does not interacts directly with the mainwindow.

Upvotes: 1

Related Questions