Is there a way to access a member in a QMainWindow from a QDialog
-
Hello all,
The topic is probably not clear but I was not sure how to word it...
I have a app that uses a QMainWindow. In the class I have a member variable, like this:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget* parent = nullptr); ... void createMyDialog(); ... MyData* data; // this is the member I want to access ... };The MainWindow can create a number of QDialog objects. All of them are pretty typical, but some need to access the MainWindow::data member:
class MyDialog : public QDialog { Q_OBJECT public: MyDialog(QWidget* parent = nullptr); ... void someMethod(); // this is the method I want to access the MainWindow::data member ... };The MainWindow creation is typical in main():
QApplication app(argc, argv); MainWindow window(); window.show(); return app.exec();The dialog creations are also pretty straight forward:
void MainWIndow::createMyDialog() { MyDialog* dlg = new MyDialog(this); dlg->exec(); }In the dialog I have a need for the data that was created in the MainWindow:
void MyDialog::someMethod() { MyData* data = ?; // how can I get the MainWindow:data // do something with the data }I know I can pass MyData as a parameter when creating MyDialog but was wondering if there is some other way to get it, maybe by using the QWidget* parent. Is this possible? I have one dialog that is deeper than one level away from the MainWindow. I was trying to avoid having to pass the socket to each level:
MainWindow -> SomeDialog -> AnotherDialog -> FinalDialog (this one needs MyData, the 2 above do not)Thanks for any insight.
-
Well this worked, not sure if it's the right way to do it...
class MainWindow : public QMainWindow ( Q_OBJECT public: MainWindow(QWidget* parent = nullptr); ... MyData* getData() { return data; } ... private: MyData* data; };void MyDialog::someMethod() { MyData* data = qobject_cast<MainWindow *>(this->parentWidget())->getData(); ... }It's a bit annoying to have to include the header for MainWindow but it seems cleaner than passing the MyData as parameter to all the dialog constructors.
I still need to check if it works for a dialog that is deeper than one parent away from the MainWindow. I'm guessing I will need the correct number of parentWidget() to get to the MainWindow.
void FinalDialog::someMethod() { // 1st parent is AnotherDialog // 2nd parent is SomeDialog // 3rd parent is MainWindow MyData* data = qobject_cast<MainWindow *>(this->parentWidget()->parentWidget()->parentWidget())->getData(); ... }Regards
-
Hello all,
The topic is probably not clear but I was not sure how to word it...
I have a app that uses a QMainWindow. In the class I have a member variable, like this:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget* parent = nullptr); ... void createMyDialog(); ... MyData* data; // this is the member I want to access ... };The MainWindow can create a number of QDialog objects. All of them are pretty typical, but some need to access the MainWindow::data member:
class MyDialog : public QDialog { Q_OBJECT public: MyDialog(QWidget* parent = nullptr); ... void someMethod(); // this is the method I want to access the MainWindow::data member ... };The MainWindow creation is typical in main():
QApplication app(argc, argv); MainWindow window(); window.show(); return app.exec();The dialog creations are also pretty straight forward:
void MainWIndow::createMyDialog() { MyDialog* dlg = new MyDialog(this); dlg->exec(); }In the dialog I have a need for the data that was created in the MainWindow:
void MyDialog::someMethod() { MyData* data = ?; // how can I get the MainWindow:data // do something with the data }I know I can pass MyData as a parameter when creating MyDialog but was wondering if there is some other way to get it, maybe by using the QWidget* parent. Is this possible? I have one dialog that is deeper than one level away from the MainWindow. I was trying to avoid having to pass the socket to each level:
MainWindow -> SomeDialog -> AnotherDialog -> FinalDialog (this one needs MyData, the 2 above do not)Thanks for any insight.
@bigguiness said in Is there a way to access a member in a QMainWindow from a QDialog:
I know I can pass MyData as a parameter when creating MyDialog
That's the way to do it.
but was wondering if there is some other way to get it, maybe by using the QWidget* parent. Is this possible?
Yes, but considered as bad pratice.
MainWindow -> SomeDialog -> AnotherDialog -> FinalDialog
multi dialogs Windows-ish awfulness ...
use QWizard instead.MyDialog* dlg = new MyDialog(this);
dlg->exec();You create a new dialog each time, if delete on close is not set, it's a memory leak. Just create the dialog on the stack since by calling exec(), your dialog is modal.