Results 1 to 6 of 6

Thread: How to pass data from QDialog to MainWindow?

  1. #1
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question How to pass data from QDialog to MainWindow?

    Hi everybody!

    This is my first time here so please bare with me

    I have a mainwindow with a menu. When one press "new game" in the menu I'd like to get a new form to make setups in. My idea is to use QDialog with 2 spinboxes for selecting int values. After the OK button is pressed, the data in these two fields should then be passed to the mainwindow in some way and the QDialog is closed.

    The first question is whether I should use QDialog or is there something more suitable for my needs? Some kind of form that can pass different values back to the mainwindow when the form is closed.

    If I use QDialog, I have a few ideas:

    Passing a function pointer (pointing to the function in the mainwindow I want to execute after I have the setupvalues) to the QDialog when I create it in the mainwindow. Then when the OK button is pressed the values are used using the function pointer.

    Passing a pointer (pointing to the mainwindow) to the QDialog so that the QDialog can run the mainwindows function with the values.
    Something like:
    Qt Code:
    1. void SetupDialog::on_buttonBox_accepted()
    2. {
    3. mainwindow->InitializeGame(m_ui->spinBox.value, spinBox_2.value);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Modifying the QDialog in some way so it returns a std:: pair instead of int when using QSetupDialog dialog.exec();
    I tryd modifying the returnvalue in the moc_setupdialog file, but when I build the project, the code was automatically generated into the moc file and overwrited my modifications.
    I wish it could it be done something like:
    std:: pair<int,int> xy = new setupdialog();

    The only way I have succeeded so far was to make temp variables in the function that starts the setupdialog, and pass pointers to them to the setupdialog. This way the setupdialog can access them directly and set there values when the OK button is pressed.
    Something like:
    Qt Code:
    1. int *x;
    2. int *y;
    3.  
    4. SetupDialog dialog = new setupdialog(x,y);
    To copy to clipboard, switch view to plain text mode 

    And in the setupdialog:

    Qt Code:
    1. void SetupDialog::on_buttonBox_accepted()
    2. {
    3. this->x = m_ui->spinBox.value;
    4. this->y = m_ui->spinBox_2.value;
    5. }
    To copy to clipboard, switch view to plain text mode 
    I'd prefer some other solution than this. I don't like the QDialog to setting the values in the mainwindow directly.

    Thanks for any help!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to pass data from QDialog to MainWindow?

    first use QDialog. Second, I would use a getter function in your dialog. would look like:
    Qt Code:
    1. MyDialog dlg;
    2. if (dlg.exec())
    3. {
    4. pair/list/or whatever xyz = dlg.getValues();
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to Lykurg for this useful post:

    ajo (21st February 2013), Saigon (16th November 2009)

  4. #3
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: How to pass data from QDialog to MainWindow?

    Thanks alot!

    This solved my problem. The reason I didn't thought about this, was that I thought the QDialog dies when the .exec returns..

  5. #4
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to pass data from QDialog to MainWindow?

    Anothere (complete!) noob here...I'm not really getting this.

    Qt Code:
    1. ...
    2. qDebug() << "Opening window...";
    3. DodajanjeRacuna x;
    4.  
    5. if (x.exec() == QDialog::Accepted ) {
    6. qDebug() << "You pressed OK!";
    7. }
    8. ...
    To copy to clipboard, switch view to plain text mode 

    The dialog opens and works OK. When I press "OK", I get "you pressed OK") but there is no such thing as x.getValues() available
    Am I missing something in my dialog? I have few text inputs and that's it... there re the constructor and destructor:

    Qt Code:
    1. #include "dodajanjeracuna.h"
    2. #include "ui_dodajanjeracuna.h"
    3. #include <QtDebug>
    4.  
    5.  
    6. DodajanjeRacuna::DodajanjeRacuna(QWidget *parent) :
    7. QDialog(parent),
    8. m_ui(new Ui::DodajanjeRacuna)
    9. {
    10. m_ui->setupUi(this);
    11. this->Rolete(); // fill dropdowns
    12. }
    13.  
    14. DodajanjeRacuna::~DodajanjeRacuna()
    15. {
    16.  
    17. delete m_ui;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Could I use *parent from constructor to pass data to opener; would that be the right way?

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to pass data from QDialog to MainWindow?

    Quote Originally Posted by phoebus View Post
    The dialog opens and works OK. When I press "OK", I get "you pressed OK") but there is no such thing as x.getValues() available
    Am I missing something in my dialog?
    You have to define it yourself. E.g.:
    Qt Code:
    1. QString DodajanjeRacuna::getTextOfInput1()
    2. {
    3. return ui->input1->text();
    4. }
    5. // assuming ui->input1 is a line edit.
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lykurg for this useful post:

    phoebus (1st March 2010)

  8. #6
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to pass data from QDialog to MainWindow?

    Yeah, that works, thanks!

Similar Threads

  1. Replies: 7
    Last Post: 25th July 2016, 15:42
  2. Replies: 2
    Last Post: 27th June 2009, 20:26
  3. How to return a lot of data from a QDialog
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2008, 13:10
  4. TextEdit in QDialog not accepting data
    By nbkhwjm in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2008, 04:53
  5. Replies: 2
    Last Post: 23rd May 2007, 04:51

Tags for this Thread

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.