Results 1 to 3 of 3

Thread: Best way for a parent class to know value chosen on child class

  1. #1
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Best way for a parent class to know value chosen on child class

    Hello,

    I have a MainWindow class, on an action I create a new project type selector.

    MainWindow.cpp
    Qt Code:
    1. ...
    2. void MainWindow::on_actionFileNew_triggered()
    3. {
    4. NewDocumentSelector newdocumentselector;
    5. newdocumentselector.exec();
    6. }
    7. ...
    To copy to clipboard, switch view to plain text mode 

    I want MainWindow to know the type selected
    Should I
    1) Emit a signal in NewDocumentSelector and connect it to MainWindow.
    or
    2) Pass MainWindow into NewDocumentSelector on initilization,
    Qt Code:
    1. NewDocumentSelector newdocumentselector(MainWindow);
    To copy to clipboard, switch view to plain text mode 
    which will then be used in NewDocumentSelector as
    Qt Code:
    1. MainWindow.thisTypeSelected();
    To copy to clipboard, switch view to plain text mode 
    or
    Is there another way? I want to follow Qt's MV pattern.
    In other words I want to access parent class instance.
    I have attached my project if you require more detail.

    Thanks
    Attached Files Attached Files
    Last edited by scarecr0w132; 4th October 2014 at 03:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way for a parent class to know value chosen on child class

    I would just call a getter on the dialog after exec()

    Qt Code:
    1. if (newdocumentselector.exec() == QDialog::Accepted) {
    2. m_type = newdocumentselector.type();
    3. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    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: Best way for a parent class to know value chosen on child class

    Or you could follow the model used by QFileDialog: emit signals whenever the user changes something the application might be interested in (like the current directory or the file extension). In your case, implement signals in the NewDocumentSelector class that are emitted when something interesting happens, and implement slots in MainWindow to listen for them. So when the user selects a new type, NewDocumentSelector emits some signal like "documentTypeChanged( DocType newType )". In your MainWindow class, you create a slot called "onDocumentTypeChanged( DocType newType )" and make a connection between the signal and slot.

    I use anda_skoa's method whenever I need to retrieve a lot of information from a dialog (like a whole page of parameters). I package the parameters into a data structure and use a setter / getter combination to set the initial values and to retrieve the changes.

    I use the QFileDialog method for simple parameters or when I want to dynamically keep track of what the user is doing in the dialog. (For example, I can dynamically change the color of something by listening for QColorDialog::currentColorChanged() to show the user what effect it will have while they are browsing the color selections).

Similar Threads

  1. Replies: 4
    Last Post: 4th March 2014, 19:28
  2. How to access to methods of parent class
    By tonnot in forum General Programming
    Replies: 3
    Last Post: 29th June 2011, 07:04
  3. Replies: 4
    Last Post: 16th March 2011, 17:12
  4. No CSS in child class
    By kartun in forum Newbie
    Replies: 0
    Last Post: 2nd February 2011, 10:01
  5. Determine Class Type of QObject Parent
    By photo_tom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 17:42

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.