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

    I am afraid I don't understand what you are looking for.
    Your initial approach looked fine!

    Cheers,
    _

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

    Default Re: MVC SQL question

    My initial example's model is looking like controller.Probably i shoold make separate model class for every table from the sql.I can't belive that there is no example in the net.
    My view and controller are not like they should be, but i don't know how to connect them in the shown example.

  3. #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

    There are plenty of QSql examples, quite some of them using models http://doc.qt.io/qt-5/sql-examples.html

    if you don't want to use the models from the QSql module, have a look at the model/view documentation:
    http://doc.qt.io/qt-5/modelview.html
    http://doc.qt.io/qt-5/model-view-programming.html

    Cheers,
    _

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

    Default Re: MVC SQL question

    Dear @anda_skoa

    Before ask here i observe all possible in Qt documentation and in internet.
    I have reviewed these examples, but as i see they are not pure MVC, or i'm wrong?!
    What about this example http://doc.qt.io/qt-5/qtsql-querymodel-example.html
    Is this pure MVC?It seems that in this case the main.cpp plays the role of controller?Is this the right approach, the controller to be the main class?
    This is similar to my first approach.

  5. #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

    Well, that's up to you.
    The example isn't that complex so its author likely decided against an explicit controller class.

    Doesn't mean you can't do that, i.e. like you had in your first posting in this thread.

    But if you like you can also make the controller part of the model class, or spread it across, model, view and delegate.

    Cheers,
    _

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

    Default Re: MVC SQL question

    i really need an example of real MVC construction.I also read that the view should emit only signals.... and the controller take the other part.Nothing more the view shuld have.I'm very confuse , really!

  7. #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

    I really don't understand what you are looking for.

    You have view and model. So any class that reacts to the signals of the view and manipulates the model, or the data the model is working on, is the controller.

    Which is what you had in your first posting, no?

    Cheers,
    _

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

    Default Re: MVC SQL question

    inmy first code i mixed the model with the controller

  9. #9
    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,
    _

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

    cpuinthebrain (28th July 2015)

  11. #10
    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

  12. #11
    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 

  13. #12
    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!

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.