PDA

View Full Version : how can dialog access variable from parent MainWindow?



krezix
18th April 2012, 16:22
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

((MainWindow *) parent)->MyVariable = something;

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 :

7604



#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "mainwindow.h"
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
Ui::Dialog *ui;
private slots:
void on_pushButton_clicked();

private:

};

#endif // DIALOG_H




#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->lstStrings->addItems(((MainWindow *) parent)->MyList);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::on_pushButton_clicked()
{
((MainWindow *) parent)->MyList.append(ui->leStringToAdd->text() );
}



and the mainwindow :

7605



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStringList>
#include <QListWidgetItem>
#include "dialog.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

QStringList MyList;
private slots:
void on_btnAdd_clicked();

void on_lstStrings_itemDoubleClicked(QListWidgetItem *item);

private:
Ui::MainWindow *ui;
Dialog *dlg;
};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

MyList << "one" << "two" << "three";

ui->lstStrings->addItems(MyList);

dlg = new Dialog(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_btnAdd_clicked()
{
dlg->exec();
}

void MainWindow::on_lstStrings_itemDoubleClicked(QListW idgetItem *item)
{
// how do i access dlg->ui->leStringToAdd widget?
}


thanks in advance!

amleto
18th April 2012, 17:53
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.


void
SomeWidget::SomeFunction()
{
MyDialog dialog;
dialog.exec();

if (dialog.result() == Accepted)
{
int newValue = dialog.GetNewValue();
this->SomeSetterFunction(newValue);
}
}


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*).

krezix
18th April 2012, 20:36
thanks for your anwser , i thought i could work with dialogs like delphi! I know what to do then! thanks once again ;)

djcm75
29th April 2012, 19:20
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::on_teacher_button_clicked()
{
Dialog d;
d.setModal(true);
connect(d.pushButton, SIGNAL("clicked()"), this->clicked())
d.exec();
}

amleto
29th April 2012, 20:33
"connect(d.pushButton, SIGNAL("clicked()"), this->clicked())"
seriously?

read a tutorial on signals and slots.

sedi
30th April 2012, 00:20
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

public slots:
void yourSlotName();
and implement this in the cpp file.
Now connect the self-made slot with the pushButton's built-in clicked() signal:

connect(d.pushButton, SIGNAL(clicked()), this, SLOT(yourSlotName());
[N.B. you should not try to create a slot named "clicked()", because that is a built-in signal of almost everything in Qt.]