Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a way to access a member in a QMainWindow from a QDialog
Qt 6.11 is out! See what's new in the release blog

Is there a way to access a member in a QMainWindow from a QDialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 773 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bigguinessB Offline
    bigguinessB Offline
    bigguiness
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • bigguinessB Offline
      bigguinessB Offline
      bigguiness
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • bigguinessB bigguiness

        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.

        M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #3

        @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.

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved