Example MVC in Qt C++
-
Take a look at this introduction:
http://doc.qt.io/qt-5.5/model-view-programming.html -
Take a look at this introduction:
http://doc.qt.io/qt-5.5/model-view-programming.html -
Yes, that's the Qt way to do it
-
Why in one file? You should separate the model, view and delegate even on file level.
-
On the side I provided you there are links to examples:
http://doc.qt.io/qt-5.5/model-view-programming.html#related-examples -
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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MVC
TEMPLATE = appSOURCES += main.cpp
mvc.cpp
model.cppHEADERS += mvc.h
model.hFORMS += mvc.ui
model.h
#ifndef MODEL_H
#define MODEL_H#include <QDialog>
class Model : public QDialog
{
Q_OBJECTpublic:
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_OBJECTpublic:
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;
} -
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. -
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. -
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. -
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 I have another project, this code can be called Model View Delegate?
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ModView
TEMPLATE = appSOURCES += main.cpp
dialog.cppHEADERS += 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_OBJECTpublic:
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();
}