Results 1 to 4 of 4

Thread: Initialising a dialog with the MainWindow pointer

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Initialising a dialog with the MainWindow pointer

    Hello,
    In my application which has a MainWindow, I allocate a whole lot of Dialogs and for each of these, I want to have a m_mainwindow pointer of type MainWindow *.
    Now when I make a dialog via the wizard add new > Qt > Qt Designer Form class
    it creates a constructor for the dialog which takes an argument of type QWidget *, when in fact this QWidget pointer is going to be passed my MainWindow pointer. So I end up defining a method of signature void init(MainWindow *) for each of my dialog. It kind of feels like a waste to have to define an extra method when I have passed the MainWindow (albeit as
    a QWidget in the constructor).
    I dont like the idea of dynamically recasting the QWidget pointer to a MainWindow pointer in the constructor.
    I am wondering about whether it is possible to change or configure the wizard that gets called when I add a new Qt Designer Form Class.
    Should I be looking into adding a new Template for the wizard?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Initialising a dialog with the MainWindow pointer

    Aside from the fact that what you are proposing almost certainly is a case of bad design (TM), and that you should instead be thinking in terms of "What kinds of signals and slots do I need to implement for MainWindow and my dialogs so they can communicate the right information between themselves", the Qt wizards are simply a tool for generating boilerplate C++ and UI code that you then modify to flesh out the classes.

    Open the cpp and h files in the code editor and just replace QWidget * with MainWindow * in the constructors (or add them as additional arguments). There's no magic here. You'll probably need to add a forward declaration ("class MainWindow;") in the dialog .h file unless you add a #include for MainWindow.h to avoid an undefined type error during compilation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Initialising a dialog with the MainWindow pointer

    Thankyou d_strantz for replying with a clear answer.
    I worked for a company that had a design style where there was a "central" object that owned all the shared services like the database connection, and objects would ask this one directly for the services.
    I'll consider the idea that dialogs etc do not know where information is stored, they just emit signals saying "i need this kind of information" and the connections created (for example in the MainWindow) decide who shall supply what information.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Initialising a dialog with the MainWindow pointer

    I'll consider the idea that dialogs etc do not know where information is stored, they just emit signals saying "i need this kind of information" and the connections created (for example in the MainWindow) decide who shall supply what information.
    That would be an excellent choice. Even if there is a "Master of the Universe", using signals and slots can hide all of that. You might also think about the idea that the MainWindow might not be the master either - it could create a master object, and connect the dialog signals and slots to that object instead of handling them itself. That will help give you a system where all of the business logic is as separate as possible from the GUI logic so that changes in the GUI will not force you into a redesign of the business logic.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. about dialog and mainwindow (ui,,ui)
    By Newbit in forum Newbie
    Replies: 0
    Last Post: 28th October 2014, 08:41
  2. Using Variables From Dialog In MainWindow
    By steadi in forum Newbie
    Replies: 2
    Last Post: 7th October 2012, 23:18
  3. How to call a dialog from a mainwindow
    By luiz4um in forum Qt Programming
    Replies: 26
    Last Post: 29th June 2010, 10:41
  4. Pointer to MainWindow
    By ThomasBrown in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2010, 04:49
  5. Communication between MainWindow and a dialog
    By Backslash in forum Newbie
    Replies: 9
    Last Post: 3rd August 2007, 04:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.