2 Attachment(s)
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
Code:
((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 :
Quote:
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 :
Attachment 7604
Code:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "mainwindow.h"
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
Ui::Dialog *ui;
private slots:
void on_pushButton_clicked();
private:
};
#endif // DIALOG_H
Code:
#include "dialog.h"
#include "ui_dialog.h"
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 :
Attachment 7605
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStringList>
#include <QListWidgetItem>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void on_btnAdd_clicked();
private:
Ui::MainWindow *ui;
Dialog *dlg;
};
#endif // MAINWINDOW_H
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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();
}
{
// how do i access dlg->ui->leStringToAdd widget?
}
thanks in advance!
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.
Code:
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*).
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 ;)
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::on_teacher_button_clicked()
{
Dialog d;
d.setModal(true);
connect(d.pushButton, SIGNAL("clicked()"), this->clicked())
d.exec();
}
Re: how can dialog access variable from parent MainWindow?
"connect(d.pushButton, SIGNAL("clicked()"), this->clicked())"
seriously?
read a tutorial on signals and slots.
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
Code:
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:
Code:
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.]