PDA

View Full Version : Sending QString to QDialog



nplus
19th March 2008, 20:07
Hi, if I have someDialog : public QDialog class how do I pass a QString from the main window when I call someDialog->exec()?

jpn
19th March 2008, 20:22
QDialog::exec() does not take arbitrary parameters so make it a member variable and provide a getter and/or setter for it..

nplus
21st March 2008, 15:16
Profile *userProfile = new Profile; //the QDialog class
QString strt=currentProfile->text(); //currentProfile is a QAction with text that is something like "some words: username"
strt = strt.section(" ", -1); // I get only the last word

userProfile->user = strt; //Maybe this is wrong?
userProfile->exec();
qDebug() << userProfile->user; //The output is "username"


Profile::Profile()
{
qDebug() << user; //outputs null string

}

The qDebug() output is

""
"username"

So the two strings aren't the same. Where's my error?

wysota
21st March 2008, 16:09
Tell me one thing, what is the execution order of the following four statements in your code:

1. x = userProfile->user;
2. QDialog userProfile;
3. userProfile->user = y;
4. userProfile->exec();

nplus
21st March 2008, 16:15
The first piece of code is in one function. The order is:
1. QDialog definition
2. Parsing the string
3. modifying userProfile->user
4. exec()

wysota
21st March 2008, 20:25
So why do you expect data assigned in operation 3 to be available during operation 1?

nplus
22nd March 2008, 16:38
Shouldn't it be available during operatin 4 - userProfile->exec() ? At least how can I assign userProfile->user when userProfile isn't declared? Can you give me an example?

wysota
22nd March 2008, 17:36
Sure, but the constructor is not called during exec(), right? So accessing it in the constructor will return the value from before you assigned the value.

If you access it at the right time, it will be there.

nplus
22nd March 2008, 18:23
Oh, I haven't noticed that. So what's the right implementation then?

Edit: I managed to get it, problem solved. Thanks.

wysota
22nd March 2008, 18:26
It depends what you want to do... I guess you should have some method that performs adjustments to the dialog after you change the variable and before exec is called. Actually you should change that variable through the same method instead of operating on it directly.

MyDialog dlg;
dlg.setSomething("xyz"); // this sets the string and adjusts the dialog
dlg.exec();