日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT学习:模型练习

發布時間:2024/9/30 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT学习:模型练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過實現將數值代碼轉換為文字的模型來介紹如何使用自定義模型。此模型中保存了不同軍種的各種武器,實現效果如圖所示。

具體操作步驟如下。
(1)ModelEx類繼承自QAbstractTableModel類,頭文件“modelex.h”中的具體代碼如下:

#include <QAbstractTableModel> #include <QVector> #include <QMap> #include <QStringList> class ModelEx : public QAbstractTableModel { public:explicit ModelEx(QObject *parent=0);//虛函數聲明 virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;virtual int columnCount(const QModelIndex &parent=QModelIndex()) 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數據結構保存“數值—文字”的映射QMap<short,QString> weaponTypeMap;QStringList weapon;QStringList header;void populateModel(); //完成表格數據的初始化填充 };

(2)源文件“modelex.cpp”中的具體代碼如下:

#include "modelex.h" ModelEx::ModelEx(QObject *parent):QAbstractTableModel(parent) {armyMap[1]=tr("空軍");armyMap[2]=tr("海軍");armyMap[3]=tr("陸軍");armyMap[4]=tr("海軍陸戰隊");weaponTypeMap[1]=tr("轟炸機");weaponTypeMap[2]=tr("戰斗機");weaponTypeMap[3]=tr("航空母艦");weaponTypeMap[4]=tr("驅逐艦");weaponTypeMap[5]=tr("直升機");weaponTypeMap[6]=tr("坦克");weaponTypeMap[7]=tr("兩棲攻擊艦");weaponTypeMap[8]=tr("兩棲戰車");populateModel(); }

populateModel()函數的具體實現代碼如下:

void ModelEx::populateModel() {header<<tr("軍種")<<tr("種類")<<tr("武器");army<<1<<2<<3<<4<<2<<4<<3<<1;weaponType<<1<<3<<5<<7<<4<<8<<6<<2;weapon<<tr("B-2")<<tr("尼米茲級")<<tr("阿帕奇")<<tr("黃蜂級")<<tr("阿利伯克級")<<tr("AAAV")<<tr("M1A1")<<tr("F-22"); }

columnCount()函數中,因為模型的列固定為“3”,所以直接返回“3”。

int ModelEx::columnCount(const QModelIndex &parent) const { return 3; }

rowCount()函數返回模型的行數。

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

data()函數返回指定索引的數據,即將數值映射為文字。

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()];default:return QVariant();}}return QVariant(); }

下表為主要角色及其描述:

headerData()函數返回固定的表頭數據,設置水平表頭的標題,具體代碼如下:

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

(3)在源文件“main.cpp”中,將模型和視圖關聯,具體代碼如下:

#include <QApplication> #include "modelex.h" #include <QTableView> int main(int argc,char *argv[]) {QApplication a(argc,argv);ModelEx modelEx;QTableView view;view.setModel(&modelEx);view.setWindowTitle(QObject::tr("modelEx"));view.resize(400,400);view.show();return a.exec(); }

總結

以上是生活随笔為你收集整理的QT学习:模型练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。