Results 1 to 4 of 4

Thread: Creating a Custom Dialog, and Returning Multiple QString's

  1. #1
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Creating a Custom Dialog, and Returning Multiple QString's

    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:

    Qt Code:
    1. //szName and szDescription are the two inputs that I want to get from the user
    2. //This class is initialized from another source file, which passes pointers to two QString's
    3. DialogAskQuestions::DialogAskQuestions(QMainWindow* parent, QString* szName, QString* szDescription)
    4. {
    5. setupUi(this);
    6.  
    7. parentWindow = parent;
    8. targetName = szName;
    9. targetDescription = szDescription;
    10.  
    11. connect(btnOK, SIGNAL(clicked()), this, SLOT(answerAccepted()));
    12. }
    13.  
    14. void DialogAskQuestions::answerAccepted() {
    15. targetName = edtName->text();
    16. targetDescription = edtDescription->text();
    17. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Custom Dialog, and Returning Multiple QString's

    After you press OK, you can still access the line edits, before the dialog gets destroyed.
    Typically, you would have something like this:
    Qt Code:
    1. ...
    2. DialogAskQuestions dlg(...);
    3. if(dlg.exec() == QDialog::Accepted)
    4. {
    5. QString s1 = dlg.edtName()->text();
    6. ...
    7. }
    8. ...
    To copy to clipboard, switch view to plain text mode 

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

  3. The following user says thank you to marcel for this useful post:

    c_07 (18th October 2007)

  4. #3
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Custom Dialog, and Returning Multiple QString's

    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?

  5. #4
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a Custom Dialog, and Returning Multiple QString's

    you can provide some bool flags or even status function, which will describe the child object' state.

    simple example:
    Qt Code:
    1. enum State
    2. {
    3. Idle = 0,
    4. Running,
    5. Error
    6. };
    7.  
    8. int currentState(); //returns current state from State enum
    9.  
    10. State _state; //variable for storing current state
    To copy to clipboard, switch view to plain text mode 

    then you can check child' state or even block parent object waiting for state changes.
    for example
    Qt Code:
    1. ChildObject obj;
    2. obj.run();
    3.  
    4. while(ChildObject::Running == obj.currentState)
    5. qApp->processEvents();
    6.  
    7. ...
    To copy to clipboard, switch view to plain text mode 

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.