Results 1 to 6 of 6

Thread: how can dialog access variable from parent MainWindow?

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

    Default how can dialog access variable from parent MainWindow?

    hi! I hope you can help me, this is a very noob question but i'm tired of googling it

    I have a mainwindow with a variable and I call a dialog , both designed with QtCreator, i want to access this variable from the dialog , so i do
    Qt Code:
    1. ((MainWindow *) parent)->MyVariable = something;
    To copy to clipboard, switch view to plain text mode 

    to do this i need to include mainwindow.h in dialog.h


    but ... when i do that i got the error :
    error: 'Dialog' does not name a type
    so this is one question .... the other question is almost the same! how can i control a Dialog widget(like a line edit) from Mainwindow ?

    here is a simple example, the mainwindow has a list widget and a pushbutton when you click the button it opens the dialog, in witch you can add a string to the list :
    the dialog :

    dialogc.png

    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include "mainwindow.h"
    6. namespace Ui {
    7. class Dialog;
    8. }
    9.  
    10. class Dialog : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Dialog(QWidget *parent = 0);
    16. ~Dialog();
    17. Ui::Dialog *ui;
    18. private slots:
    19. void on_pushButton_clicked();
    20.  
    21. private:
    22.  
    23. };
    24.  
    25. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. Dialog::Dialog(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::Dialog)
    6. {
    7. ui->setupUi(this);
    8. ui->lstStrings->addItems(((MainWindow *) parent)->MyList);
    9. }
    10.  
    11. Dialog::~Dialog()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Dialog::on_pushButton_clicked()
    17. {
    18. ((MainWindow *) parent)->MyList.append(ui->leStringToAdd->text() );
    19. }
    To copy to clipboard, switch view to plain text mode 


    and the mainwindow :

    mainwindow.png

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QStringList>
    6. #include <QListWidgetItem>
    7. #include "dialog.h"
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. QStringList MyList;
    21. private slots:
    22. void on_btnAdd_clicked();
    23.  
    24. void on_lstStrings_itemDoubleClicked(QListWidgetItem *item);
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. Dialog *dlg;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. MyList << "one" << "two" << "three";
    11.  
    12. ui->lstStrings->addItems(MyList);
    13.  
    14. dlg = new Dialog(this);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::on_btnAdd_clicked()
    23. {
    24. dlg->exec();
    25. }
    26.  
    27. void MainWindow::on_lstStrings_itemDoubleClicked(QListWidgetItem *item)
    28. {
    29. // how do i access dlg->ui->leStringToAdd widget?
    30. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance!

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how can dialog access variable from parent MainWindow?

    no no no no no.

    Dialog should not be doing anything to their parent! Obeying simple principles will help you avoid poor code like you are making - including circular references that just will not work.
    e.g. A.hpp includes B.hpp & B.hpp include A.hpp. This just doesnt work if the full types are needed.

    If you want the parent/mainwindow to change as a result of something happening in the dialog, then the parent should pull the info off from the dialog and change its own members itself.
    Qt Code:
    1. void
    2. SomeWidget::SomeFunction()
    3. {
    4. MyDialog dialog;
    5. dialog.exec();
    6.  
    7. if (dialog.result() == Accepted)
    8. {
    9. int newValue = dialog.GetNewValue();
    10. this->SomeSetterFunction(newValue);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    If you need things to happen while the dialog is open, then give the dialog signals and let the parent connect the signal to whatever slot it wants.

    A dialog should be agnostic of its parent - it should NOT need to know ANYTHING at all about it (except that it is a QWidge*).
    Last edited by amleto; 18th April 2012 at 18:01.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    krezix (18th April 2012)

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

    Default Re: how can dialog access variable from parent MainWindow?

    thanks for your anwser , i thought i could work with dialogs like delphi! I know what to do then! thanks once again

  5. #4
    Join Date
    Apr 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how can dialog access variable from parent MainWindow?

    If you need things to happen while the dialog is open, then give the dialog signals and let the parent connect the signal to whatever slot it wants.
    Hi, can you give me sample code for the above solution using connect

    I tried this code but was unsuccessful

    void MainWindow:n_teacher_button_clicked()
    {
    Dialog d;
    d.setModal(true);
    connect(d.pushButton, SIGNAL("clicked()"), this->clicked())
    d.exec();
    }

  6. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how can dialog access variable from parent MainWindow?

    "connect(d.pushButton, SIGNAL("clicked()"), this->clicked())"
    seriously?

    read a tutorial on signals and slots.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #6
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: how can dialog access variable from parent MainWindow?

    Hi,
    this is a good text to learn from:
    http://qt-project.org/doc/qt-4.8/signalsandslots.html

    In your MainWindow header file you have to define a
    Qt Code:
    1. public slots:
    2. void yourSlotName();
    To copy to clipboard, switch view to plain text mode 
    and implement this in the cpp file.
    Now connect the self-made slot with the pushButton's built-in clicked() signal:
    Qt Code:
    1. connect(d.pushButton, SIGNAL(clicked()), this, SLOT(yourSlotName());
    To copy to clipboard, switch view to plain text mode 
    [N.B. you should not try to create a slot named "clicked()", because that is a built-in signal of almost everything in Qt.]

Similar Threads

  1. How to access objects of parent Dialog from Child Dialog .
    By ranjit.kadam in forum Qt Programming
    Replies: 4
    Last Post: 18th April 2011, 06:39
  2. Replies: 3
    Last Post: 12th April 2011, 10:58
  3. closing child dialog closes parent dialog
    By sparticus_37 in forum Newbie
    Replies: 2
    Last Post: 28th May 2010, 19:46
  4. How to blur parent dialog when child dialog is displayed
    By abhilashajha in forum Qt Programming
    Replies: 4
    Last Post: 10th June 2009, 13:01
  5. How can I access public variable parent Form
    By validator in forum Qt Programming
    Replies: 14
    Last Post: 18th December 2008, 21:12

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.