編輯:關於 Android 開發
上一篇把二維表模型跑通了,這一篇要把它改造成能表達層次關係的樹狀模型。核心思路是:不再用 QList 存資料列,而是引入一個節點類別,讓每筆資料自己記住父子關係。
class TreeNode;
typedef TreeNode* TreeNodePtr;
class TreeNode
{
public:
explicit TreeNode();
TreeNodePtr parent() const;
void setParent(const TreeNodePtr p); // 設置父節點,根節點的父節點為 NULL
void appendNode(TreeNodePtr node);
void removeNode(int row);
void setData(int role, QVariant value);
QVariant data(int role);
QList<TreeNodePtr> childs; // 保存子節點指針
private:
TreeNodePtr mParent = NULL; // 父節點
QHash<int, QVariant> mRecord; // 一個節點保存一列資料,key 為 role
};
實作起來沒什麼難度:
TreeNodePtr TreeNode::parent() const
{
return mParent;
}
void TreeNode::setParent(const TreeNodePtr p)
{
this->mParent = p;
}
void TreeNode::appendNode(TreeNodePtr node)
{
childs.append(node);
}
void TreeNode::removeNode(int row)
{
childs.removeAt(row);
}
QVariant TreeNode::data(int role)
{
return mRecord[role];
}
void TreeNode::setData(int role, QVariant value)
{
mRecord[role] = value;
}
QList 存不了樹,把它換掉,改成維護根節點清單,並補上一個私有的掛載輔助函式:
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<TreeNodePtr> mRootEntrys; // 保存根節點
void _addEntryNode(TreeNodePtr node, TreeNodePtr parent = 0);
};
void SqlMenuEntry::_addEntryNode(TreeNodePtr node, TreeNodePtr parent)
{
if (parent == NULL) {
mRootEntrys.append(node);
} else {
node->setParent(parent);
parent->appendNode(node);
}
}
新增或清空整棵樹之前,用 beginResetModel() / endResetModel() 把這段操作包起來,View 才會正確刷新:
beginResetModel(); auto node = new TreeNode(); node->setData(nameRole, "parent 1"); node->setData(iconRole, "parenticon1"); _addEntryNode(node); // 加入第一個根節點 auto subNode = new TreeNode(); subNode->setData(nameRole, "child1"); _addEntryNode(subNode, node); // 加入子節點 endResetModel();
rowCount:parent 有效時回傳該節點的子節點數,否則回傳根節點數。
int SqlMenuEntry::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
// 節點資訊在 index() 時已存進 QModelIndex 的 internalPointer
TreeNodePtr parentNode = (TreeNodePtr)parent.internalPointer();
return parentNode->childs.size();
}
return mRootEntrys.size();
}
index:把對應的節點指標存進 internalPointer,後續函式才有辦法取回節點資料。
QModelIndex SqlMenuEntry::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid()) {
if ((row >= 0) && (row < mRootEntrys.size())) {
return createIndex(row, column, mRootEntrys.at(row));
}
} else {
TreeNodePtr parentNode = (TreeNodePtr)parent.internalPointer();
if ((row >= 0) && (row < parentNode->childs.size())) {
return createIndex(row, column, parentNode->childs[row]);
}
}
return QModelIndex();
}
parent:回傳某個節點的父節點索引,根節點回傳空索引。
QModelIndex SqlMenuEntry::parent(const QModelIndex &child) const
{
TreeNodePtr node = (TreeNodePtr)child.internalPointer();
if (node == NULL || node->parent() == NULL) {
return QModelIndex(); // 根節點沒有 parent
}
return createIndex(0, 0, node->parent());
}
data:直接從節點取出對應 role 的值。
QVariant SqlMenuEntry::data(const QModelIndex &index, int role) const
{
TreeNodePtr node = (TreeNodePtr)index.internalPointer();
if (node == NULL) {
return QVariant();
}
return node->data(role);
}
TreeView {
anchors.fill: parent
TableViewColumn {
title: "Name"
role: "name"
}
TableViewColumn {
title: "Entry"
role: "icon"
}
model: MenuEntryModel { }
}
整份改造其實圍繞一個機制運轉:QModelIndex 的 internalPointer。View 手上永遠只有 modelIndex,模型必須靠它把「這個索引對應哪個節點」偷渡進去,index() 負責寫入,rowCount()、parent()、data() 負責讀出。把這條線看懂,Qt 的 Model/View 設計就不再神祕。
接下來還要補上資料的新增、刪除與更新,以及對應的 beginInsertRows / endInsertRows 通知機制,留待後續補充。
我的 Android 第二課:專案結構、清單檔、DDMS 與打電話案例
我的 Android 第二課:專案結構、清單檔、DDMS 與打電話案例 各位好,小編又來分享知識了。上一篇筆記講解了 Android 工具與 SDK 的設定,今天則要帶大
Android 多圖片上傳實作:CodeIgniter 後端與 Volley 客戶端
Android 多圖片上傳實作:CodeIgniter 後端與 Volley 客戶端 在 App 中上傳多張圖片時,常需要同時傳遞參數。本文以 PHP CodeIgnit
安卓開源項目精選周報(第 1 期)
安卓開源項目精選周報(第 1 期) 由 OpenDigg 出品的安卓開源項目周報第一期上線了。本期彙集了 OpenDigg 過去一周新收錄的優質 Android 開源項目
box-flex 彈性可伸縮盒模型筆記
box-flex 彈性可伸縮盒模型筆記在移動端開發中,Flexbox 是一種非常實用的布局方案。它能夠在未知或動態容器內,讓子元素按照需要伸縮、排列與對齊。相比傳統的 f