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;
((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
#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
#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
To copy to clipboard, switch view to plain text mode
#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() );
}
#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() );
}
To copy to clipboard, switch view to plain text mode
and the mainwindow :
mainwindow.png
#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
#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
To copy to clipboard, switch view to plain text mode
#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?
}
#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(QListWidgetItem *item)
{
// how do i access dlg->ui->leStringToAdd widget?
}
To copy to clipboard, switch view to plain text mode
thanks in advance!
Bookmarks