Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Example MVC in Qt C++
Forum Updated to NodeBB v4.3 + New Features

Example MVC in Qt C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 2 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Renn
    wrote on 23 Nov 2015, 01:21 last edited by
    #1

    I want to ask about MVC in Qt C++
    Can anyone give an example of a very simple MVC to me? I want to learn MVC in Qt C++

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 23 Nov 2015, 05:39 last edited by
      #2

      Take a look at this introduction:
      http://doc.qt.io/qt-5.5/model-view-programming.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply 23 Nov 2015, 10:01
      0
      • J jsulm
        23 Nov 2015, 05:39

        Take a look at this introduction:
        http://doc.qt.io/qt-5.5/model-view-programming.html

        R Offline
        R Offline
        Renn
        wrote on 23 Nov 2015, 10:01 last edited by
        #3

        @jsulm Qt means no MVC, but the Model View and Delegate?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 23 Nov 2015, 10:11 last edited by
          #4

          Yes, that's the Qt way to do it

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply 24 Nov 2015, 02:06
          0
          • J jsulm
            23 Nov 2015, 10:11

            Yes, that's the Qt way to do it

            R Offline
            R Offline
            Renn
            wrote on 24 Nov 2015, 02:06 last edited by
            #5

            @jsulm ModelViewDelegate in one file if it can be called ModelViewDelegate?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 24 Nov 2015, 05:38 last edited by
              #6

              Why in one file? You should separate the model, view and delegate even on file level.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply 24 Nov 2015, 07:59
              0
              • J jsulm
                24 Nov 2015, 05:38

                Why in one file? You should separate the model, view and delegate even on file level.

                R Offline
                R Offline
                Renn
                wrote on 24 Nov 2015, 07:59 last edited by
                #7

                @jsulm I need an example of a project so that I was not confused

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 24 Nov 2015, 08:02 last edited by
                  #8

                  On the side I provided you there are links to examples:
                  http://doc.qt.io/qt-5.5/model-view-programming.html#related-examples

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply 24 Nov 2015, 09:51
                  0
                  • J jsulm
                    24 Nov 2015, 08:02

                    On the side I provided you there are links to examples:
                    http://doc.qt.io/qt-5.5/model-view-programming.html#related-examples

                    R Offline
                    R Offline
                    Renn
                    wrote on 24 Nov 2015, 09:51 last edited by
                    #9

                    @jsulm Code like this can be called Model View Delegate?

                    MVC.pro
                    QT += core gui

                    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

                    TARGET = MVC
                    TEMPLATE = app

                    SOURCES += main.cpp
                    mvc.cpp
                    model.cpp

                    HEADERS += mvc.h
                    model.h

                    FORMS += mvc.ui

                    model.h

                    #ifndef MODEL_H
                    #define MODEL_H

                    #include <QDialog>

                    class Model : public QDialog
                    {
                    Q_OBJECT

                    public:
                    explicit Model(QWidget *parent = 0);

                    signals:
                    void sendFromData(int);

                    private slots:
                    void receiveData(QStringList);

                    public slots:

                    private:
                    int hitung1, hitung2;

                    };

                    #endif // MODEL_H

                    mvc.h

                    #ifndef MVC_H
                    #define MVC_H

                    #include <QMainWindow>
                    #include "model.h"

                    namespace Ui {
                    class Mvc;
                    }

                    class Mvc : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit Mvc(QWidget *parent = 0);
                    ~Mvc();

                    private slots:
                    void on_hitungAngka_clicked();

                    void receiveFromData(int);
                    

                    signals:
                    void sendData(QStringList);

                    private:
                    Ui::Mvc *ui;

                    Model *model;
                    
                    int angkaPertama, angkaKedua;
                    

                    };

                    #endif // MVC_H

                    main.cpp

                    #include "mvc.h"
                    #include <QApplication>

                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    Mvc w;
                    w.show();

                    return a.exec();
                    

                    }

                    model.cpp

                    #include "model.h"
                    #include <QDebug>

                    Model::Model(QWidget *parent) :
                    QDialog(parent)
                    {
                    }
                    void Model::receiveData(QStringList sl)
                    {
                    int hasil;
                    hitung1 = sl[0].toInt();
                    hitung2 = sl[1].toInt();
                    hasil = hitung1 + hitung2;
                    emit sendFromData(hasil);
                    }

                    mvc.cpp

                    #include "mvc.h"
                    #include "ui_mvc.h"
                    #include <QDebug>

                    Mvc::Mvc(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::Mvc)
                    {
                    ui->setupUi(this);
                    }

                    Mvc::~Mvc()
                    {
                    delete ui;
                    }

                    void Mvc::on_hitungAngka_clicked()
                    {
                    model = new Model(this);
                    QStringList sl;
                    sl.append(ui->input1->text());
                    sl.append(ui->input2->text());
                    connect(this, SIGNAL(sendData(QStringList)), model, SLOT(receiveData(QStringList)));
                    connect(model, SIGNAL(sendFromData(int)), this, SLOT(receiveFromData(int)));
                    emit sendData(sl);
                    }

                    void Mvc::receiveFromData(int hasil)
                    {
                    qDebug() << hasil;
                    }

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 24 Nov 2015, 09:59 last edited by
                      #10

                      No:
                      Model has to be a subclass of QAbstractItemModel (see http://doc.qt.io/qt-4.8/model-view-programming.html#model-classes).
                      Not sure why you subclass QMainWindow.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      R 3 Replies Last reply 24 Nov 2015, 10:08
                      0
                      • J jsulm
                        24 Nov 2015, 09:59

                        No:
                        Model has to be a subclass of QAbstractItemModel (see http://doc.qt.io/qt-4.8/model-view-programming.html#model-classes).
                        Not sure why you subclass QMainWindow.

                        R Offline
                        R Offline
                        Renn
                        wrote on 24 Nov 2015, 10:08 last edited by
                        #11

                        @jsulm Is it in the file model.cpp, and model.h?

                        1 Reply Last reply
                        0
                        • J jsulm
                          24 Nov 2015, 09:59

                          No:
                          Model has to be a subclass of QAbstractItemModel (see http://doc.qt.io/qt-4.8/model-view-programming.html#model-classes).
                          Not sure why you subclass QMainWindow.

                          R Offline
                          R Offline
                          Renn
                          wrote on 25 Nov 2015, 01:25 last edited by
                          #12

                          @jsulm What should i change?

                          1 Reply Last reply
                          0
                          • J jsulm
                            24 Nov 2015, 09:59

                            No:
                            Model has to be a subclass of QAbstractItemModel (see http://doc.qt.io/qt-4.8/model-view-programming.html#model-classes).
                            Not sure why you subclass QMainWindow.

                            R Offline
                            R Offline
                            Renn
                            wrote on 25 Nov 2015, 03:45 last edited by
                            #13

                            @jsulm I have another project, this code can be called Model View Delegate?

                            ModView.pro

                            QT += core gui

                            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

                            TARGET = ModView
                            TEMPLATE = app

                            SOURCES += main.cpp
                            dialog.cpp

                            HEADERS += dialog.h

                            FORMS += dialog.ui

                            dialog.h

                            #ifndef DIALOG_H
                            #define DIALOG_H

                            #include <QDialog>
                            #include <QtGui>
                            #include <QtCore>

                            namespace Ui {
                            class Dialog;
                            }

                            class Dialog : public QDialog
                            {
                            Q_OBJECT

                            public:
                            explicit Dialog(QWidget *parent = 0);
                            ~Dialog();

                            private slots:
                            void on_insert_clicked();

                            void on_update_clicked();
                            
                            void on_delete_2_clicked();
                            

                            private:
                            Ui::Dialog *ui;
                            QStringListModel *model;
                            };

                            #endif // DIALOG_H

                            dialog.cpp

                            #include "dialog.h"
                            #include "ui_dialog.h"

                            Dialog::Dialog(QWidget *parent) :
                            QDialog(parent),
                            ui(new Ui::Dialog)
                            {
                            ui->setupUi(this);
                            model = new QStringListModel(this);
                            QStringList list;
                            model->setStringList(list);
                            ui->listView->setModel(model);
                            ui->comboBox->setModel(model);

                            ui->listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
                            

                            }

                            Dialog::~Dialog()
                            {
                            delete ui;
                            }

                            //insert
                            void Dialog::on_insert_clicked()
                            {
                            int row = model->rowCount();
                            model->insertRows(row,1);

                            QModelIndex index = model->index(row);
                            ui->listView->setCurrentIndex(index);
                            ui->listView->edit(index);
                            

                            }

                            //update
                            void Dialog::on_update_clicked()
                            {
                            int row = ui->listView->currentIndex().row();
                            model->insertRows(row,1);

                            QModelIndex index = model->index(row);
                            ui->listView->setCurrentIndex(index);
                            ui->listView->edit(index);
                            

                            }

                            //delete
                            void Dialog::on_delete_2_clicked()
                            {
                            model->removeRows(ui->listView->currentIndex().row(),1);
                            }

                            main.cpp

                            #include "dialog.h"
                            #include <QApplication>

                            int main(int argc, char *argv[])
                            {
                            QApplication a(argc, argv);
                            Dialog w;
                            w.show();

                            return a.exec();
                            

                            }

                            1 Reply Last reply
                            0

                            2/13

                            23 Nov 2015, 05:39

                            11 unread
                            • Login

                            • Login or register to search.
                            2 out of 13
                            • First post
                              2/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved