文件保存树形结构数据(三)

2014-11-23 21:46:34 · 作者: · 浏览: 24
atic_cast(index.internalPointer());
}
else
{
return m_pTreeData; //这里不要返回NULL
}
}
QModelIndex TreeDataModel::parent( const QModelIndex & index ) const
{
if (index.isValid())
{
DataItem *pItem = dataFromIndex(index);
if (pItem)
{
DataItem *pParent = pItem->GetParent();
if (pParent)
{
DataItem *pGrandParent = pParent->GetParent();
if (pGrandParent)
{
int row = pGrandParent->indexOf(pParent);
return createIndex(row,index.column(),pParent);
}
}
}
}
return QModelIndex();
}
void TreeDataModel::SaveData( QDataStream &out )
{
m_pTreeData->SerialzeData(true,out);
}
void TreeDataModel::LoadData( QDataStream &in )
{
m_pTreeData->SerialzeData(false,in);
}
主框架类
这个类主要实现左边的树形把数据保存到文件中,然后在右边的树形结构加载显示出来。
[cpp]
class MainWidget:public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *patent = NULL);
~MainWidget();
protected slots:
void leftSelectBtnSlot();
void rightSelectBtnSlot();
void saveBtnSlot();
void loadBtnSlot();
private:
QSplitter *m_pSplitter;
QTreeView *m_pLeftTreeView;
QTreeView *m_pRightTreeView;
QPushButton *m_pLeftSaveBtn;
QPushButton *m_pRightLoadBtn;
QPushButton *m_pLeftSelectBtn;
QPushButton *m_pRightSelectBtn;
QLineEdit *m_pLeftLEdit;
QLineEdit *m_pRightLEdit;
QGridLayout *m_pLeftLayout;
QGridLayout *m_pRightLayout;
TreeDataModel *m_pLeftModel;
TreeDataModel *m_pRightModel;
};
[cpp]
MainWidget::MainWidget( QWidget *patent /*= NULL*/ ):QWidget(patent)
{
m_pLeftModel = new TreeDataModel();
m_pRightModel = new TreeDataModel();
m_pSplitter = new QSplitter(this);
QFrame *pLeftFrame = new QFrame(this);
QFrame *pRightFrame = new QFrame(this);
m_pLeftLayout = new QGridLayout(pLeftFrame);
m_pRightLayout = new QGridLayout(pRightFrame);
m_pLeftLEdit = new QLineEdit(this);
m_pRightLEdit = new QLineEdit(this);
m_pLeftSaveBtn = new QPushButton(tr("保存"),this);
m_pRightLoadBtn = new QPushButton(tr("加载"),this);
m_pLeftTreeView = new QTreeView(this);
m_pRightTreeView = new QTreeView(this);
m_pLeftSelectBtn = new QPushButton(tr("选择文件"),this);
m_pRightSelectBtn = new QPushButton(tr("选择文件"),this);
m_pRightLEdit->setReadOnly(true);
m_pLeftLayout->addWidget(m_pLeftSelectBtn,0,0,1,1);
m_pLeftLayout->addWidget(m_pLeftLEdit,0,1,1,1);
m_pLeftLayout->addWidget(m_pLeftSaveBtn,0,2,1,1);
m_pLeftLayout->addWidget(m_pLeftTreeView,1,0,3,3);
m_pRightLayout->addWidget(m_pRightSelectBtn,0,0,1,1);
m_pRightLayout->addWidget(m_pRightLEdit,0,1,1,1);
m_pRightLayout->addWidget(m_pRightLoadBtn,0,2,1,1);
m_pRightLayout->addWidget(m_pRightTreeView,1,0,3,3);
m_pLeftTreeView->setModel(m_pLeftModel);