一个 只读 readonly 示例
来源:互联网 发布:阿里数据平台两个 编辑:程序博客网 时间:2024/05/05 07:12
stringListModel.cpp
#ifndef STRINGLISTMODEL_H#define STRINGLISTMODEL_H#include <QAbstractListModel>#include <QStringList>class StringListModel : public QAbstractListModel{public: StringListModel(QObject *parent):QAbstractListModel(parent){}//对QAbstractModel 的部分纯虚函数进行实现 int columnCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;private: QStringList StringList;};#endif // STRINGLISTMODEL_H
stringlistmodel.cpp
#include "stringlistmodel.h"#include <qDebug>#include <QFont>#include <QBrush>#include <QPixmap>int StringListModel::columnCount(const QModelIndex &) const{ return 3;}int StringListModel::rowCount(const QModelIndex &) const{ return 2;}QVariant StringListModel::data(const QModelIndex &index, int role) const{ int row = index.row(); int column = index.column(); qDebug()<<QString("Row%1,Column%2").arg(index.row()+1).arg(index.column()+1); switch(role){ case Qt::BackgroundRole ://背景 if(row == 0 && column == 2){ QBrush color(Qt::red); return color; } if(row == 1 && column == 2){ QPixmap pix("../images/1.jpg"); return pix; } break; case Qt::DisplayRole : //展示 if(row == 0 && column == 1) return QString("<---LEFT"); if(row == 1 && column == 1) return QString("RIGHT--->"); return QString("Row%1,Column%2").arg(index.row()+1).arg(index.column()+1); break; case Qt::FontRole : //字体 if(row == 0 && column == 0){ QFont boldFont; boldFont.setBold(true); return boldFont; } break; case Qt::TextAlignmentRole : //对齐 if(row == 1 && column == 1){ return Qt::AlignRight + Qt::AlignVCenter; } break; case Qt::CheckStateRole : //check框 if(row == 1 && column == 0){ return Qt::Checked; } } return QVariant();}QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const{ return QVariant();}
main.h
#include "widget.h"#include "stringlistmodel.h"#include <QApplication>#include <QTableView>int main(int argc, char *argv[]){ QApplication a(argc, argv); QTableView * view = new QTableView; StringListModel * myModel = new StringListModel(0); view->setModel(myModel); view->resize(500,300); view->show(); return a.exec();}
The role parameter is used to let the model know which property is being requested.
ItemDataRole是一个枚举变量,可以在文档里查到:
ItemDataRole-enum
0 0
- 一个 只读 readonly 示例
- const,readonly 常量与只读
- 只读字段(readonly)和常量字段
- 设置Textbox只读属性,ReadOnly or ContentEditable
- 输入框只读属性readonly控制
- 设置Textbox只读属性,ReadOnly or ContentEditable
- 破解C#的readonly只读字段
- 设置checkbox为只读(readOnly)
- const常量和readonly只读变量
- C#中的只读字段——readonly
- html示例,readonly和Disable。
- .Net/C# 实现真正的只读属性 (ReadOnly Property)
- 下拉框只读(类似输入框的readonly)
- @Transactional(readOnly = true)无法控制事物只读
- 【jQuery】用jQuery给文本框添加只读属性【readOnly】
- 网页文本框只读readonly与不可用disabled区别
- android EditText 控件无法代码设置ReadOnly 只读
- html中input只读属性readonly 和 disable的区别
- 我是如何利用漏洞窃取麦当劳用户的账号和密码的
- hive orc创建出错create as select
- ReactNative调用Native(Android)
- 记一次JVM调优之旅(斗争full gc)
- ant项目构建(打jar包小案例)
- 一个 只读 readonly 示例
- struts2中的EL表达式和request
- 关于ORACLE的重启命令(linux)
- mysql 中 and 和 or 的用法
- nyoj117——求逆序数
- [git] warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF
- 弹出层 layer !!!常用
- 仿途牛旅游详情页面 置顶功能
- springMvc 常用接口HandlerMethodArgumentResolver使用