#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QStandardItemModel>#include <QDebug>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QStandardItemModel* model; QList <QStandardItem*> item; QList <QStandardItem*> childItem;private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked();private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); model = new QStandardItemModel(this); ui->treeView->setModel(model);}MainWindow::~MainWindow(){ delete ui;}//--------добавить родителя void MainWindow::on_pushButton_clicked(){ item.append(new QStandardItem ("animals")); model->appendRow(item.last());}//-------добавить наследникаvoid MainWindow::on_pushButton_2_clicked(){ childItem.append(new QStandardItem("dog")); QModelIndex index = ui->treeView->selectionModel()->currentIndex(); item[index.row()]->appendRow(childItem.last());}//--------удалить итемvoid MainWindow::on_pushButton_3_clicked(){ QModelIndex index = ui->treeView->selectionModel()->currentIndex(); model-> removeRow(index.row(),index); qDebug() << index;}
model->removeRows(index.row(), 1, index.parent());
QModelIndex::internalPointer()
void MainWindow::saveState(void){ QSettings settings("TreeView", "settings_l"); settings.beginGroup("MainWindow"); for(int i=0; model->index(i,0).row()>-1; i++){ QModelIndex index = model->index(i,0); settings.setValue(QString::number(i), index); for(int ii=0; index.child(ii,0).row()>-1; ii++){ QModelIndex indexChild = index.child(ii,0); settings.setValue(QString::number(i)+QString::number(ii), indexChild); } } settings.endGroup();}
void MainWindow::saveState(void){ QSettings settings("TreeView", "settings_l"); settings.beginGroup("MainWindow"); for(int i=0; model->index(i,0).row()>-1; i++){ QPersistentModelIndex index = model->index(i,0); settings.setValue(QString::number(i), index); //for(int ii=0; index.child(ii,0).row()>-1; ii++){ // settings.setValue(QString::number(i)+QString::number(ii), index.child(ii,0)); // } } settings.endGroup();}