//主题文件
[.......]
#include "Index_treemodel.h"
[.......]
DBControl *db = new DBControl();
tabIndex = new QWidget();
IndexGridLayout = new QGridLayout(tabIndex);
IndexLineEdit = new QLineEdit(tabIndex);
IndexGridLayout->addWidget(IndexLineEdit, 0, 0, 1, 1);
IndexListView = new QTreeView(tabIndex);
IndexGridLayout->addWidget(IndexListView, 1, 0, 1, 1);
QStringList Iheaders;
Iheaders << tr("id") << tr("nodetext");
QString Iresult = db->IndexTree("QMYSQL","localhost","tree","root","123456","select id,nodetext from treeview order by nodetext Asc");
Index_TreeModel *amodel = new Index_TreeModel(Iheaders,Iresult);
IndexListView->setModel(amodel);
IndexListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
BWtabWidget->addTab(tabIndex, QString());
BWtabWidget->setTabText(BWtabWidget->indexOf(tabIndex), QApplication::translate("MainWindow", "\347\264\242\345\274\225", 0, QApplication::UnicodeUTF8));
[.......]
//Index_treemodel.h
#ifndef INDEX_TREEMODEL_H
#define INDEX_TREEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class Index_TreeItem;
//! [0]
class Index_TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
Index_TreeModel(const QStringList &headers, const QString &data,QObject *parent = 0);
~Index_TreeModel();
//! [0] //! [1]
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
//! [1]
//! [2]
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole);
bool setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role = Qt::EditRole);
bool insertRows(int position, int rows,
const QModelIndex &parent = QModelIndex());
bool removeRows(int position, int rows,
const QModelIndex &parent = QModelIndex());
private:
void setupModelData(const QStringList &lines, Index_TreeItem *parent);
Index_TreeItem *getItem(const QModelIndex &index) const;
Index_TreeItem *rootItem;
};
//! [2]
#endif
//Index_treemodel.cpp
#include <QtGui>
#include <QDebug>
#include <QtSql>
#include "Index_treeitem.h"
#include "Index_treemodel.h"
//! [0]
Index_TreeModel::Index_TreeModel(const QStringList &headers,const QString &data,QObject *parent) : QAbstractItemModel(parent)
{
QVector<QVariant> rootData;
foreach (QString header, headers)
rootData << header;
rootItem = new Index_TreeItem(rootData);
setupModelData(data.split(QString("\n")), rootItem);
}
//! [0]
//! [1]
Index_TreeModel::~Index_TreeModel()
{
delete rootItem;
}
//! [1]
//! [2]
int Index_TreeModel::columnCount(const QModelIndex &parent) const
{
return rootItem->columnCount();
}
//! [2]
QVariant Index_TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
Index_TreeItem *item = getItem(index);
return item->data(index.column());
}
//! [3]
Qt::ItemFlags Index_TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
//! [3]
//! [4]
Index_TreeItem *Index_TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
Index_TreeItem *item = static_cast<Index_TreeItem*>(index.internalPointer());
if (item) return item;
}
return rootItem;
}
//! [4]
QVariant Index_TreeModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
//! [5]
QModelIndex Index_TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
//! [5]
//! [6]
Index_TreeItem *parentItem = getItem(parent);
Index_TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
//! [6]
bool Index_TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
Index_TreeItem *parentItem = getItem(parent);
bool success = false;
QString tag;
QString newtag;
beginInsertRows(parent, position, position + rows - 1);
parentItem->insertChildren(position, rows, rootItem->columnCount());
success = true;
endInsertRows();
return success;
}
//! [7]
QModelIndex Index_TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
Index_TreeItem *childItem = getItem(index);
Index_TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
}
//! [7]
bool Index_TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
{
Index_TreeItem *parentItem = getItem(parent);
bool success = false;
beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->removeChildren(position, rows);
endRemoveRows();
return success;
}
//! [8]
int Index_TreeModel::rowCount(const QModelIndex &parent) const
{
Index_TreeItem *parentItem = getItem(parent);
return parentItem->childCount();
}
//! [8]
bool Index_TreeModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
if (role != Qt::EditRole)
return false;
Index_TreeItem *item = getItem(index);
return item->setData(index.column(), value);
}
bool Index_TreeModel::setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role)
{
if (role != Qt::EditRole || orientation != Qt::Horizontal)
return false;
return rootItem->setData(section, value);
}
void Index_TreeModel::setupModelData(const QStringList &lines, Index_TreeItem *parent)
{
QList<Index_TreeItem*> parents;
QList<int> indentations;
parents << parent;
indentations << 0;
int number = 0;
while (number < lines.count())
{
int position = 0;
QString lineData = lines[number].trimmed();
if (!lineData.isEmpty())
{
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
position = columnStrings[0].length();
QVector<QVariant> columnData;
for (int column = 0; column < columnStrings.count(); ++column)
columnData << columnStrings[column];
Index_TreeItem *parent = parents.last();
parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
parent->child(parent->childCount() - 1)->setData(0, columnData[1]);
parent->child(parent->childCount() - 1)->setData(1, columnData[0]);
}
number++;
}
}