QTreeView
QTreeView和QTableView有些相似,只是数据有点不一样
初始化:
{
d_tree_view = new QTreeView();
d_tree_view->setAnimated(true);
d_tree_view->setExpandsOnDoubleClick(true);
d_tree_view->setContextMenuPolicy(Qt::CustomContextMenu);
d_tree_view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
d_model = new PCModel();
d_tree_view->setModel(d_model);
layout_root->addWidget(d_tree_view);
}
// 习惯上连上一些槽函数,去操作QTreeView
// 比如点击、双击、右键菜单
connect(d_tree_view, SIGNAL(clicked(QModelIndex)), this, SLOT(slot_select_sw(QModelIndex)) );
connect(d_tree_view, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slot_show_sw(QModelIndex)) );
connect(d_tree_view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slot_show_context_menu(QPoint)) );
Item数据,主要是父子节点
PCItem .h
#ifndef PCITEM_H
#define PCITEM_H
#include <QObject>
#include <QVector>
#include <QDebug>
class PCItem : public QObject
{
Q_OBJECT
public:
explicit PCItem(QObject* parent = 0);
~PCItem();
int get_id() const;
void clear();
bool delete_item(PCItem* item);
public:
int get_index(PCItem* item);
private:
int d_id;
public:
PCItem* d_parent;
// children
QVector<PCItem*> d_children;
public:
QString d_name;
QString d_age;
};
#endif // PCITEM_H
PCItem .cpp
#include "PCItem.h"
#include "coreutil/IdUtil.h"
#include <QDir>
#include <QDateTime>
#include <QFileInfo>
#include <iostream>
PCItem::PCItem(QObject* parent)
: QObject(parent),
d_parent(0),
d_children(QVector<PCItem*>())
{
d_id = IdUtil::get_id();
}
PCItem::~PCItem()
{
QVector<PCItem*>::iterator begin = d_children.begin();
QVector<PCItem*>::iterator end = d_children.end();
while(begin != end) {
delete *begin;
*begin = 0;
++begin;
}
}
int
PCItem::get_id() const
{
return d_id;
}
void
PCItem::clear()
{
}
void
PCItem::clear_read_info()
{
}
Model
PCModel.h
#ifndef PCMODEL_H
#define PCMODEL_H
#include <QAbstractItemModel>
class PCItem;
/**
*
*/
class PCModel : public QAbstractItemModel
{
Q_OBJECT
public:
PCModel(QObject* parent = 0);
~PCModel();
PCItem* get_item();
void clear_all_info();
void refresh_table();
public:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex& child) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const;//
QVariant data(const QModelIndex& idx, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex& idx) const;
// when QCheckBox need(reference)
//bool setData(const QModelIndex& idx, const QVariant& value, int role = Qt::EditRole);
private:
PCItem* d_item_root;
};
#endif // PCMODEL_H
PCModel.cpp
#include "PCModel.h"
#include "PCItem.h"
#include "enum/GlobalAction.h"
#include "validvalue/FileUtil.h"
#include <QMessageBox>
#include <QFileInfo>
#include <QApplication>
#include <QStyle>
#include <QDebug>
using namespace GlobalAction;
PCModel::PCModel(QObject* parent)
: QAbstractItemModel(parent),
d_item_root(new PCItem())
{
}
PCModel::~PCModel()
{
//qDebug() << __FILE__ << __LINE__ << " ~PCModel ";
if( d_item_root )
{
d_item_root->deleteLater();
d_item_root = 0;
}
}
PCItem*
PCModel::get_item()
{
return d_item_root;
}
void
PCModel::clear_all_info()
{
beginResetModel();
d_item_root->clear();
endResetModel();
}
void
PCModel::refresh_table()
{
beginResetModel();
endResetModel();
}
QVariant
PCModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation);
if( role == Qt::DisplayRole )
{
switch(section){
case 0:
return tr("你的头部标题名称");
default:
break;
}
}
return QVariant();
}
int
PCModel::rowCount(const QModelIndex& parent) const
{
if( parent.isValid() )
{
PCItem* item = (PCItem*)parent.internalPointer();
//qDebug() << __FILE__ << __LINE__ << " rowCount isValid " << item->d_children.size();
return item->d_children.size();
}
else
{
return d_item_root->d_children.size();
}
return 0;
}
int
PCModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 1;
}
QModelIndex
PCModel::parent(const QModelIndex& child) const
{
if( !child.isValid() || !child.internalPointer() )
{
return QModelIndex();
}
PCItem* item = (PCItem*)child.internalPointer();
//qDebug() << __FILE__ << __LINE__ << " item = " << item;
PCItem* item_parent = item->d_parent;
if( item_parent )
{
PCItem* item_parent_p = item_parent->d_parent;
if( item_parent_p )
{
//const int& idx = item_parent_p->d_children.indexOf(item_parent);
const int& idx = item_parent_p->get_index(item_parent);
if( -1 != idx )
{
return createIndex(idx, 0, item_parent);
}
}
}
return QModelIndex();
}
QModelIndex
PCModel::index(int row, int column, const QModelIndex& parent) const
{
if( parent.isValid() )
{
PCItem* item = (PCItem*)parent.internalPointer();
//qDebug() << __FILE__ << __LINE__ << " row = " << row;
if( row > -1 && row < item->d_children.size() )
{
if( item->d_children.at(row)->d_is_show_name )
{
return createIndex(row, column, item->d_children.at(row));
}
}
}
else
{
if( row > -1 && row < d_item_root->d_children.size() )
{
return createIndex(row, column, d_item_root->d_children.at(row));
}
}
return QModelIndex();
}
bool
PCModel::hasChildren(const QModelIndex& idx) const
{
if( !idx.isValid() )
{
return true;
}
//qDebug() << __FILE__ << __LINE__ << " idx = " << idx;
PCItem* item = (PCItem*)idx.internalPointer();
if( item && item->d_children.size() > 0 )
{
return true;
}
return false;
}
Qt::ItemFlags
PCModel::flags(const QModelIndex& idx) const
{
return QAbstractItemModel::flags(idx);
}
QVariant
PCModel::data(const QModelIndex& idx, int role) const
{
if( !idx.isValid() )
{
return QVariant();
}
PCItem* item = (PCItem*)idx.internalPointer();
if( role == Qt::DecorationRole )
{
// 图标
//qApp->style()->standardIcon()
return QIcon(FileUtil::get_icon_name_from_name(item->d_name));
}
if( role == Qt::DisplayRole || role == Qt::EditRole )
{
switch( idx.column() ) {
case 0:
{
return item->d_name;
}
default:
qWarning("data: invalid display value column %d", idx.column());
return QVariant();
}
}
if( role == Qt::UserRole )
{
//qDebug() << __FILE__ << __LINE__ << " UserRole";
//return "UserRole";
}
return QVariant();
}
//bool
//PCModel::setData(const QModelIndex& idx, const QVariant& value, int role)
//{
// if( !idx.isValid() )
// {
// return false;
// }
// if( role == Qt::UserRole )
// {
// TODO
// emit dataChanged(idx, idx); // to refresh QCheckBox
// return true;
// }
// return false;
//}