#include <QtGui/QApplication>
#include <QtCore/QModelIndex>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
#include <QTreeView>
#include <QtDebug>
int main(int argc, char *argv[])
{
treeView.setModel(&model);
// (Case 1)
// QStandardItem* rootItem = model.invisibleRootItem();
// rootItem->insertRow(0, new QStandardItem("Child Item"));
// QStandardItem* childItem = rootItem->child(0, 0);
// if (childItem)
// childItem->insertRow(0, new QStandardItem("Grandchild Item"));
// (Case 2)
bool isSuccess = false;
if (childItemIndex.isValid()) {
isSuccess = model.insertRow(0, childItemIndex); // make a grandchild, but the item does not appear
}
qDebug() << "isSuccess : " << isSuccess; // true
qDebug() << "childItemIndex.isValid : " << childItemIndex.isValid(); // true
QModelIndex grandchildItemIndex
= model.
index(0,
0, childItemIndex
);
qDebug() << "grandchildItemIndex.isValid() : " << grandchildItemIndex.isValid(); // false
treeView.show();
return application.exec();
}
#include <QtGui/QApplication>
#include <QtCore/QModelIndex>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
#include <QTreeView>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QStandardItemModel model;
QTreeView treeView;
treeView.setModel(&model);
// (Case 1)
// QStandardItem* rootItem = model.invisibleRootItem();
// rootItem->insertRow(0, new QStandardItem("Child Item"));
// QStandardItem* childItem = rootItem->child(0, 0);
// if (childItem)
// childItem->insertRow(0, new QStandardItem("Grandchild Item"));
// (Case 2)
model.insertRow(0, new QStandardItem("Child Item"));
QModelIndex childItemIndex = model.index(0, 0, QModelIndex());
bool isSuccess = false;
if (childItemIndex.isValid()) {
isSuccess = model.insertRow(0, childItemIndex); // make a grandchild, but the item does not appear
}
qDebug() << "isSuccess : " << isSuccess; // true
childItemIndex = model.index(0, 0, QModelIndex());
qDebug() << "childItemIndex.isValid : " << childItemIndex.isValid(); // true
QModelIndex grandchildItemIndex = model.index(0, 0, childItemIndex);
qDebug() << "grandchildItemIndex.isValid() : " << grandchildItemIndex.isValid(); // false
treeView.show();
return application.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks