View Full Version : Model-view-controller example?
brcain
24th February 2006, 03:57
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
Everall
24th February 2006, 08:47
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
brcain
24th February 2006, 16:33
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. :D
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.
shankarnag
27th April 2010, 09:15
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
JD2000
27th April 2010, 12:48
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/getting-started/getting-started#read-the-official-qt
renaldi2108
25th November 2015, 05:24
Check my 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 insertData();
void updateData();
void deleteData();
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);
connect(ui->insert, SIGNAL(clicked()), this, SLOT(insertData()));
connect(ui->update, SIGNAL(clicked()), this, SLOT(updateData()));
connect(ui->delete_2, SIGNAL(clicked()), this, SLOT(deleteData()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::insertData()
{
int row = model->rowCount();
model->insertRows(row ,1);
QModelIndex index = model->index(row);
ui->listView->setCurrentIndex(index);
ui->listView->edit(index);
}
void Dialog::updateData()
{
int row = ui->listView->currentIndex().row();
model->insertRows(row, 0);
QModelIndex index = model->index(row);
ui->listView->setCurrentIndex(index);
ui->listView->edit(index);
}
void Dialog:: deleteData()
{
int row = ui->listView->currentIndex().row();
model->removeRows(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();
}
Powered by vBulletin® Version 4.2.5 Copyright © 2023 vBulletin Solutions Inc. All rights reserved.