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裡的Invoke和BeginInvoke
- 網路回撥:Block和Delegate的對比BloC
- Angular @Inject 註解的實際應用例子和工作原理淺析Angular
- (筆記) Delegate的再進化:Action Generic Delegate (.NET) (C#)筆記C#
- scrapy 使用的基本流程和例子
- Sqlserver鎖升級的理解和例子SQLServer
- trait 和型別的方法同名的例子AI型別
- C#-委託delegateC#
- delegate 繼承問題繼承
- Yii 框架Model和ActiveRecord 的區別框架
- 用一個實際例子理解Dockervolume工作原理Docker
- 徹底弄懂C#中delegate、event、EventHandler、Action、Func的使用和區別C#
- C# 委託(delegate)、泛型委託和Lambda表示式C#泛型
- Delegate 運算子深入淺出
- C++【string】用法和例子C++
- C++【vector】用法和例子C++
- 用一個實際例子理解Docker volume工作原理Docker
- 表單和v-model
- 工作286:v-model沒有值會報錯
- go 值傳遞和地址傳遞的例子Go
- [iOS] 從 application delegate 引申三點iOSAPP
- 瞭解下C# 委託(Delegate)C#
- C++【stack/queue】用法和例子C++
- Vue表單和v-modelVue
- popmenu的例子
- 解說pytorch中的model=model.to(device)PyTorchdev
- petite-vue原始碼剖析-雙向繫結`v-model`的工作原理Vue原始碼
- Model設計中常見的技巧和注意事項
- Github配合Eclipse和JetBrains系編譯器的使用(基礎篇)GithubEclipseAI編譯
- DedeCMS v5.7 通過檔案包含和CSRF的配合利用
- Delegate如何進行型別轉換?型別
- 如何避免UITableView重寫大量delegate以及n多if-else判斷和BlockUIViewBloC
- machine learning model(algorithm model) .vs. statistical modelMacGo
- Python中threading的join和setDaemon的區別及用法[例子]Pythonthread
- [轉載] Python中協程的詳細用法和例子Python
- Laravel中的ModelLaravel