任务:显示一个2x3的表格,将表格中的数据显示如下:

    

思考:

1.如何显示数据和上个例子一样。

2.但是每个单元格的数据都是有角色划分的。 Qt::ItemDataRole

3.View从Model中获取数据,通过data函数。每一个单元格根据角色,调用多次data函数。个人理解:单元格什么角色都调用一次data函数。

代码如下:就是修改data函数的代码。

QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column(); if(role == Qt::DisplayRole) {
if(row == && col == )
return QString ("Row%1,Column%2").arg(index.row()+).arg(index.column()+);
if(row == && col == )
return QString ("Row%1,Column%2").arg(index.row()+).arg(index.column()+);
if(row == && col == )
return QString ("Row%1,Column%2").arg(index.row()+).arg(index.column()+);
if(row == && col == )
return QString ("Row%1,Column%2").arg(index.row()+).arg(index.column()+);
if(row == && col == )
return QString ("<--left");
if(row == && col == )
return QString ("right-->");
} if(role == Qt::FontRole) {
if(row == && col == ) {
QFont bold;
bold.setBold(true);
return bold;
}
} if(role == Qt::TextAlignmentRole) {
if(row == && col == )
return Qt::AlignLeft + Qt::AlignVCenter;
if(row == && col == )
return Qt::AlignRight + Qt::AlignVCenter;
} if(role == Qt::BackgroundColorRole) {
if(row == && col == )
return Qt::red;
} if(role == Qt::CheckStateRole) {
if(row == && col == )
return Qt::Checked;
} return QVariant();
}

10.model/view实例(2)的更多相关文章

  1. 10.model/view实例(3)

    任务:3x2的表格,第一个单元格显示当前时间 思考: 1.data函数里面QTime::currentTime()显示当前时间 2.但是这个事件是一个固定的时间,不会变动 3.需要时间变动,View就 ...

  2. 10.model/view实例(1)

    1.如图显示一个2x3的表格: 思考: 1.QTableView显示这个表 2.QAbstractTableModel作为模型类. 3.文档中找到subclass的描述 When subclassin ...

  3. 10.model/view实例(4)

    任务:给表单的每一列添加列名. 思考: 1.只需要添加一个函数 headerData(). 横向方面添加列名 代码如下: QVariant MyModel::headerData(int sectio ...

  4. 11.树形Model/View实例

    任务1:显示如图的树形结构 思考: 1.使用QTreeView显示. 2.Model使用QStandardItemModel,qt的一个标准model. 3.QStandardItemModel下每一 ...

  5. 深入浅出Java MVC(Model View Controller) ---- (JSP + servlet + javabean实例)

    在DRP中终于接触到了MVC,感触是确实这样的架构系统灵活性不少,现在感触最深的就是使用tomcat作为服务器发布比IIS好多了,起码发布很简单,使用起来方便. 首先来简单的学习一下MVC的基础知识, ...

  6. Qt Model/view 小实例 文件目录浏览器

    1. 文件目录浏览器 直接在main.cpp文件中添加下列代码 #include "mainwindow.h" #include <QApplication> #inc ...

  7. MVC(Model View Controller)框架

    MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  8. 利用反射和泛型把Model对象按行储存进数据库以及按行取出然后转换成Model 类实例 MVC网站通用配置项管理

    利用反射和泛型把Model对象按行储存进数据库以及按行取出然后转换成Model 类实例 MVC网站通用配置项管理   2018-3-10 15:18 | 发布:Admin | 分类:代码库 | 评论: ...

  9. 第15.27节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QTreeWidget详解

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 树部件(Tree Widget)是Qt Designer中 Item Widgets(It ...

随机推荐

  1. hibernate ORM related

    一.单向关联(unidirectional associations): 1.1.1 Many-to-one Employee.hbm.xml <class name="Employe ...

  2. const指针和指向const的指针

    int *const p=&a; 这是const指针,这种指针必须在定义时就给出它所指向的地址,否则会error:uninitialized const 'p'.const指针的指针本身是co ...

  3. fastCGI模块

    这个模块允许nginx同FastCGI协同工作,并且控制哪些参数将被安全传递.例: location / { fastcgi_pass localhost:9000; fastcgi_index in ...

  4. shell变量扩展技巧

    SHELL中有一些变量扩展的技巧,做下归纳总结 1.取字符串slice规则一:${变量名:位置起点}含义:由指定的位置起点开始,截取子字符串到字符串结束例如: var="/etc/passw ...

  5. 怪盗基德的滑翔翼(还是最长x序列)

    //怪盗基德的滑翔翼 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstri ...

  6. 下载安装jdk

    阿里云服务器部署第一步:下载-安装jdk 1.下载路径:http://www.oracle.com/ 2.通过windows远程桌面连接上服务器:连接方式: 然后在服务器上c:/user/下创建jav ...

  7. Weblogic-unable to get file lock, will retry …问题解决

    weblogic部署应用出现如下报错: <2017-8-15 下午05时08分44秒 CST> <Info> <Management> <BEA-141281 ...

  8. POJ3468:A Simple Problem with Integers

    浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:http://poj.org/problem?id=3468 分块裸题.对于每个块记录 ...

  9. dockerfile http_php

    FROM centos6.6-php5.5:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/php.ini /etc/php.ini.bak COPY ./ph ...

  10. [转载]关于linux下system()函数的总结

    1.曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.这里必须要搞懂system()函数,因为有时你不得不面对它. 2.先来看一下system()函数的简 ...