QString request = "SELECT " "b.id, " "b.id_parent " "FROM " "branch b " "WHERE " "b.isdeleted = 0"; m_branchModel->setQueryString(request); // это SqlQueryModel BranchesProxyModel *branchModel = new BranchesProxyModel(m_branchModel); // это моя модель от QAbstractProxyModel branchModel->setSourceModel(m_branchModel); ui->tvBranches->setModel(branchModel); // Это QTreeView ui->tvBranchesOrig->setModel(m_branchModel); // Это QTreeView
#ifndef BRANCHESPROXYMODEL_H#define BRANCHESPROXYMODEL_H#include <QAbstractProxyModel>class BranchesProxyModel : public QAbstractProxyModel{ Q_OBJECTpublic: explicit BranchesProxyModel(QObject *parent = 0); void setSourceModel(QAbstractItemModel* newSourceModel); QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex parent(const QModelIndex &child) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; bool hasChildren(const QModelIndex &parent = QModelIndex()) const; QModelIndex mapToSource(const QModelIndex &proxyIndex) const; QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;};#endif // BRANCHESPROXYMODEL_H
#include "branchesproxymodel.h"#include <QStandardItem>#include <QMessageBox>BranchesProxyModel::BranchesProxyModel(QObject *parent) : QAbstractProxyModel(parent){}void BranchesProxyModel::setSourceModel(QAbstractItemModel *sourceModel){ QAbstractProxyModel::setSourceModel(sourceModel); connect(sourceModel, &QAbstractItemModel::dataChanged, this, &BranchesProxyModel::dataChanged);}QModelIndex BranchesProxyModel::index(int row, int column, const QModelIndex &parent) const{ if (!sourceModel()) return QModelIndex(); if (column == 1) column = 2; QModelIndex index = sourceModel()->index(row, column); return createIndex(row, column, index.internalPointer());}QModelIndex BranchesProxyModel::parent(const QModelIndex &child) const{ if (!child.isValid()) return QModelIndex(); if (!sourceModel()) return QModelIndex(); return mapFromSource(sourceModel()->parent(child));}int BranchesProxyModel::rowCount(const QModelIndex &parent) const{ int result = 0; if (!sourceModel()) return result; int rowCount = sourceModel()->rowCount(); for (int i = 0; i < rowCount; i++) { qlonglong paretnId = sourceModel()->index(i, 1).data().toLongLong(); if (paretnId < 1) ++result; } return result;}int BranchesProxyModel::columnCount(const QModelIndex &parent) const{ return 2;}bool BranchesProxyModel::hasChildren(const QModelIndex &parent) const{ if (!sourceModel()) return false; qlonglong id = index(parent.row(), 0).data().toLongLong(); int rowCount = sourceModel()->rowCount(); for (int i = 0; i < rowCount; i++) { qlonglong paretnId = sourceModel()->index(i, 1).data().toLongLong(); if (paretnId == id) return true; } return false;}QModelIndex BranchesProxyModel::mapToSource(const QModelIndex &proxyIndex) const{ if(!proxyIndex.isValid()) return QModelIndex(); if (!sourceModel()) return QModelIndex(); return sourceModel()->index(proxyIndex.row(), proxyIndex.column(), mapToSource(proxyIndex.parent()));}QModelIndex BranchesProxyModel::mapFromSource(const QModelIndex &sourceIndex) const{ if (!sourceIndex.isValid()) return QModelIndex(); return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());}
CSqlTreeModel *sqlTreeModel = new CSqlTreeModel(this); sqlTreeModel->setTableName(""); sqlTreeModel->prepare(); ui->tvBranchesTree->setModel(sqlTreeModel);
void uoMainWindow::createTables(){ QString dbName = qApp->applicationDirPath() + QDir::separator()+"main.db";#ifdef Q_OS_LINUX QDir home = QDir::home(); if (!home.exists(".uoReceptor")) { home.mkpath(".uoReceptor"); } dbName = home.absolutePath() + "/.uoReceptor/" + QDir::separator()+"main.db";#endif m_dbMan->setupDataBase(dbName); m_receptTable = m_dbMan->addRefTable("ШаблоныPецептов","Шаблоны рецептов",6,120); m_receptTable->setOption(codeType_Number, true); m_receptTable->addField("Комментарий", fieldType_String,150); m_templField = m_receptTable->addField("Шаблон", fieldType_TextHTML); uoMdiObserver::instance()->m_receptTableId = m_receptTable->m_baseNom; uoDbRefTable* tmlpTabl = m_dbMan->addRefTable("Лекарства","Лекарства",6, 70); tmlpTabl->addField("ЛатИмя",fieldType_String,70)->m_descr = "Латинское наименование"; tmlpTabl->addField("Описание",fieldType_Text); tmlpTabl->setOption(codeType_Number, true); tmlpTabl = m_dbMan->addRefTable("Симптомы","Симптомы",6, 100); tmlpTabl->setOption(codeType_Number, true); tmlpTabl->addField("Описание",fieldType_Text); tmlpTabl = m_dbMan->addRefTable("Заболевания","Заболевания",6, 100); tmlpTabl->setOption(codeType_Number, true); tmlpTabl->addField("Описание",fieldType_Text); m_dbMan->restructuring();// Реструктуризация m_dbMan->createGuiFactory(this); m_receptTemplDocWidget = new QDockWidget(tr("Recept template"), this); m_receptTemplDocWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); uoUrl url = m_receptTable->getUoUrlByForm(uoFormT_Tree); url.setWithElem(true); m_docTreeFormRecept = m_dbMan->getForm(url); if (m_docTreeFormRecept) { m_docTreeFormRecept->setParent(m_receptTemplDocWidget); m_receptTemplDocWidget->setWidget(m_docTreeFormRecept); addDockWidget(Qt::LeftDockWidgetArea, m_receptTemplDocWidget); m_actionShowDocRecept = m_receptTemplDocWidget->toggleViewAction(); }}