做的网站如何全屏代码,前端如何做响应式网站,搜索量排名,wordpress科技文章目录 前言模型/视图编程1.先写模型2. 视图3. 委托 例子#xff08;Qt代码#xff09;例1 查询本机文件系统例2 标准模型项操作例3 自定义模型示例:军事武器模型例4 只读模型操作示例例5 选择模型操作例6 自 定 义委 托(在testSelectionModel上修改) 前言
在Qt中#xf… 文章目录 前言模型/视图编程1.先写模型2. 视图3. 委托 例子Qt代码例1 查询本机文件系统例2 标准模型项操作例3 自定义模型示例:军事武器模型例4 只读模型操作示例例5 选择模型操作例6 自 定 义委 托(在testSelectionModel上修改) 前言
在Qt中有两种常见的模型/视图架构用于实现大量的数据存储、处理和显示Model/View和Model/View/Controller。
两者的区别是什么其实就是看开发者是否想要自己自定义控制器
下面我举个例子你就明白了
QAbstractItemModel属于Model层负责数据的存储和管理。QAbstractItemDelegate属于View层负责定制视图中单元格的显示和编辑。控制器Controller是开发者自行设计和实现的用于处理用户输入事件并更新模型或视图。
这样可以将Qt中的模型/视图架构描述为Model/View并通过自定义的控制器来实现Model/View/Controller的设计模式。 下面来详细写一下MVC
模型/视图编程
Qt 中 的 模 型 / 视 图 架 构 用 来 实 现 大 量 的 数 据 存 储 、 处 理 及 显 示 。 MVC(Model-View-Controller)包括了 3 个组件
模型Model是应用对象用来表示数据视图View是模型的用户界面用来显示数据控制Controller定义了用户界面对用户输入的反应方式。委托Delegate用于定制数据的渲染和编辑方式。
control和Delegate不用分的太清你就直接理解为Delegate就是Controller即可 说白了MVC就是3个部分一个是Data数据第二个相当于容器用来放数据的然后将数据可视化第三个是负责数据和容器的交互问题。 菜鸟教程 https://www.runoob.com/design-pattern/mvc-pattern.html
最后再理解一波这个是最正确的!!!mvc是经典的三层结构将数据视图和逻辑分离,这个不是Qt专属单纯的cpp编程java编程python编程Kotlin编程…都能使用更像是一种框架模式【注意框架模式不是设计模式】而Model/View框架才是qt自己的查了一下大概是从qt4开始引入在Qt中这个框架模式中设计到三个类model类view类和delegate类。model类保存数据view负责显示而delegate负责协调model和view之间的数据edit(编辑)和render(渲染)
然后再讲一下流程
第一步用户通过与View进行交互例如编辑单元格、拖放操作等触发用户操作事件。第二步View将用户的操作事件传递给Delegate进行处理。Delegate根据需要对数据进行修改、验证或其他特定操作。第三步Delegate将更新后的数据传递给Model进行存储和更新。第四步Model通知View数据的变化View重新获取更新后的数据并刷新显示。
举个不太恰当的例子比如用户编辑了单元格上的数据【在View上操作】触发了某个功能比如说是根据已有数据生成新的数据然后View将这个请求发给Delegate进行委托Delegate计算出了新生成的数据并将更新的数据发给了Model然后Model通知View数据有变然后View更新数据刷新用户界面这样用户就看到了新的界面
然后不正确的想法我用横线划掉了就不删了全当做一种思考过程了
1.先写模型
所有的模型都基于 $QAbstractItemModel 类该类提供了十分灵活的接口来处理各种视图这些视图可以将数据的表现形式为表格 类该类提供了十分灵活的接口来处理各种视 图这些视图可以将数据的表现形式为表格 类该类提供了十分灵活的接口来处理各种视图这些视图可以将数据的表现形式为表格table 、列表 、列表 、列表list 、树 、树 、树tree$。
Qt 提供了一些现成的模型来处理数据项
$QStringListModel $存储简单的 Q S t r i n g QString QString 项目列表$QStandardItemModel $管理复杂的属性结构数据项每一个数据项可以包含任意的数据 Q F i l e S y s t e m M o d e l QFileSystemModel QFileSystemModel 提供了本地文件系统中文件和目录信息 Q S q l Q u e r y M o d e l QSqlQueryModel QSqlQueryModel、 Q S q l T a b l e M o d e l QSqlTableModel QSqlTableModel 和 Q S q l R e l a t i o n T a b l e M o d e l QSqlRelationTableModel QSqlRelationTableModel 用来访问数据库。
若标准模型还无法满足需要时可子类化 QAbstractItemModel、QAbstractListModel 或 QAbstractTableModel 来创建自定义的模型。 常见的 3 种模型为列表模型、表格模型、树模型如下图所示 为确保数据表示与数据获取相分离Qt 引入了模型索引的概念输入和委托均可 通过模型索引来请求数据并显示。只有模型需要知道怎样获取数据被模型管理的数据类型 可以被广泛的定义。模型索引包含一个指针指向创建他们的模型使用多个模型时可避免 混淆。模型索引 Q M o d e l I n d e x QModelIndex QModelIndex 类提供对一块数据的临时引用用来修改或检索模型中的数 据获取一个数据项的模型索引必须指定模型的 3 个属性行号、列号和父项的模型索引。 如 QModelIndex index model-index(row,column,parent); 也可通过模型指定的相关数据项对应的模型索引以及特定的角色来获取需要的类型数据如 QVariant value model-data(index,role); 常用的角色类型:
常量描述Qt::DisplayRole数据被渲染为文本(数据类型为QString类型)Qt::DecorationRole数据被渲染为图标等装饰(数据为QColor、Qlcon或QPixmap类型)Qt::EditRole数据可以在编辑器中进行编辑数据为QSring类型Qt::ToolTipRole数据显示在数据项的工具提示中数据类型为 QStringQt::StatusTipRole数据显示在状态栏中数据为 QString 类型Qt::WhatsThisRole数据显示在数据项的“What’s This”模式下数据为 QString 类型Qt::SizeHintRole数据项的大小提示将会应用到视图数据为 QSize 类型Qt::FontRole默认代理的绘制使用的字体Qt::TextAlignmentRole默认代理的对齐方式Qt::BackgroundRole默认代理的背景画刷Qt::ForegroundRole默认代理的前景画刷Qt::CheckStateRole默认代理的前景画刷Qt::UserRole重点用户自定义的数据的起始位置………
2. 视图
Qt 提供了 QListView、QTableView 视图、QTreeView 视图分别实现列表、表格与树视 图效果。
QListView 将数据项显示为一个列表
QTableView 将模型中的数据显示在一个表格中
QTreeView 将模型中的数据项显示在具有层次的列表中。
QTableView 和 QTreeView 在显示项目的时候同时还可以显示标头通过 QHeaderView 类实现。
自定义视图类是基于QAbstractItemView 抽象基类如实现条形图饼状图等特殊显示方式。
视图类的选择行为
常量描述QAbstractView::SelectItems选择单个项目QAbstractView::SelectRows只选择行QAbstractView::SelectColumns只选择列
视图类的选择模式
常量描述QAbstractItemView::SigleSelection当用户选择一个项目时所有已经选择的项目将成为未选择状态而且用户无法在已经选择的项目上单击来取消选择QAbstractView::ContiguousSelection用户单击一个项目的同时按 Shift 键则所有当前项目和单击项目之间的项目都将被选择或取消选择QAbstractView::ContiguousSelection具有 ContiguousSelection 的特性且可按 Ctrl 键进行不连续选择QAbstractView::MultiSelection用户选择一个项目时不影响其他已经选择的项目QAbstractView::NoSelection项目无法被选择
选择模型更新方式
常量描述QItemSelectionModel::NoUpdate不做选择QItemSelectionModel::Clear选择被清除QItemSelectionModel::Select选择指定索引QItemSelectionModel::Deselect取消指定索引的选择QItemSelectionModel::Toggle切换指定索引的选择QItemSelectionModel::Current当前选择被更新QItemSelectionModel::Rows索引扩展为跨行QItemSelectionModel::Columns索引扩展为跨列QItemSelectionModel::SelectCurrentSelect|Current组合QItemSelectionModel::ToggleCurrentToggle|Current组合QItemSelectionModel::ClearAndSelectClear|Select组合
3. 委托
在模型/视图框架中 Q A b s t r a c t I t e m D e l e g a t e QAbstractItemDelegate QAbstractItemDelegate 是委托类的抽象基类 Qt 默认的委托实现由 Q S t y l e d I t e m D e l e g a t e QStyledItemDelegate QStyledItemDelegate 类 提 供 这 也 被 用 作 Qt 标 准 视 图 的 默 认 委 托 选 择 Q S t y l e d I t e m D e l e g a t e QStyledItemDelegate QStyledItemDelegate 或者 QItemDelegate 中其一来为视图中的项目 绘制和提供编辑器。 不同的是 : Q S t y l e d I t e m D e l e g a t e QStyledItemDelegate QStyledItemDelegate使用当前的样式来绘制项目实现自定义委托建议使用 Q S t y l e d I t e m D e l e g a t e QStyledItemDelegate QStyledItemDelegate作为基类。
Qt 提供了项目视图的便捷类这些类底层通过模型/视图框架实现。
这些部件分别是 QListWidget 提供一个项目列表 QTreeWidget 显示一个多层次的树结构 QTableWidget提供了一个以项目作为单元的表格。 它们每一个类都继承了 QAbstractItemView 类的行为。
之所以成为便捷因其用起来比较简单使用于少量的数据的存储和显示。因没有将视图与模型分离所以没有视图类灵活不能和任意的模型一起使用。
例子Qt代码
例1 查询本机文件系统
//项目名testModelView
//效果查询本机文件系统//修改内容如下//main.cpp
#include mainwindow.h#include QApplication
#include QAbstractItemModel
#include QDirModel
#include QTreeView
#include QListView
#include QTableView
#include QAbstractItemView
#include QItemSelectionModel
#include QSplitterint main(int argc, char *argv[])
{QApplication app(argc, argv);//1.创建模型QDirModel model;//默认情况下QDirModel打开的系统上的cdef盘等也就是你系统的根目录//2.创建树视图、列表视图、表格视图QTreeView tree;QListView list;QTableView table;//3.在视图中设置模型tree.setModel(model);list.setModel(model);table.setModel(model);//4.设置视图对象的选择方式为多选然后list/table选择方式同treetree.setSelectionMode(QAbstractItemView::MultiSelection);list.setSelectionMode(tree.selectionMode());table.setSelectionMode(tree.selectionMode());//5.树视图双击信号发射后列表以及表格视图都要刷新内容QObject::connect(tree,SIGNAL(doubleClicked(QModelIndex)),list,SLOT(setRootIndex(QModelIndex)));QObject::connect(tree,SIGNAL(doubleClicked(QModelIndex)),table,SLOT(setRootIndex(QModelIndex)));//6.创建分割器他可以将界面分割成多个子区域QSplitter *splitter new QSplitter;splitter-addWidget(tree);splitter-addWidget(list);splitter-addWidget(table);splitter-setWindowTitle(QString(模型/视图));splitter-show();//MainWindow w;//w.show();return app.exec();
} 例2 标准模型项操作
//项目名 testQModelIndex
//效果标准模型项操作//修改内容如下//main.cpp
#include mainwindow.h#include QApplication
#include QStandardItemModel
#include QTreeView
#include QDebugint main(int argc, char *argv[])
{QApplication a(argc, argv);//MainWindow w;//w.show();//1.创建标准项模型QStandardItemModel model;//2.获取标准项模型的根项根项是不可见的QStandardItem *parentItem model.invisibleRootItem();//3.创建标准项item0设置文本设置图标工具提示QStandardItem *item0 new QStandardItem;item0-setText(QString(A));//4.PixmapQPixmap pixmap0(50,50);pixmap0.fill(Qt::red);item0-setIcon(QIcon(pixmap0));item0-setToolTip( QString(As tips));//将item0作为父项的子项parentItem-appendRow(item0);parentItem item0;//创建item0的子项QStandardItem *item1 new QStandardItem;item1-setText(QString(B));QPixmap pixmap1(50,50);pixmap1.fill(Qt::blue);item1-setIcon(pixmap1);item1-setToolTip(QString(Bs ...));parentItem-appendRow(item1);QStandardItem * item2 new QStandardItem;QPixmap pixmap2(50,50);pixmap2.fill(Qt::green);item2-setData(C,Qt::EditRole);item2-setData(index C,Qt::ToolTipRole);item2-setData(QIcon(pixmap2),Qt::DecorationRole);parentItem-appendRow(item2);//在树视图中显示数据QTreeView view;view.setModel(model);view.show();QModelIndex indexA model.index(0,0,QModelIndex());qDebug()indexA row coutn:model.rowCount(indexA);QModelIndex indexB model.index(0,0,indexA);qDebug()indexB text:model.data(indexB,Qt::EditRole).toString();qDebug() indexB toolTip:model.data(indexB,Qt::ToolTipRole).toString();return a.exec();
} 例3 自定义模型示例:军事武器模型
//testCustomModel//添加 c类 WeaponModel 继承自 QAbstractTableModel//weaponmodel.h
#ifndef WEAPONMODEL_H
#define WEAPONMODEL_H#include QAbstractTableModelclass WeaponModel : public QAbstractTableModel
{
public:explicit WeaponModel(QObject *parent nullptr);virtual int rowCount(const QModelIndex parent QModelIndex()) const;virtual int columnCount(const QModelIndex parent QModelIndex())const;QVariant data(const QModelIndex index,int role) const;QVariant headerData(int section,Qt::Orientation orientation,int role)const;private:QVectorshortarmy;//军队QVectorshortweaponType;//武器类型QMapshort,QStringarmyMap; //军队映射QMapshort,QString weaponMap;//武器映射QStringList weapon; // 武器QStringList header; //表头void populateModel();//表格数据的初始化
};#endif // WEAPONMODEL_H//weaponmodel.cpp
#include weaponmodel.hWeaponModel::WeaponModel(QObject *parent): QAbstractTableModel{parent}
{//请坚持使用英文,英文代码,中文注释armyMap[1] QString(air force);//空军armyMap[2] QString(Navy);//海军armyMap[3] QString(army);//陆军armyMap[4] QString(marines);//海军陆战队weaponMap[1] QString(bomber);//轰炸机weaponMap[2] QString(fighter);//战斗机weaponMap[3] QString(aircraft carrier);//航空母舰weaponMap[4] QString(destroyer);//驱逐舰weaponMap[5] QString(helicopter);//直升机weaponMap[6] QString(tank);//坦克weaponMap[7] QString(Amphibious assault ship);//两栖攻击舰weaponMap[8] QString(Amphibious tank);//两栖战车populateModel();}int WeaponModel::rowCount(const QModelIndex parent) const
{return army.size();
}int WeaponModel::columnCount(const QModelIndex parent) const
{return 3;
}//放置指定索引的数据,将数据映射成文字
QVariant WeaponModel::data(const QModelIndex index, int role) const
{if(!index.isValid())return QVariant();if(role Qt::DisplayRole){switch(index.column()){case 0:return armyMap[army[index.row()]];break;case 1:return weaponMap[weaponType[index.row()]];break;case 2:return weapon[index.row()];default:return QVariant();}}return QVariant();
}QVariant WeaponModel::headerData(int section, Qt::Orientation orientation, int role) const
{if(role Qt::DisplayRole orientation Qt::Horizontal)return header[section];return QAbstractTableModel::headerData(section,orientation,role);
}void WeaponModel::populateModel()
{header QString(军种)//军种QString(种类)//种类QString(武器);//武器army12342431;//weaponType13574862;//8个数据内容随便写weaponQString(B-2)QString(尼尔兹极)//尼尔兹极QString(APACHE)QString(Wasp class)//阿帕奇 黄蜂级QString(Proportional Burke level)QString(AAAV)//比例伯克级 AAAVQString(M1A1)QString(F-22);//M1A1 F-22}//main.cpp
#include mainwindow.h
#include weaponmodel.h
#include QApplication
#include QTableViewint main(int argc, char *argv[])
{QApplication app(argc, argv);WeaponModel model;QTableView view;view.setModel(model);view.setWindowTitle(QString(table view));//一定不要写中文,空格可以有view.resize(600,400);view.show();//MainWindow w;//w.show();return app.exec();
} 例4 只读模型操作示例
//testModel//添加自定义类 StringListModel 继承自 QAbstractListModel
//stringlistmodel.h
#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H#include QAbstractListModelclass StringListModel : public QAbstractListModel
{Q_OBJECT
public:StringListModel(const QStringList strings,QObject *parent 0):QAbstractListModel(parent),m_stringList(strings){}//模型行数int rowCount(const QModelIndex parent QModelIndex()) const;//指定模型索引的数据项QVariant data(const QModelIndex index,int role)const;//表头内容(数或表格)QVariant headerData(int section,Qt::Orientation orientation,int role Qt::DisplayRole)const;//项目属性Qt::ItemFlags flags(const QModelIndex index)const;//编辑数据bool setData(const QModelIndex index,const QVariant value,int role Qt::EditRole);//插入行bool insertRows(int position,int rows,const QModelIndex index QModelIndex());//删除行bool removeRows(int position,int rows,const QModelIndex index QModelIndex());
private:QStringList m_stringList;
};#endif // STRINGLISTMODEL_H//stringlistmodel.cpp
#include stringlistmodel.h//row 行数
int StringListModel::rowCount(const QModelIndex parent) const
{return m_stringList.count();
}/**用于获取模型中特定索引位置的数据。如果索引无效它会返回一个空的 QVariant。*如果索引是最后一个元素也会返回一个空的 QVariant。*否则它根据传入的 role 返回对应的数据。**Qt::DisplayRole 和 Qt::EditRole 角色用于显示和编辑数据。*在这里它返回了 m_stringList 中相应索引位置的字符串。
*/
QVariant StringListModel::data(const QModelIndex index, int role) const
{if(!index.isValid())return QVariant();if(index.row() m_stringList.size())return QVariant();if(role Qt::DisplayRole || role Qt::EditRole)return m_stringList.at(index.row());elsereturn QVariant();
}/** headerData 函数返回表头的数据。在这里如果 role 不是 Qt::DisplayRole它会返回一个空的 QVariant。* 否则它会根据传入的 section 和 orientation 返回对应的表头数据。
*/
QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
{if(role ! Qt::DisplayRole)return QVariant();//水平表头if(orientation Qt::Horizontal)return QString(Column %1).arg(section);//Column 0...elsereturn QString(Row %1).arg(section);//Row 0...
}
/** Qt::ItemIsEnabled表示项目是可用的。* 否则它返回默认标志并添加了 Qt::ItemIsEditable 标志表示项目可以被编辑。
*/
Qt::ItemFlags StringListModel::flags(const QModelIndex index) const
{if(!index.isValid())return Qt::ItemIsEnabled;return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}/** 检查索引是否有效且角色是否为 Qt::EditRole。如果是它将替换 m_stringList 中相应位置的字符串* 并发出 dataChanged 信号来通知其他组件数据已经改变。
*/
bool StringListModel::setData(const QModelIndex index, const QVariant value, int role)
{//检测索引有效且项目可编辑if(index.isValid()roleQt::EditRole){m_stringList.replace(index.row(),value.toString());emit dataChanged(index,index);return true;}return false;
}
/** 它首先使用 beginInsertRows 告知其他组件将要开始插入行的操作。然后在指定的位置插入了指定数量的字符串* 这些字符串都是固定的 You are coming from Star.。* 最后使用 endInsertRows 告知其他组件插入操作已经完成。
*/
bool StringListModel::insertRows(int position, int rows, const QModelIndex index)
{//告知其他组件指定的行开始插入操作beginInsertRows(QModelIndex(),position,position rows - 1);for(int row 0; row rows; row){m_stringList.insert(position,QString(You are coming from Star.));}//告知其他组件完成操作endInsertRows();return true;
}//从那一行开始,删除多少行
bool StringListModel::removeRows(int position,int rows, const QModelIndex index)
{//告知其他组件指定的行开始删除操作beginRemoveRows(QModelIndex(),position,position rows -1);for(int row 0; row rows ; row){m_stringList.removeAt(position);}//告知其他组件完成操作return true;
}//main.cpp
#include mainwindow.h#include QApplication
#include stringlistmodel.h
#include QListView
#include QTableViewint main(int argc, char *argv[])
{QApplication app(argc, argv);QStringList list;list QString(Sun)QString(the Earth) QString(moon)QString(Saturn);StringListModel model(list);model.insertRows(3,4);//从第3行开始插入,插入4列(You are coming from Star.)model.removeRows(3,2);QListView listview;listview.setModel(model);listview.setWindowTitle(List View);//在Qt中你可以通过使用 setWindowTitle() 方法来为任意窗口设置标题名称。listview.show();QTableView tableview;tableview.setModel(model);tableview.setWindowTitle(Table View);tableview.show();//MainWindow w;//w.show();return app.exec();
} 例5 选择模型操作
//testSelectionModel//添加自定义类 MainWindow 继承自 QMainWindow
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindowQT_BEGIN_NAMESPACE
namespace Ui { class mainwindow; }
QT_END_NAMESPACEclass QTableView;
class QItemSelection;
class QModelIndex;class mainwindow : public QMainWindow
{Q_OBJECTpublic:mainwindow(QWidget *parent nullptr);~mainwindow();private slots:void getCurrentItemData();//当前选择void toggleSection();//切换选择//更新选择,selected表新的选择,deselected表以前的选择void updateSelection(const QItemSelection selected,const QItemSelection deselected);//改变当前模型索引void changeCurrent(const QModelIndex current,const QModelIndex previous);private:Ui::mainwindow *ui;QTableView *m_tableView;QTableView *m_tableView2;
};
#endif // MAINWINDOW_H//mainwindow.cpp
#include mainwindow.h
#include ui_mainwindow.h
#include QTableView
#include QDebug
#include QStandardItemMOdel
#include QItemSelection
//#include spinboxdelegate.hmainwindow::mainwindow(QWidget *parent): QMainWindow(parent), ui(new Ui::mainwindow)
{ui-setupUi(this);//创建标准项模型,7行4列QStandardItemModel *model new QStandardItemModel(7,4,this);for(int row 0 ; row 7 ; row){for(int column 0; column 4 ;column){QStandardItem * item new QStandardItem(QString(%1).arg(row*4 column));//标准模型设置数据项model-setItem(row,column,item);}}m_tableView new QTableView;m_tableView-setModel(model);setCentralWidget(m_tableView);//设置主窗口的中心部件为表格视图//获取视图的选择模式QItemSelectionModel *selectionModel m_tableView-selectionModel();QModelIndex topLeft;//左上角模型索引QModelIndex bottomRight;//右上角模型索引topLeft model-index(1,1);//1行1列bottomRight model-index(5,2);//5行2列//创建模型选择QItemSelection selection(topLeft,bottomRight);//以选择的方式唉选择项目selectionModel-select(selection,QItemSelectionModel::Select);//添加动作addAction(动作文本,响应这,槽方法)ui-menubar-addAction(QString(Current Project),this,mainwindow::getCurrentItemData);ui-menubar-addAction(QString(Switch Selection),this,mainwindow::toggleSection);//关联选择模型的选择改变,当前项改变的信号connect(selectionModel,QItemSelectionModel::selectionChanged,this,mainwindow::updateSelection);connect(selectionModel,QItemSelectionModel::currentChanged,this,mainwindow::changeCurrent);m_tableView2 new QTableView;m_tableView2-setWindowTitle(tableView2);m_tableView2-resize(400,300);m_tableView2-setModel(model);m_tableView2-setSelectionModel(selectionModel);m_tableView2-show();}mainwindow::~mainwindow()
{delete ui;delete m_tableView2;
}void mainwindow::getCurrentItemData()
{qDebug() QString(Current Data:)m_tableView-selectionModel()-currentIndex().data();
}void mainwindow::toggleSection()
{//左上角模型索引QModelIndex topLeft m_tableView-model()-index(0,0,QModelIndex());//右下角模型索引QModelIndex bottomRight m_tableView-model()-index(m_tableView-model()-rowCount(QModelIndex())-1,m_tableView-model()-columnCount(QModelIndex())-1,QModelIndex());//项选择QItemSelection curSelection(topLeft,bottomRight);m_tableView-selectionModel()-select(curSelection,QItemSelectionModel::Toggle);}void mainwindow::updateSelection(const QItemSelection selected, const QItemSelection deselected)
{QModelIndex index;//indexes()返回所有选择项的模型索引QModelIndexList list selected.indexes();//给现在选择的项目填充数据foreach (index, list) {QString text QString(%1,%2).arg(index.row()).arg(index.column());m_tableView-model()-setData(index,text);}//清空上一次选择的内容list deselected.indexes();foreach (index, list) {m_tableView-model()-setData(index,);}
}void mainwindow::changeCurrent(const QModelIndex current, const QModelIndex previous)
{qDebug()QString(from (%1,%2) to (%3,%4)).arg(previous.row()).arg(previous.column()).arg(previous.row()).arg(current.column());
}//main.cpp例6 自 定 义委 托(在testSelectionModel上修改)
//testSelectionModel//testSelectionModel 项 目 中添 加 自 定 义 委托 类 SpinBoxDelegate 继 承 自QItemDelegate//spinboxdelegate.h
#ifndef SPINBOXDELEGATE_H
#define SPINBOXDELEGATE_H#include QItemDelegateclass SpinBoxDelegate : public QItemDelegate
{Q_OBJECT
public:SpinBoxDelegate(QObject *parent 0);//创建编辑器QWidget *createEditor(QWidget *parent,const QStyleOptionViewItem option,const QModelIndex index)const override;//设置编辑器数据void setEditorData(QWidget *editor,const QModelIndex index)const override;//更新编辑器几何属性void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem option,const QModelIndex index)const override;
};#endif // SPINBOXDELEGATE_H//spinboxdelegate.cpp
#include spinboxdelegate.h#include QSpinBoxSpinBoxDelegate::SpinBoxDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem option, const QModelIndex index) const
{QSpinBox *editor new QSpinBox(parent);editor-setMinimum(0);editor-setMaximum(100);return editor;
}void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex index) const
{int value index.model()-data(index,Qt::EditRole).toInt();//类型转换:QWidget* 转 QSpinBox*QSpinBox * spinBox static_castQSpinBox*(editor);//编辑器设置数据spinBox-setValue(value);
}void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem option, const QModelIndex index) const
{editor-setGeometry(option.rect);
}//mainwindow.cpp的构造函数中添加
SpinBoxDelegate *delegate new SpinBoxDelegate(this);
//视图设置自定义委托
m_tableView-setItemDelegate(delegate);
文章转载自: http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.kwfnt.cn.gov.cn.kwfnt.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.qbfkz.cn.gov.cn.qbfkz.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.kjgdm.cn.gov.cn.kjgdm.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.ndnhf.cn.gov.cn.ndnhf.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn