QListView配合Model和Delegate工作的例子
主要程式碼:
#ifndef STUDENT_H
#define STUDENT_H
#include <QString>
class Student
{
public:
Student();
void setId(int id);
int id();
void setName(const QString &name);
QString name();
private:
int m_id;
QString m_name;
};
#endif // STUDENT_H
#include "student.h"
Student::Student()
{
m_id=0;
m_name="";
}
void Student::setId(int id)
{
this->m_id=id;
}
int Student::id()
{
return this->m_id;
}
void Student::setName(const QString &name)
{
this->m_name=name;
}
QString Student::name()
{
return this->m_name;
}
#ifndef STUDENTFRAME_H
#define STUDENTFRAME_H
#include <QFrame>
namespace Ui {
class StudentFrame;
}
class StudentFrame : public QFrame
{
Q_OBJECT
public:
explicit StudentFrame(QWidget *parent = 0);
~StudentFrame();
void setName(const QString &name);
void setSelected(bool selected);
private:
Ui::StudentFrame *ui;
};
#endif // STUDENTFRAME_H
#include "studentframe.h"
#include "ui_studentframe.h"
StudentFrame::StudentFrame(QWidget *parent) :
QFrame(parent),
ui(new Ui::StudentFrame)
{
ui->setupUi(this);
}
StudentFrame::~StudentFrame()
{
delete ui;
}
void StudentFrame::setName(const QString &name)
{
ui->lineName->setText(name);
}
void StudentFrame::setSelected(bool selected)
{
if(selected)
{
this->setStyleSheet("#StudentFrame{border:2px solid red;}");
}
else
{
this->setStyleSheet("#StudentFrame{border:none;}");
}
}
#ifndef STUDENTLISTVIEWMODEL_H
#define STUDENTLISTVIEWMODEL_H
#include <QAbstractListModel>
class Student;
class StudentListViewModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit StudentListViewModel(QObject *parent = 0);
~StudentListViewModel();
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
void add();
Student *at(int index);
private:
QList<Student*> m_studentList;
int m_testNumber;
signals:
public slots:
};
#endif // STUDENTLISTVIEWMODEL_H
#include "studentlistviewmodel.h"
#include "entity/student.h"
#include <QSize>
StudentListViewModel::StudentListViewModel(QObject *parent) : QAbstractListModel(parent)
{
m_testNumber=10000;
}
StudentListViewModel::~StudentListViewModel()
{
}
int StudentListViewModel::rowCount(const QModelIndex &parent) const
{
return m_studentList.size();
}
int StudentListViewModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QVariant StudentListViewModel::data(const QModelIndex &index, int role) const
{
Student *student=m_studentList.at(index.row());
if(student==NULL)
{
return QVariant();
}
else
{
switch (role) {
case Qt::UserRole:
return student->id();
case Qt::DisplayRole:
return student->name();
case Qt::SizeHintRole:
return QSize(0, 100);
default:
return QVariant();
}
}
return QVariant();
}
void StudentListViewModel::add()
{
Student *student=new Student;
student->setId(m_testNumber);
student->setName(QString::number(m_testNumber));
m_studentList.push_back(student);
emit layoutChanged();
m_testNumber++;
}
Student *StudentListViewModel::at(int index)
{
return m_studentList.at(index);
}
#ifndef STUDENTITEMDELEGATE_H
#define STUDENTITEMDELEGATE_H
#include <QStyledItemDelegate>
class StudentListViewModel;
class StudentFrame;
class StudentItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit StudentItemDelegate(StudentListViewModel *studentListViewModel, QObject *parent = 0);
~StudentItemDelegate();
protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
StudentListViewModel *m_studentListViewModel;
StudentFrame *m_studentFrame;
signals:
public slots:
};
#endif // STUDENTITEMDELEGATE_H
#include "studentitemdelegate.h"
#include "model/studentlistviewmodel.h"
#include "entity/student.h"
#include "widget/studentframe.h"
#include <QPainter>
StudentItemDelegate::StudentItemDelegate(StudentListViewModel *studentListViewModel,QObject *parent) : QStyledItemDelegate(parent)
{
this->m_studentListViewModel=studentListViewModel;
m_studentFrame=new StudentFrame;
}
StudentItemDelegate::~StudentItemDelegate()
{
if(m_studentFrame)
{
m_studentFrame->deleteLater();
m_studentFrame=NULL;
}
}
void StudentItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column()==0)
{
Student *student=m_studentListViewModel->at(index.row());
if(student!=NULL)
{
m_studentFrame->setName(student->name());
m_studentFrame->resize(option.rect.width(), option.rect.height());
if (option.state & QStyle::State_Selected)
{
m_studentFrame->setSelected(true);
}
else
{
m_studentFrame->setSelected(false);
}
QPixmap pixmap=m_studentFrame->grab();
painter->drawPixmap(option.rect, pixmap);
return;
}
}
QStyledItemDelegate::paint(painter, option, index);
}
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include "entity/student.h"
class StudentListViewModel;
class StudentItemDelegate;
namespace Ui {
class MainWidget;
}
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private slots:
void on_buttonAdd_clicked();
private:
Ui::MainWidget *ui;
StudentListViewModel *m_studentListViewModel;
StudentItemDelegate *m_studentItemDelegate;
};
#endif // MAINWIDGET_H
#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "model/studentlistviewmodel.h"
#include "delegate/studentitemdelegate.h"
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
this->setWindowTitle("List View Test");
m_studentListViewModel=new StudentListViewModel(this);
m_studentItemDelegate=new StudentItemDelegate(m_studentListViewModel, this);
ui->listView->setModel(m_studentListViewModel);
ui->listView->setItemDelegate(m_studentItemDelegate);
}
MainWidget::~MainWidget()
{
delete ui;
}
void MainWidget::on_buttonAdd_clicked()
{
m_studentListViewModel->add();
}
執行效果:
(------------完------------)
相關文章
- QListView和QListWidgetView
- with 和 whereHas 的配合使用
- 深度解析 Delegate 和 Notification 和 KVO
- c#之delegate和eventC#
- Swift3.0 Delegate和Block的實現SwiftBloC
- iOS開發之protocol和delegateiOSProtocol
- ModelArts準備工作
- 網路回撥:Block和Delegate的對比BloC
- 基礎才是重中之重~delegate裡的Invoke和BeginInvoke
- Angular @Inject 註解的實際應用例子和工作原理淺析Angular
- 代理的基本使用(delegate)
- C#中的delegateC#
- [譯] Jquery中 .bind() .live() .delegate() 和 .on() 之間的區別jQuery
- (筆記) Delegate的再進化:Action Generic Delegate (.NET) (C#)筆記C#
- C#-delegateC#
- scrapy 使用的基本流程和例子
- 誰能說說SessionFacade的粒度,以及和Delegate的關係?Session
- c#之delegateC#
- delegate Thread 的簡單使用thread
- 從OC和C#中找樂趣:相同又不同的delegateC#
- 【java web】--JSP model1和model2的聯絡和區別JavaWebJS
- Sqlserver鎖升級的理解和例子SQLServer
- nginxrewrite引數和例子Nginx
- C#-委託delegateC#
- delegate 繼承問題繼承
- C#委託(delegate)C#
- Yii 框架Model和ActiveRecord 的區別框架
- 徹底弄懂C#中delegate、event、EventHandler、Action、Func的使用和區別C#
- 窺探Swift之協議(Protocol)和委託代理(Delegate)回撥的使用Swift協議Protocol
- trait 和型別的方法同名的例子AI型別
- smack和openfire通訊的簡單例子Mac單例
- C# 委託(delegate)、泛型委託和Lambda表示式C#泛型
- IOS學習之一個示例弄懂代理(delegate)和協議iOS協議
- 用一個實際例子理解Dockervolume工作原理Docker
- C++【vector】用法和例子C++
- oracle expdp和impdp使用例子Oracle
- Django 公共模型欄位的設定和繼承(AuditBaseModel、ApprovalBaseModel)Django模型繼承APP
- The request was denied by service delegate (SBMainWorkspace) for reasoAI