PDA

View Full Version : Creating a Custom Dialog, and Returning Multiple QString's



c_07
17th October 2007, 22:12
I know this is more of a programming question that a Qt question, but I'm stumped as to how I would implement a dialog (with its own class) that gets, say, two or so text responses from the author, and returns them. My last unsuccessful try was to pass pointers to two QString's to the CustomDialog class's initialization, but I can't figure out how to set the text of those two variables to the text from the two line edits in the popup dialog. Here's what I have so far:


//szName and szDescription are the two inputs that I want to get from the user
//This class is initialized from another source file, which passes pointers to two QString's
DialogAskQuestions::DialogAskQuestions(QMainWindow * parent, QString* szName, QString* szDescription)
{
setupUi(this);

parentWindow = parent;
targetName = szName;
targetDescription = szDescription;

connect(btnOK, SIGNAL(clicked()), this, SLOT(answerAccepted()));
}

void DialogAskQuestions::answerAccepted() {
targetName = edtName->text();
targetDescription = edtDescription->text();
}

Am I going about this the wrong way? How do best I return information (i.e., two QString's) to the class' caller?

Thank you!

marcel
17th October 2007, 22:24
After you press OK, you can still access the line edits, before the dialog gets destroyed.
Typically, you would have something like this:


...
DialogAskQuestions dlg(...);
if(dlg.exec() == QDialog::Accepted)
{
QString s1 = dlg.edtName()->text();
...
}
...


If edtName is private then you have to provide a getter for it.

c_07
18th October 2007, 01:37
Thank you! I didn't know that. Problem solved.

For future reference, though, how would I use pointers with class constructors to pass data back and forth, from the class that generates the information wanted back to the caller that created the class and wants that information? :eek:

nile.one
18th October 2007, 09:07
you can provide some bool flags or even status function, which will describe the child object' state.

simple example:


enum State
{
Idle = 0,
Running,
Error
};

int currentState(); //returns current state from State enum

State _state; //variable for storing current state


then you can check child' state or even block parent object waiting for state changes.
for example


ChildObject obj;
obj.run();

while(ChildObject::Running == obj.currentState)
qApp->processEvents();

...


and another Qt-styled solution is using signal-slot connection, which is of course the best one.