PDA

View Full Version : Function acces in mainwindow class from a dialog class?



realdarkman71
20th August 2011, 13:57
Hi all,

I have the following classes:

MainWindow class:


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
QString someData();
};

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

QString MainWindow::someData() {
return "...";
}


Dialog class:


namespace Ui {
class Dialog;
}

class Dialog : public QDialog {
Q_OBJECT

void anyFunction();

public:
explicit Dialog(QWidget *parent = 0);
};

Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) {
ui->setupUi(this);
}

void Dialog::anyFunction() {
qDebug() << MainWindow->someData();
}


In the Dialog class (row 19.): how can I access the someData() function from MainWindow?

Thanks in advance!
Chris

wysota
20th August 2011, 15:04
You need a pointer to MainWindow instance.

realdarkman71
20th August 2011, 16:09
That is obvious to me, but how I surrender the pointer?

wysota
20th August 2011, 16:19
I assume you are calling the dialog from within the main window, so you can call some custom method that will feed this pointer to the dialog. However if the goal of your MainWindow::someData() is to provide data for the dialog, it is better to add a method to the dialog that allows to pass (and store) the data object directly from the window when the dialog gets called.

realdarkman71
23rd August 2011, 21:53
Ok, thanks for your help!