Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: MVC SQL question

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

    Default MVC SQL question

    I'd like to make simple invoice app.
    I use SQLite for database.
    I can't find similar example of MVC structured app using SQL(QsqlQuery) without ORM.

    What i can't understand is how the model class should looks like?
    Should be there some class named Model for instance that simply has functions which returns data with different queries?

    For instance:

    Model

    Qt Code:
    1. QSqlQueryModel Model::getPartners(){
    2.  
    3. QSqlQueryModel *model = new QSqlQueryModel(this);
    4. model->setQuery("SELECT ID, Name, City, Address, Bulstat FROM partners");
    5. return model;
    6.  
    7. }
    8.  
    9.  
    10. void Model::addNewPartner(QString name,QString city, QString address, QString mol, QString bulstat, QString vat, QString phone, QString email){
    11.  
    12. QSqlQuery query;
    13. query.prepare("INSERT INTO partners .............
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: MVC SQL question

    Yes, that does look like a valid approach.

    Model might want to keep the pointer to the QSqlQueryModel as a member though, so that it can update it after an INSERT.

    Cheers,
    _

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

    Default Re: MVC SQL question

    How in this case showld look the controller and the view?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MVC SQL question

    Your Model class looks like a controlller, the view could be something like a QTableView or QDataWidgetMapper.

    Cheers,
    _

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

    Default Re: MVC SQL question

    this is why i need an example

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

    Default Re: MVC SQL question

    I'm trying to make a simple working example of MVC, but i miss the part of connecting signal-slots from external classes, in this case it should be implemented in the controller.PLEASE, HELP me to make this working and let me know whether this is the right approach:

    The Controller
    Qt Code:
    1. #include "controller.h"
    2. #include <mainwindow.h>
    3. #include <modelpartner.h>
    4.  
    5. Controller::Controller()
    6. {
    7.  
    8. QObject::connect(&MainWindow, SIGNAL(addItem(int)),
    9. &ModelPartner, SLOT(add()));
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    The View
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::on_pushButton_2_clicked()
    17. {
    18. //add item
    19. emit addItem(1);
    20.  
    21. }
    22.  
    23. void MainWindow::on_pushButton_clicked()
    24. {
    25. //remove item
    26. }
    To copy to clipboard, switch view to plain text mode 

    The Model

    Qt Code:
    1. #include "modelpartner.h"
    2. #include <QSqlQueryModel>
    3. #include <QStringList>
    4. #include <QDebug>
    5.  
    6. ModelPartner::ModelPartner()
    7. {
    8.  
    9.  
    10.  
    11.  
    12. }
    13.  
    14.  
    15. QStringList *ModelPartner::partners(){
    16.  
    17. QStringList theList;
    18. theList << "hello";
    19. return &theList;
    20. }
    21.  
    22.  
    23. void ModelPartner::add(){
    24.  
    25. qDebug() << "added";
    26. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

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

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

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

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

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

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

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

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

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

    cpuinthebrain (28th July 2015)

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

  18. #17
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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 

  19. #18
    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!

  20. #19
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

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

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.