Results 1 to 20 of 21

Thread: MVC SQL question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: MVC SQL question

    Well, the controller created the model instance, but you could as easily pass the externally constructed model to it.

    Cheers,
    _

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

    cpuinthebrain (28th July 2015)

  3. #2
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: MVC SQL question

    I still need an example, sorry , but the mess is still in my head

  4. #3
    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: MVC SQL question

    Lets assume for simplicity that the controller creates and owns the model (but you can make it setable with a bit of more effort)

    Qt Code:
    1. class Controller : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Controller(QObject *parent = 0);
    6.  
    7. QSqlQueryModel *model() const { return m_model; }
    8.  
    9. void setView(QTableView *view);
    10.  
    11. public slots:
    12. void updateModel();
    13.  
    14. private:
    15. QSqlQueryModel *m_model;
    16.  
    17. private slots:
    18. void onCellClicked(const QModelIndex &index);
    19. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Controller::Controller(QObject *parent)
    2. : QObject(parent)
    3. , m_model(new QSqlQueryModel(this))
    4. {
    5. }
    6.  
    7. void Controller::setView(QTableView *view)
    8. {
    9. connect(view, SIGNAL(clicked(QModelIndex)), this, SLOT(onCellClicked(QModelIndex)));
    10.  
    11. // could also install a custom delegate here and connect to it
    12. }
    13.  
    14. void Controller::updateModel()
    15. {
    16. m_model->setQuery(...);
    17. }
    18.  
    19. void Controller::onCellClicked(const QModelIndex &index)
    20. {
    21. // do whatever needs to be done
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: MVC SQL question

    This is what i can't understand.
    Why the controller should contain the model?You mean because in our case the model is the DataBase?If it was ORM or QObject it would be in different class, right?

    By the way many thanks for your time and for the example!

  6. #5
    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: MVC SQL question

    As I wrote: for simplicity of the example.

    Qt Code:
    1. class Controller : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Controller(QSqlQueryModel *model, QObject *parent = 0);
    6. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Controller::Controller(QSqlQueryModel *model, QObject *parent)
    2. : QObject(parent)
    3. , m_model(model)
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #6
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: MVC SQL question

    Didn't understand, am i right that the model is a separate class when we have ORM and when using core SQL we can skip it because the controller connect the DB and the view the same way.

  8. #7
    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: MVC SQL question

    The model is a separate class in my example, is it not?

    Whether you separate model and controller is always your choice.

    Cheers,
    _

Similar Threads

  1. rcc question
    By mojo2000 in forum Newbie
    Replies: 13
    Last Post: 18th November 2009, 22:26
  2. Qwt question
    By baray98 in forum Qwt
    Replies: 3
    Last Post: 26th November 2007, 20:31
  3. MVC question
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2007, 19:45
  4. GUi or Not GUI - that is the question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2007, 18:19

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.