Results 1 to 6 of 6

Thread: Model-view-controller example?

  1. #1
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Model-view-controller example?

    Hello,

    Does anyone have Qt-based MVC examples to share?

    It'd be a great addition to the "official" examples ... perhaps a tutorial.

    Cheers,
    Ben

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Model-view-controller example?

    you could try the Links on top of this page. There is a lot of non official QT stuff to enjoy

    What is it you want to learn/do exactly? .

    Cheers

  3. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Model-view-controller example?

    you could try the Links on top of this page. There is a lot of non official QT stuff to enjoy
    I'll do that ... still navigating my way through this forum.

    What is it you want to learn/do exactly? .
    Specifically, I want to get my hands around MVC and QT. I have a cursory knowledge of Qt ... a somewhat better understanding of MVC. I believe MVC is one of today's best practices for developing robust GUI applications. It just seems like it should be an "official" tutorial ... in my humble opinion that is.

    I've seen MVC done a few ways ... subject/observer, listener pattern, etc. However, I'm still wrestling with issues/limitations of different approaches ... and was hoping others might have observations to share.

  4. #4

    Default Re: Model-view-controller example?

    Here is simple model view controller code, which will show in multiple views

    QFileSystemModel* model = new QFileSystemModel;
    model->setRootPath( QDir::root().path() );

    ui->listView->setModel(model);
    ui->treeView->setModel(model);
    ui->tableView->setModel(model);
    ui->columnView->setModel(model);

    You need to use signals and slots for communications between model/view

  5. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model-view-controller example?

    A slightly dated version of the official QT book is available online, the section dealing with models is pretty good:

    http://qt.nokia.com/developer/gettin...he-official-qt

  6. #6
    Join Date
    Oct 2015
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Model-view-controller example?

    Check my code can be called Model View Delegate?

    ModView.pro
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = ModView
    6. TEMPLATE = app
    7.  
    8.  
    9. SOURCES += main.cpp\
    10. dialog.cpp
    11.  
    12. HEADERS += dialog.h
    13.  
    14. FORMS += dialog.ui
    To copy to clipboard, switch view to plain text mode 
    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6. #include <QtCore>
    7.  
    8. namespace Ui {
    9. class Dialog;
    10. }
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Dialog(QWidget *parent = 0);
    18. ~Dialog();
    19.  
    20. private slots:
    21. void insertData();
    22.  
    23. void updateData();
    24.  
    25. void deleteData();
    26.  
    27. private:
    28. Ui:: Dialog *ui;
    29. };
    30.  
    31. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog:: Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui:: Dialog)
    7. {
    8. ui->setupUi(this);
    9. model = new QStringListModel(this);
    10. model->setStringList(list);
    11. ui->listView->setModel(model);
    12. ui->comboBox->setModel(model);
    13.  
    14. ui->listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView:: DoubleClicked);
    15.  
    16. connect(ui->insert, SIGNAL(clicked()), this, SLOT(insertData()));
    17. connect(ui->update, SIGNAL(clicked()), this, SLOT(updateData()));
    18. connect(ui->delete_2, SIGNAL(clicked()), this, SLOT(deleteData()));
    19. }
    20.  
    21. Dialog::~Dialog()
    22. {
    23. delete ui;
    24. }
    25.  
    26. void Dialog::insertData()
    27. {
    28. int row = model->rowCount();
    29. model->insertRows(row ,1);
    30.  
    31. QModelIndex index = model->index(row);
    32. ui->listView->setCurrentIndex(index);
    33. ui->listView->edit(index);
    34. }
    35.  
    36. void Dialog::updateData()
    37. {
    38. int row = ui->listView->currentIndex().row();
    39. model->insertRows(row, 0);
    40. QModelIndex index = model->index(row);
    41. ui->listView->setCurrentIndex(index);
    42. ui->listView->edit(index);
    43. }
    44.  
    45. void Dialog:: deleteData()
    46. {
    47. int row = ui->listView->currentIndex().row();
    48. model->removeRows(row, 1);
    49. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Dialog w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 25th November 2015 at 08:03. Reason: missing [code] tags

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Help with Model View
    By weepdoo in forum Qt Programming
    Replies: 13
    Last Post: 12th October 2007, 10:32
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

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.