QT: custom Model and related demo

Posted by karimali831 on Sat, 18 Apr 2020 16:51:39 +0200

Use background

With the increase of development projects, the business structure is more and more complex. In order to improve the development efficiency of programmers and reduce the maintenance cost (code readability), using MVC (MVD) mode in QT is an effective method. Customizing the Model is the first step in using the MVC pattern.

How to customize

QT gives us many predefined models, view s and delegate s, which means that we can easily and quickly inherit the corresponding classes to complete the development. We only need to follow the relevant specifications and implement some specific methods to complete the custom model class.

Step: take QAbstractTableModel as an example

  1. Create a new C + + class, generate header file and cpp file
  2. Inherit the QAbstractTableModel class in the header class definition and override the following functions
    1. virtual int rowCount(const QModelIndex &parent) const;
    2. virtual int columnCount(const QModelIndex &parent) const;
    3. QVariant data(const QModelIndex &index, int role) const;
    4. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  3. Define the data type you need in the header file
  4. Initialize your own data type in the cpp file, and implement the above methods, refer to the following code

Demo

header:

#ifndef MODELEX_H
#define MODELEX_H
#include <QAbstractTableModel>
#include <QVector>
#include <QMap>
#include <QStringList>
class ModelEx :public QAbstractTableModel
{
public:
    ModelEx();

    //Declare virtual function
    virtual int rowCount(const QModelIndex &parent) const;
    virtual int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

signals:

public slots:

private:
    QVector<short> army;
    QVector<short> weaponType;
    QMap<short,QString>armyMap;
    QMap<short,QString> weaponTypeMap;//Weapon type correspondence
    QStringList weapon;//Weapon list
    QStringList header;
    void populateModel();//Initialization function
};

#endif // MODELEX_H

cpp:

#include "modelex.h"
#include <QDebug>


ModelEx::ModelEx()
{
   //Initialize numeric text correspondence
    armyMap[1] = "navy";
    armyMap[2] = "air force";
    armyMap[3] = "Land force";
    armyMap[4] = "Marine Corps";

    weaponTypeMap[1] = "Bomber";
    weaponTypeMap[2] = "Fighter";
    weaponTypeMap[3] = "Aircraft carrier";
    weaponTypeMap[4] = "Destroyer";
    weaponTypeMap[5] = "Helicopter";
    weaponTypeMap[6] = "TANKE";
    weaponTypeMap[7] = "Amphibious combat vehicle";
    weaponTypeMap[8] ="I made it up";


    populateModel();

}

void ModelEx::populateModel(){
    header<<"Services"<<"type"<<"arms";//Header information
    army<<1<<2<<3<<4<<4<<2<<3;
    weaponType<<1<<6<<3<<7<<4<<8<<2;//How values and words are translated
    weapon<<"B--2"<<"A-2"<<"D-!qaew"<<"B--2"<<"A-2"<<"D-!qaew"<<"B--2";
}

//Returns the number of columns in the model
int ModelEx::columnCount(const QModelIndex &parent) const{
    return header.size();
}

int ModelEx::rowCount(const QModelIndex &parent) const{
    return army.size();
}

//Returns the data of the specified index, that is, the value is mapped to the text
QVariant ModelEx::data(const QModelIndex &index, int role) const{
    if(!index.isValid()){
        return QVariant();
    }

    if(role==Qt::DisplayRole){
        switch(index.column()){
         case 0:
            return armyMap[army[index.row()]];
            break;

         case 1:
            return weaponTypeMap[weaponType[index.row()]];
            break;
         case 2:
            return weapon[index.row()];
            break;
         default:
              return QVariant();
            break;
        }
    }

      return QVariant();
}

QVariant ModelEx::headerData(int section, Qt::Orientation orientation, int role) const{
    if(role==Qt::DisplayRole&&orientation==Qt::Horizontal)
        return header[section];
    return QAbstractTableModel::headerData(section,orientation,role);
}

Test:

#include "qmodeldialog.h"
#include <QApplication>
#include <QTableView>
#include "modelex.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    QModelDialog w;
//    w.show();
    ModelEx *model = new ModelEx();
    QTableView view;
    view.setModel(model);
    view.setWindowTitle("ModelEx");
    view.resize(400,300);
    view.show();

    return a.exec();
}

Qt5 development and examples, Third Edition, Electronic Industry Press - Lu Wentong

Topics: Qt Qt5