Results 1 to 6 of 6

Thread: Programming desgin

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

    Default Programming desgin

    Hello,

    I have a class Document and a class Form. I want Document class to have access to form class.
    These are my two approaches -

    Approach 1:
    Approach 1 project found here.
    mainwindow.cpp
    Qt Code:
    1. ...
    2. // Inside MainWindow init method
    3. Document* document = new Document();
    4. setCentralWidget(document->currentForm());
    5. ...
    To copy to clipboard, switch view to plain text mode 

    document.cpp
    Qt Code:
    1. ...
    2. Document::Document()
    3. {
    4. form = new Form();
    5. }
    6.  
    7. Form* Document::currentForm()
    8. {
    9. return form;
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 

    Approach 2:
    Approach 2 project found here.
    mainwindow.cpp
    Qt Code:
    1. ...
    2. // Inside MainWindow init method
    3. Form* form = new Form();
    4. setCentralWidget(form);
    5. Document* document = new Document();
    6. document->setCurrentForm(form);
    7. ...
    To copy to clipboard, switch view to plain text mode 

    document.cpp
    Qt Code:
    1. ...
    2. void Document::setCurrentForm(Form* currentForm)
    3. {
    4. form = currentForm;
    5. }
    6. ...
    To copy to clipboard, switch view to plain text mode 

    which design should I use?
    I want to follow Qt's design patterns.

    Thanks
    Last edited by scarecr0w132; 1st January 2015 at 05:53.

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

    Default Re: Programming desgin

    I would pass the document to the form, i.e. make the UI depend on the data/logic.

    Something like
    Qt Code:
    1. Document *document = new Document;
    2. Form *form = new Form(document);
    3. // or
    4. form->setDocument(document);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    scarecr0w132 (1st January 2015)

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

    Default Re: Programming desgin

    Thanks anda_skoa

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,938
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Programming desgin

    I would pass the document to the form, i.e. make the UI depend on the data/logic.
    I would even take this a step further and make the coupling between document and form as loose as possible. I would set it up so that the document knows absolutely nothing about the form; instead, when something in the document changes that the UI should know about, the document should emit a signal that tells about the change. Likewise, it might be better if the form also doesn't know anything about the document - it listens for signals about what has changed, and in turn, if the user edits something one the form that should be saved in the document, the form should emit a signal containing the changed items.

    With this kind of loose connection, the form doesn't need a document pointer, nor does the document need a form pointer. Something in the middle (the MainWindow, for example) sets up all the connections between document and form signals / slots. I think you could classify this as an example of a bridge pattern in design patterns parlance.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Programming desgin

    Such a fully disconnected setup does not always work and even if it works it can make it very complex.

    It basically becomes fully asynchronous, e.g. if the form needs something it has to emit a request signal and doesn't know when or even if the document is going to answer.
    A bit like a web app, but in a single local process.

    A one directional dependency can make things considerably easier to code and understand, e.g. like Qt's views and models.
    The view (and delegates) can make direct calls the model but the model doesn't know anything about the view (or delegates).

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    d_stranz (2nd January 2015)

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,938
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Programming desgin

    Yes, this can also be true. Decoupling to this extent almost always comes at the cost of higher complexity. In simple apps it is probably overkill.

    I find decoupling like this to be most useful when the UI is large, with many views and dialogs. These don't all need to know about the details of the underlying "document"; in fact most of them only deal with a small part of the document's contents. So in general, MainWindow might be the only part of the UI that knows all about the document; when it needs to post a dialog, it pulls out the relevant subset of information, initializes the dialog with it, and then pushes the changed information back into the document at the end. If the document or dialog change, MainWindow is the only one who needs to know the details.

Similar Threads

  1. Qt C++ CGI programming
    By karankumar1609 in forum General Programming
    Replies: 7
    Last Post: 4th October 2019, 12:56
  2. MMX Programming
    By vhptt in forum Newbie
    Replies: 3
    Last Post: 19th November 2013, 05:10
  3. Is this a QT programming?
    By knuxie1135 in forum Newbie
    Replies: 1
    Last Post: 7th November 2012, 16:09
  4. Qt programming
    By archanasubodh in forum Installation and Deployment
    Replies: 2
    Last Post: 6th March 2008, 08:55
  5. QT COM Programming
    By sarav in forum Newbie
    Replies: 5
    Last Post: 24th February 2007, 13:41

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.