Qt World Summit: Register Today!

Set and Get Dialog's controls from the Parent

  • Hello, I'm on My first Qt Project, I used to code in VB6, Qt is very different from vb6, so I'm lost.

    now I'm on Linux(Ubuntu) and I'm re doing a vb6 project(translating it) in Qt, In vb6 I could from a Form(MainWindow) a call Dialog Form, set new Dialog control's value and then show it, and before close this Dialog, could (if Accepted)set some public variables and then from the Form(MainWindow) use this variables. I'm going to Manipulate an File, for doing this I created a Dialog(with a class), in which I put some controls(dateEdit, SpinBox, DoubleBox) I'm planning to use the same Dialog, to Add or Edit some data, in order to do this I need to access to the Dialog's controls to pre set their values and use this controls values after I Accepted it

    Img: http://s14.postimg.org/jrep84l0h/aaaaaa.png

    I have to set a dateEdit.date, depending if I add or edit, hide the SpinBox depending if I add or edit units or hide it if I need to add or edit Currency.

    what I think is that I could create the variables that I need in a .h file, include it in Mainwindow.cpp and in AddDialog.cpp and in this way I could easily get and set the values for this variables, or creating a class, but I'm confiused...

  • Lifetime Qt Champion


    I would recommend that you start with QDialog's "documentation":http://qt-project.org/doc/qt-4.8/qdialog.html and examples.

    That will give you some good ideas on how things are done Qt style.

    Hope it helps

  • I think I solved it, I don't know if it is the right way of doing this but it works for me:

    lass AddDialog : public QDialog


    QDate  GetToday();
    void  SetToday(QDate val);

    // the rest of the AddDialog class
    QDate AddDialog::GetToday(){
    return ui->dateEdit->date();
    void AddDialog::SetToday(QDate val){

    and the Mainwindow.cpp:
    QDate MyDate;
    AddDialog *mD = new AddDialog(this);

    if(mD->exec() == 1){
        MyDate = mD->GetToday();
    delete mD;


    apparently it worked for me.

    in spite of this I accept suggestions

  • Lifetime Qt Champion

    If you want to follow Qt's style, it should rather be:

    QDate today() const;
    void setToday(const QDate &date);

Log in to reply