QListView配合Model和Delegate工作的例子

lxj434368832發表於2020-11-29

主要程式碼:


#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();
}

 

執行效果:

(------------完------------)

相關文章