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
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
To copy to clipboard, switch view to plain text mode
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtGui>
#include <QtCore>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private slots:
void insertData();
void updateData();
void deleteData();
private:
Ui:: Dialog *ui;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
ui(new Ui:: Dialog)
{
ui->setupUi(this);
model->setStringList(list);
ui->listView->setModel(model);
ui->comboBox->setModel(model);
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);
ui->listView->setCurrentIndex(index);
ui->listView->edit(index);
}
void Dialog::updateData()
{
int row = ui->listView->currentIndex().row();
model->insertRows(row, 0);
ui->listView->setCurrentIndex(index);
ui->listView->edit(index);
}
void Dialog:: deleteData()
{
int row = ui->listView->currentIndex().row();
model->removeRows(row, 1);
}
#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);
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks