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. How to get main window from dialog?
QtWS25 Last Chance

How to get main window from dialog?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.5k Views
  • 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 LarssonH Offline
    Hanna LarssonH Offline
    Hanna Larsson
    wrote on last edited by
    #1

    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!

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      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
      3
      • Hanna LarssonH Hanna Larsson

        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

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

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

        • Login

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