編輯:關於 Android 開發
QML 中的 ListView、TableView、GridView 若只用 ListModel 餵資料,很快會撞到天花板:讀不到本機檔案系統、接不上既有的 SQL 資料庫。實務上的做法是反過來——用 C++ 實作資料模型,把展示與編輯交給 QML。本文先從最基礎的 QAbstractItemModel 開始。
Qt 提供 QAbstractTableModel、QSQLTableModel 等高階模型,但它們全都繼承自 QAbstractItemModel。理解這一支抽象類別的介面與運作機制,等於掌握了整個 Qt Model/View 體系的底層規則。
它是抽象類別,要能實例化,至少必須實作這五個方法:
int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QModelIndex parent(const QModelIndex &child) const;
這點和 QWidget 體系很不一樣。在 QML 中,TableViewColumn 透過 role 屬性對應模型欄位:
TableView {
id: tableView1
anchors.fill: parent
TableViewColumn {
width: 50
title: ""
role: "tagging"
}
TableViewColumn {
width: 80
title: "操作"
role: "name"
}
}
View 會呼叫模型的 roleNames() 來得知有哪些 role 可用。因此除了那五個純虛函式,還必須多實作一個:
QHash<int, QByteArray> roleNames() const;
class SqlMenuEntry : public QAbstractItemModel, public QQmlParserStatus
{
Q_OBJECT
public:
explicit SqlMenuEntry(QObject *parent = 0);
~SqlMenuEntry();
enum MenuEntryRoles {
idRole = Qt::UserRole + 1,
nameRole,
defaultEntryRole,
customEntryRole,
iconRole,
iconHoverRole
};
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex &child) const;
QHash<int, QByteArray> roleNames() const;
private:
QHash<int, QByteArray> mRoleNames;
QList<QHash<int, QVariant>> mRecords; // 真正的資料存放處
// 注意:QList 只能保存二維資料,無法表達樹狀節點,此處僅作示範
};
roleNames() 的實作非常直接,回傳成員即可:
QHash<int, QByteArray> SqlMenuEntry::roleNames() const
{
return mRoleNames;
}
而 mRoleNames 在建構子中初始化:
SqlMenuEntry::SqlMenuEntry(QObject *parent)
: QAbstractItemModel(parent)
{
mRoleNames[nameRole] = "name";
mRoleNames[idRole] = "menuid";
mRoleNames[iconRole] = "icon";
mRoleNames[defaultEntryRole] = "default";
mRoleNames[iconHoverRole] = "iconHover";
}
完成之後,QML 端就能直接用這些字串名稱取資料:
ListView {
model: MenuEntryModel { }
delegate: Item {
Column {
Text { text: name }
Text { text: icon }
}
}
}
int SqlMenuEntry::rowCount(const QModelIndex &parent) const
{
return mRecords.size();
}
int SqlMenuEntry::columnCount(const QModelIndex &parent) const
{
// QML 不用列取資料,預設回傳 1;
// 若不回傳 1,View 會判定為空表而不抓資料
return 1;
}
QModelIndex SqlMenuEntry::index(int row, int column, const QModelIndex &parent) const
{
if ((row >= 0) && (row < mRecords.size())) {
return createIndex(row, column);
}
return QModelIndex(); // 回傳無效的空索引
}
QModelIndex SqlMenuEntry::parent(const QModelIndex &child) const
{
return QModelIndex(); // 二維表的列沒有 parent 節點
}
QVariant SqlMenuEntry::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
return mRecords[index.row()][role];
}
return QVariant();
}
mRecords 的內容可依需求產生,例如用 QSqlQuery 從資料庫取回後逐列填入:
QHash<int, QVariant> row; row[nameRole] = "name1"; row[iconRole] = "icon1"; mRecords.append(row);
qmlRegisterType<SqlMenuEntry>("com.limutech.tv", 1, 0, "MenuEntryModel");
註冊後即可在 QML 中建立實例:
import com.limutech.tv 1.0
MenuEntryModel {
id: menuEntryModel
}
ListView {
model: menuEntryModel
// delegate ...
}
看過這六步就明白:要讓 TreeView 正常顯示,二維表模型並不夠——必須處理 parent 不為空的情況,模型本身也得能承載樹狀資料。下一篇就來動這場手術。
我們為什麼要在 Android 中用 RxJava:從需求變更的痛點談起
最近 RxJava 風生水起,不學一下都不好意思了。本人也是初學,一開始確實覺得程式碼好像更複雜、更難懂了。後来看了一篇外文,感同身受,簡單翻譯整理如下。本文著重說明使
BaseHttpListActivity:快速打造 Android 網路列表頁
BaseHttpListActivity:快速打造 Android 網路列表頁 在 Android 開發裡,「向伺服器取得列表、顯示、刷新、載入更多、快取」幾乎是每個 A
ListFragment 使用詳解:左右聯動列表與數據刷新
ListFragment 的基本概念 ListFragment 是 Fragment 的一種特殊形式,它封裝了一個 ListView,便於開發者以列表方式展示數據。相比直
自定義下拉重新整理動畫實作:仿百度外賣騎手重新整理效果
自定義下拉重新整理動畫實作:仿百度外賣騎手重新整理效果 下拉重新整理已不只是載入資料的動作,也是品牌識別與動畫細節的一部分。本文以百度外賣風格為例,說明如何拆解動畫、建立