Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to get main window from dialog?

    General and Desktop
    3
    3
    1266
    Loading More Posts
    • 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.
    • Hanna Larsson
      Hanna Larsson last edited by

      Hi!

      I have a dialog with a buttonbox, OK and Cancel, and I want the OK-button to change the index of my Stacked Widget that is placed in my main window. How do I get the main window from my dialog?

      Hanna

      Bonjour!

      kshegunov 1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by VRonin

        The correct way would be to make the dialog emit a signal and let the main window take care of changing the index.

        anyway if you passed the mainwindow as parent in the constructor dialog you could get it via qobject_cast<MainWindowType*>(parent()) method

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply Reply Quote 3
        • kshegunov
          kshegunov Moderators @Hanna Larsson last edited by kshegunov

          @Hanna-Larsson
          Hello,
          As @VRonin said you can retrieve it from the QObject parent property. However for the sake of design, you shouldn't do it in the first place. As he hinted, the QDialog should know nothing of the outside world (consequently of the MainWindow) it should rather "notify" others of things that happened (with signals).

          This way you also ensure you can use your dialog with a QWidget parent, of with QLineEdit parent etc., which is not true if the dialog knows it's owned by a QMainWindow object.
          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply Reply Quote 3
          • First post
            Last post