PDA

View Full Version : How to return a value to a parent window



pcheng
15th June 2012, 10:46
I have a problem that will probably be something very simple to solve but I can't seem to find it on the web.

I have a window that shows a table from a MySQL database. I created a find dialog box which opens and allows the user to find an item from the table. The results of the find are shown below the find dialog box in a tableview. When a user selects the item he was searching for he clicks on a button select which in turn opens the product window with the current index shown.

The problem is that I create a new window of the product every time I find something. Is there a way to go back to the original product window and simple use the index returned by the find dialog box to set the mapper index to?

Thanks,

Pericles

Added after 44 minutes:

I kind of solved the problem. I am not sure how crude is my solution.

I called the find dialog box using exec() and then created a getIndex() function to return the index value of the found item.

Now to get the correct index value.

Pericles

Santosh Reddy
15th June 2012, 10:46
Two ways to do this.

1. Pass the handle of the parent window to the dialog and call the required function directly (you can also get the parent handle from parent() call)
2. Create a signal in the dialog and connect to a slot in the parent while creating the dialog.

I prefer the second one, this way dialog is independent of parent.

Ali Reza
17th June 2012, 09:58
@ Santosh Reddy

hi sir

about this sentence: "2. Create a signal in the dialog and connect to a slot in the parent while creating the dialog.
"
please explain a bit.

thanks in advance.

ChrisW67
17th June 2012, 23:47
Signals & Slots

Ali Reza
19th June 2012, 07:43
hi dear Chris !!!
i knew Signals & Slots. please explain ....
lol
10Q

wysota
19th June 2012, 10:27
So if you know signals and slots then what other explanation do you need? I think Santosh told you exactly what to do.

sberl
19th June 2012, 19:47
I am having a similar issue to pcheng. I am able to write information to dialogs by doing something like

dialog->ui->textEdit->setText(QString);
However, I do not know how to do it vice versa; therefore, retrieve the data from the dialog and use it in the main window. Santosh mentioned signals and slots, but I don't really know anything about those and the documentation isn't very easy to understand.

Seeing as this is the Newbie Forum, do you think you could guide us through this?

wysota
19th June 2012, 19:49
I am having a similar issue to pcheng. I am able to write information to dialogs by doing something like

dialog->ui->textEdit->setText(QString);
However, I do not know how to do it vice versa; therefore, retrieve the data from the dialog and use it in the main window.

dialog->ui->textEdit->text()
?

sberl
19th June 2012, 19:57
maybe i wasn't clear. that is how i would go about writing information to a dialog. but, i need to extract the information from the dialog and use it in my main window after clicking an OK button

wysota
19th June 2012, 20:17
Maybe you're still not clear. What text() does is that it returns a value. You can't set (aka write) anything using that method.

Ali Reza
19th June 2012, 20:18
when OK button clicked in your dialog window, you can emit a signal that contains whole of dialog information and connect this signal to main window slot.

sberl
19th June 2012, 20:47
@wysota. i don't know what your talking about. it seems to work fine for me


Dialog_Test *testdialog = new Dialog_Test(this);
testdialog->setWindowTitle("Test Dialog");
testdialog->ui->textBoxInDialog->setText(mystring);


@Ali Reza
I know this is going to cause outrage, but how do I emit a signal, i can set one up


testdialog.h
signals:
int emitSignal();

testdialog.cpp
int emitSignal()
{
return myint;
}

but how do I connect that with anything.

wysota
19th June 2012, 21:15
@wysota. i don't know what your talking about. it seems to work fine for me


Dialog_Test *testdialog = new Dialog_Test(this);
testdialog->setWindowTitle("Test Dialog");
testdialog->ui->textBoxInDialog->setText(mystring);

Compare your code to mine.

sberl
19th June 2012, 21:34
Compare your code to mine.

'text' : is not a member of 'QLineEdit'

wysota
19th June 2012, 21:40
That's sad because the docs say it is -- QLineEdit::text

Ali Reza
20th June 2012, 10:32
by commamd:
emit signalFunctionName;