Example MVC in Qt C++



  • 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++


  • Moderators

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



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


  • Moderators

    Yes, that's the Qt way to do it



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


  • Moderators

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



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


  • Moderators

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



  • @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;
    }


  • Moderators

    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.



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



  • @jsulm What should i change?



  • @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();
    

    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.