PDA

View Full Version : Returning from Dialog



SSqt5.2
29th September 2014, 12:06
Hi All,
I am working with QT-4 on desktop applications,
I have creted Child1 and Child 1 has child2, both are dilogs,
my parent is mainwindow and both the childs are dialogs.
when i close Child2, i am returning to parent instead of child 1.

i have use event::accepted to process the data.
What should be the method to accept the data from child2 dialog.

Thanks in advance.

anda_skoa
29th September 2014, 12:58
That description is way to confusing.

You need at least post the two methods that create/execute the dialogs.

Cheers,
_

SSqt5.2
8th October 2014, 13:03
I am using exec() method to show the dialog and checking the return status.

main window has add_item button which has on click slot as
connect( ui->add_contact, SIGNAL(clicked()), this, SLOT(addItem()) );

The slot functiopn Add_Item is as follows
void MainWindow::addItem()
{
MYDialog dlg( this );
if( dlg.exec() == QDialog::Accepted )//for this we have used signal and slot of OK on dialog_window
{
ui->contact_list->addItem( dlg.name() + " -- " + dlg.number() );
qDebug() << "entry accepted form dialog";
}
}


the MYDialog has a lineedit which on return press event creates a numpad dialog.
//in constructor of MYDialog
connect( ui->num_Edit, SIGNAL( returnPressed () ) , this, SLOT( Accept_numer_thrKeypad() ));


Accept_numer_thrKeypad this function creates/executes numpad.
void Dialog::Accept_numer_thrKeypad( )
{
keypad numpad(this);

QString number;
number = this->number();
numpad.setText(number);

if( numpad.exec() == keypad::Accepted )
{
QString value;
value = numpad.return_valueof_keypad();
setNumber(value);
}
}

Both the childs have accept() and reject() slots connected with the OK and Cancel button created on them.
So when i press OK of numpad i am returning to MainWindow, but i expect i should return to its parent i.e. MYdialog object dlg.

wysota
8th October 2014, 22:56
What do you mean by "returning to" main window?

SSqt5.2
9th October 2014, 08:52
Returning to main window means, after clicking OK/Cancel button on Child-2 dialog, the Main window appears, i am expecting to code such that after clicking OK/Cancel of Child-2 dialog, Child-1 dialog should be visible.

wysota
9th October 2014, 10:16
Returning to main window means, after clicking OK/Cancel button on Child-2 dialog, the Main window appears,
Hmm... and what code makes it disappear? Do you call hide() anywhere?

SSqt5.2
17th October 2014, 12:33
In my code i have never called hide(), or Delete() for any dialog or widgets. the dialog disappears after event on OK/Cancel button.

wysota
17th October 2014, 13:18
In my code i have never called hide(), or Delete() for any dialog or widgets. the dialog disappears after event on OK/Cancel button.

So how come when you launch a dialog, the main window disappears?

Maybe it would be best if you provided a minimal compilable example reproducing the problem?

This code seems to work fine:


#include <QtWidgets>

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow() {
QPushButton *pb = new QPushButton("Press");
setCentralWidget(pb);
connect(pb, SIGNAL(clicked()), this, SLOT(showDialog()));

}
public slots:
void showDialog() {
QDialog dlg;
QPushButton *pb = new QPushButton("Show another dialog");
QVBoxLayout *l = new QVBoxLayout(&dlg);
l->addWidget(pb);
connect(pb, SIGNAL(clicked()), this, SLOT(showDialog2()));
dlg.exec();
}
void showDialog2() {
QDialog dlg;
QPushButton *pb = new QPushButton("Close");
QVBoxLayout *l = new QVBoxLayout(&dlg);
l->addWidget(pb);
connect(pb, SIGNAL(clicked()), &dlg, SLOT(accept()));
dlg.exec();
}
};

#include "main.moc"

int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow mw;
mw.show();
app.exec();
}

SSqt5.2
20th October 2014, 11:03
Thanks for reply.
I tried this and find the error :: i was taking event on "QDialog" where it was supposed to be user_defined dialog(so, as Child-2 is also derived from QDialog, the event was passed to Child-1).

Another error.
If i have accept and reject slot for my-defined dialog,
and i check the event as
if (numpad.exec() == Keypad::Accepted )
{
}
else if( numpad.exec() == Keypad::Rejected )
{
}

i find that i need to specify two events before the dialog returns to parent (may be bcz, in one function i ma calling exec() twice). What is the way to return to parent window by accept or reject event only? (can i write switch case for event and if yes how?)

anda_skoa
20th October 2014, 11:16
You are potentially running the dialog twice.
If the first run does not return Keypad::Accepted, the second if condition will be evaluated which runs the dialog again.

If your exec() can return more than two values, consider using a switch instead of multiple if/else-if

Cheers,
_