1 0 "TypeRoot" "Магазин" "Кристалл"2 1 "TypeGoods" "Товары" ""3 2 "TypeBeer" "Пиво" ""4 3 "TypeMark" "Балтика№9" "22рубля"5 3 "TypeMark" "Балтика№3" "20рублей"
qRegisterMetaType<MyTypeClassOrStructure>(QLatin1String("MyTypeFromBase").latin1());
MyTypeStruc *mt = new MyTypeStruc; mt->id = BeerBarcode; mt->str = "Note"; QStandardItem *item = new QStandardItem(); item->setData(Text,Qt::DisplayRole); QVariant gh; gh.setValue(*mt); item->setData(gh,Qt::UserRole); model->setData(index, gh );
QWidget *TypeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option , const QModelIndex & index ) const{ QVariant originalValue = index.model()->data(index, Qt::UserRole); else if ( originalValue.canConvert<MyTypeStruc>() ) { QPushButton *button = new QPushButton(parent); button->setMaximumWidth(16); button->setText("..."); connect(button, SIGNAL(activated(int)), this, SLOT(emitCommitData())); return button; }}
query.prepare( QString("SELECT %1 FROM %2 WHERE %3 = %4") .arg(QSqlTreeViewModel::primaryFieldName) .arg(QSqlTreeViewModel::tableName) .arg(QSqlTreeViewModel::parentFieldName) .arg(id) );
QStandardItemModel *model = new QStandardItemModel; model->setColumnCount(2); model->setHeaderData(0, Qt::Horizontal, tr("Key")); model->setHeaderData(1, Qt::Horizontal, tr("Value")); TreeView1->setModel(model); QStandardItem *Root1 = new QStandardItem("Root1"); QVariant itemData = 1; // для теста присвоим значение 1 Root1->setData(itemData, Qt::UserRole); // и установим его в итем model->setItem(0,0,Root1); // первая строка, первая колонка QStandardItem *ChdRoot1 = new QStandardItem("Child of Root1"); // создадим дочерний элемент itemData = 2; ChdRoot1->setData(itemData, Qt::UserRole); Root1->setChild(0,0,ChdRoot1); // установим ребенка в первую строку, первую колонку родительского элемента (не глобальной модели!) QStandardItem *ChdValue1 = new QStandardItem("Value of Child"); itemData = 3; ChdValue1->setData(itemData, Qt::UserRole); Root1->setChild(0,1,ChdValue1); // установим ребенка во вторую колонку, первой строки для родителя
void SqlTreeModel::refreshAllString(int id, QStandardItem *parent){ QSqlQuery q; if (id==-1) id = 0; q.exec(QString("SELECT * FROM %1 WHERE %2=%3").arg(tableName).arg(parentFieldName).arg(id)); while ( q.next()) { QStandardItem *item = new QStandardItem(q.value(2).toString()); QList<QStandardItem *> itemColumn; itemColumn << item; if (q.record().count()>2) { for (int i=3; i<q.record().count(); ++i) { QStandardItem *columnItem = new QStandardItem(q.value(i).toString()); itemColumn << columnItem; } } parent->appendRow(itemColumn); refreshAllString(q.value(q.record().indexOf(primaryFieldName)).toInt(), item); }}
C++ (Qt)#ifndef TREEMODEL_H#define TREEMODEL_H #include <QAbstractItemModel>#include <QModelIndex>#include <QVariant> class TreeItem; class TreeModel : public QAbstractItemModel{ Q_OBJECT public: TreeModel(const QString &table, QObject *parent = 0); ~TreeModel(); QVariant data(const QModelIndex &index, int role) const; Qt::ItemFlags flags(const QModelIndex &index) 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; QString primaryFieldName; QString parentFieldName; QString tableName; private: //void setupModelData(); void init(int id = 0, TreeItem *parent = 0); void setupFieldName(const QString &table, const QString primaryFieldName = "ID", const QString parentFieldName = "P_ID"); TreeItem *rootItem;}; #endif-------------------#include <QtGui>#include <QtSql> #include "treeitem.h"#include "treemodel.h" #define DEFAULT_PRIMARY_FIELD_NAME "ID"#define DEFAULT_PARENT_FIELD_NAME "P_ID" TreeModel::TreeModel(const QString &table, QObject *parent) : QAbstractItemModel(parent){ setupFieldName(table); // setupModelData();}//TreeModel::~TreeModel(){ delete rootItem;}//int TreeModel::columnCount(const QModelIndex &parent) const{ if (parent.isValid()) return static_cast<TreeItem*>(parent.internalPointer())->columnCount(); else return rootItem->columnCount();}//QVariant TreeModel::data(const QModelIndex &index, int role) const{ if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole) return QVariant(); TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); return item->data(index.column());}//Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const{ if (!index.isValid()) return Qt::ItemIsEnabled; return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}//QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const{ if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return rootItem->data(section); return QVariant();}//QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)const{ TreeItem *parentItem; if (!parent.isValid()) parentItem = rootItem; else parentItem = static_cast<TreeItem*>(parent.internalPointer()); TreeItem *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); else return QModelIndex();}//QModelIndex TreeModel::parent(const QModelIndex &index) const{ if (!index.isValid()) return QModelIndex(); TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer()); TreeItem *parentItem = childItem->parent(); if (parentItem == rootItem) return QModelIndex(); return createIndex(parentItem->row(), 0, parentItem);}//int TreeModel::rowCount(const QModelIndex &parent) const{ TreeItem *parentItem; if (!parent.isValid()) parentItem = rootItem; else parentItem = static_cast<TreeItem*>(parent.internalPointer()); return parentItem->childCount();}// Это матырства моего мозга//----------------------------------------------------------------------/*void TreeModel::setupModelData(){ QList<QVariant> rootData; rootData << "Field 1"<< "Field 2"<< "Field 3"<< "Field 4"; rootItem = new TreeItem(rootData); init(0, rootItem); }//void init(int id, TreeItem *parent){ QList<TreeItem*> parents; parents << parent; QSqlQuery q; q.exec(QString("SELECT * FROM %1 WHERE %2=%3").arg(tableName).arg(parentFieldName).arg(id)); while ( q.next()) { QList<QVariant> columnData; if (q.record().count()>0) { for (int column=2; column<q.record().count(); column++) { columnData << q.value(column); } } parents.last()->appendChild(new TreeItem(columnData, parents.last())); init(q.value(q.record().indexOf(primaryFieldName)).toInt(), parents); }}*///void TreeModel::setupFieldName(const QString &table, const QString primaryFieldN, const QString parentFieldN ){ tableName.clear(); primaryFieldName.clear(); parentFieldName.clear(); tableName.append(table); primaryFieldName.append(primaryFieldN); parentFieldName.append(parentFieldN); }
C++ (Qt)void CDataBase::GetData(map<int, pair<int, QString> > &nodesData){ if (!database->isOpen()) return ; QSqlQueryModel model; model.setQuery("SELECT * FROM [nodes] ORDER BY node_id");// ВАЖНО: обязательна сортировка по ID - узла, т.к. после будет один линейный проход// и для каждого последующего узла выше в таблице должен существовать родитель for (long i = 0; i < model.rowCount(); i++) nodesData[model.data(model.index(i, 0)).toInt()] = make_pair(model.data(model.index(i, 1)).toInt(), model.data(model.index(i, 2)).toString());}
C++ (Qt)void MainWindow::CreateTreeView (void){ map<int, pair<int, QString> >nodesData; base->GetData(nodesData); treeView = ui->treeWidget; map <int, pair<int, QString> >::iterator iIter; map<int, QTreeWidgetItem* >treeItems; treeView->blockSignals(true); for (iIter = nodesData.begin(); iIter != nodesData.end(); iIter++) { int k = iIter->first; QTreeWidgetItem *item; if (iIter->second.first == 0) // root node { item = new QTreeWidgetItem(treeView); item->setText(0, iIter->second.second); } else { item = new QTreeWidgetItem(treeItems[iIter->second.first]); item->setText(0, iIter->second.second); } item->setData(1, 1, iIter->first); treeItems[k] = item; } treeView->blockSignals(false);}