Results 1 to 20 of 23

Thread: Use "extern QStandardItemModel*" to provide access from anywhere?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Use "extern QStandardItemModel*" to provide access from anywhere?

    I'd go for more of a container solution. Rather than having the customers, articles, invoices, etc polluting the global namespace, create a management class for them and then pass a reference to this management class in the constructor of your other classes so they may use it to access the data they need.

    I certainly wouldn't use global variables for the task. You start with a single global, and then it multiplies, and then before you know it you have code that isn't maintainable or reusable easily in other projects (you have to have knowledge of the application as a whole, rather than just specific classes)

  2. The following user says thank you to squidge for this useful post:

    homerun4711 (31st December 2010)

  3. #2
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Use "extern QStandardItemModel*" to provide access from anywhere?

    Thanks for the anwers. I think I go for that container solution.

    Is there some example code around for dealing with models in that way?

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Use "extern QStandardItemModel*" to provide access from anywhere?

    Something like: [pseudo code]

    Qt Code:
    1. class DataManager
    2. {
    3. public:
    4. DataManager(); // Remember to set m_customerModel etc to NULL in constructor
    5. ~DataManager();
    6. void SetCustomerModel(QStandardItemModel *customerModel);
    7. void SetInvoiceModel(QStandardItemModel *invoiceModel);
    8. #ifdef DEBUG
    9. QStandardItemModel *customerModel()
    10. {
    11. if (m_customerModel == NULL) throw E_NULLPOINTEREXCEPTION;
    12. return m_customerModel;
    13. }
    14. #else
    15. inline QStandardItemModel *customerModel() {return m_customerModel};
    16. inline QStandardItemModel *invoiceModel() {return m_invoiceModel};
    17. #endif
    18. private:
    19. QStandardItemModel *m_customerModel;
    20. QStandardItemModel *m_invoiceModel;
    21. };
    22.  
    23. DataManager *dmgr = new DataManager();
    24. dmgr->SetCustomerModel (myModel);
    25. [...]
    26. myNewClass = new someclass(dmgr);
    27. [...]
    28. dmgr->customerModel()->doSomething(); or pass dmgr->customerModel() pointer to some other function or class.
    29. If you added another pointer to the class and forget to call the set(), it'll just throw an exception to remind you.
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to squidge for this useful post:

    homerun4711 (31st December 2010)

  6. #4
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Use "extern QStandardItemModel*" to provide access from anywhere?

    Thank you very much. I already wrote some lines on this also called my class DataManager

    Kind regards,
    HomeR

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 21:46
  2. Replies: 3
    Last Post: 15th February 2010, 17:27
  3. Replies: 2
    Last Post: 22nd October 2009, 08:22
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. Access an "embedded" MySQL-Database
    By Lykurg in forum Newbie
    Replies: 10
    Last Post: 21st February 2006, 10:28

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
  •  
Qt is a trademark of The Qt Company.