mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-04 00:05:25 +08:00
6.5.3 clean
This commit is contained in:
@ -0,0 +1,40 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(simpledommodel LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/simpledommodel")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Xml)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(simpledommodel
|
||||
domitem.cpp domitem.h
|
||||
dommodel.cpp dommodel.h
|
||||
main.cpp
|
||||
mainwindow.cpp mainwindow.h
|
||||
)
|
||||
|
||||
set_target_properties(simpledommodel PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(simpledommodel PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
Qt6::Xml
|
||||
)
|
||||
|
||||
install(TARGETS simpledommodel
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
@ -0,0 +1,62 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "domitem.h"
|
||||
|
||||
#include <QtXml>
|
||||
|
||||
//! [0]
|
||||
DomItem::DomItem(const QDomNode &node, int row, DomItem *parent)
|
||||
: domNode(node),
|
||||
//! [0]
|
||||
// Record the item's location within its parent.
|
||||
//! [1]
|
||||
parentItem(parent),
|
||||
rowNumber(row)
|
||||
{}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
DomItem::~DomItem()
|
||||
{
|
||||
qDeleteAll(childItems);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
QDomNode DomItem::node() const
|
||||
{
|
||||
return domNode;
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
DomItem *DomItem::parent()
|
||||
{
|
||||
return parentItem;
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
DomItem *DomItem::child(int i)
|
||||
{
|
||||
DomItem *childItem = childItems.value(i);
|
||||
if (childItem)
|
||||
return childItem;
|
||||
|
||||
// if child does not yet exist, create it
|
||||
if (i >= 0 && i < domNode.childNodes().count()) {
|
||||
QDomNode childNode = domNode.childNodes().item(i);
|
||||
childItem = new DomItem(childNode, i, this);
|
||||
childItems[i] = childItem;
|
||||
}
|
||||
return childItem;
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
int DomItem::row() const
|
||||
{
|
||||
return rowNumber;
|
||||
}
|
||||
//! [6]
|
@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DOMITEM_H
|
||||
#define DOMITEM_H
|
||||
|
||||
#include <QDomNode>
|
||||
#include <QHash>
|
||||
|
||||
//! [0]
|
||||
class DomItem
|
||||
{
|
||||
public:
|
||||
DomItem(const QDomNode &node, int row, DomItem *parent = nullptr);
|
||||
~DomItem();
|
||||
DomItem *child(int i);
|
||||
DomItem *parent();
|
||||
QDomNode node() const;
|
||||
int row() const;
|
||||
|
||||
private:
|
||||
QDomNode domNode;
|
||||
QHash<int, DomItem *> childItems;
|
||||
DomItem *parentItem;
|
||||
int rowNumber;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // DOMITEM_H
|
@ -0,0 +1,153 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "dommodel.h"
|
||||
#include "domitem.h"
|
||||
|
||||
#include <QtXml>
|
||||
|
||||
//! [0]
|
||||
DomModel::DomModel(const QDomDocument &document, QObject *parent)
|
||||
: QAbstractItemModel(parent),
|
||||
domDocument(document),
|
||||
rootItem(new DomItem(domDocument, 0))
|
||||
{
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
DomModel::~DomModel()
|
||||
{
|
||||
delete rootItem;
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
int DomModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return 3;
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
QVariant DomModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QVariant();
|
||||
|
||||
if (role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
|
||||
const DomItem *item = static_cast<DomItem*>(index.internalPointer());
|
||||
|
||||
const QDomNode node = item->node();
|
||||
//! [3] //! [4]
|
||||
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
return node.nodeName();
|
||||
case 1:
|
||||
{
|
||||
const QDomNamedNodeMap attributeMap = node.attributes();
|
||||
QStringList attributes;
|
||||
for (int i = 0; i < attributeMap.count(); ++i) {
|
||||
QDomNode attribute = attributeMap.item(i);
|
||||
attributes << attribute.nodeName() + "=\""
|
||||
+ attribute.nodeValue() + '"';
|
||||
}
|
||||
return attributes.join(' ');
|
||||
}
|
||||
case 2:
|
||||
return node.nodeValue().split('\n').join(' ');
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return Qt::NoItemFlags;
|
||||
|
||||
return QAbstractItemModel::flags(index);
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
QVariant DomModel::headerData(int section, Qt::Orientation orientation,
|
||||
int role) const
|
||||
{
|
||||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
||||
switch (section) {
|
||||
case 0:
|
||||
return tr("Name");
|
||||
case 1:
|
||||
return tr("Attributes");
|
||||
case 2:
|
||||
return tr("Value");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if (!hasIndex(row, column, parent))
|
||||
return QModelIndex();
|
||||
|
||||
DomItem *parentItem;
|
||||
|
||||
if (!parent.isValid())
|
||||
parentItem = rootItem;
|
||||
else
|
||||
parentItem = static_cast<DomItem*>(parent.internalPointer());
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
DomItem *childItem = parentItem->child(row);
|
||||
if (childItem)
|
||||
return createIndex(row, column, childItem);
|
||||
return QModelIndex();
|
||||
}
|
||||
//! [8]
|
||||
|
||||
//! [9]
|
||||
QModelIndex DomModel::parent(const QModelIndex &child) const
|
||||
{
|
||||
if (!child.isValid())
|
||||
return QModelIndex();
|
||||
|
||||
DomItem *childItem = static_cast<DomItem*>(child.internalPointer());
|
||||
DomItem *parentItem = childItem->parent();
|
||||
|
||||
if (!parentItem || parentItem == rootItem)
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(parentItem->row(), 0, parentItem);
|
||||
}
|
||||
//! [9]
|
||||
|
||||
//! [10]
|
||||
int DomModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.column() > 0)
|
||||
return 0;
|
||||
|
||||
DomItem *parentItem;
|
||||
|
||||
if (!parent.isValid())
|
||||
parentItem = rootItem;
|
||||
else
|
||||
parentItem = static_cast<DomItem*>(parent.internalPointer());
|
||||
|
||||
return parentItem->node().childNodes().count();
|
||||
}
|
||||
//! [10]
|
@ -0,0 +1,38 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DOMMODEL_H
|
||||
#define DOMMODEL_H
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QDomDocument>
|
||||
#include <QModelIndex>
|
||||
|
||||
class DomItem;
|
||||
|
||||
//! [0]
|
||||
class DomModel : public QAbstractItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DomModel(const QDomDocument &document, QObject *parent = nullptr);
|
||||
~DomModel();
|
||||
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation,
|
||||
int role = Qt::DisplayRole) const override;
|
||||
QModelIndex index(int row, int column,
|
||||
const QModelIndex &parent = QModelIndex()) const override;
|
||||
QModelIndex parent(const QModelIndex &child) const override;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
private:
|
||||
QDomDocument domDocument;
|
||||
DomItem *rootItem;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // DOMMODEL_H
|
@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
MainWindow window;
|
||||
window.resize(640, 480);
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "dommodel.h"
|
||||
|
||||
#include <QDomDocument>
|
||||
#include <QTreeView>
|
||||
#include <QMenuBar>
|
||||
#include <QFileDialog>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent),
|
||||
model(new DomModel(QDomDocument(), this)),
|
||||
view(new QTreeView(this))
|
||||
{
|
||||
fileMenu = menuBar()->addMenu(tr("&File"));
|
||||
fileMenu->addAction(tr("&Open..."), QKeySequence::Open, this, &MainWindow::openFile);
|
||||
fileMenu->addAction(tr("E&xit"), QKeySequence::Quit, this, &QWidget::close);
|
||||
|
||||
view->setModel(model);
|
||||
|
||||
setCentralWidget(view);
|
||||
setWindowTitle(tr("Simple DOM Model"));
|
||||
}
|
||||
|
||||
void MainWindow::openFile()
|
||||
{
|
||||
QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"),
|
||||
xmlPath, tr("XML files (*.xml);;HTML files (*.html);;"
|
||||
"SVG files (*.svg);;User Interface files (*.ui)"));
|
||||
|
||||
if (!filePath.isEmpty()) {
|
||||
QFile file(filePath);
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
QDomDocument document;
|
||||
if (document.setContent(&file)) {
|
||||
DomModel *newModel = new DomModel(document, this);
|
||||
view->setModel(newModel);
|
||||
delete model;
|
||||
model = newModel;
|
||||
xmlPath = filePath;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QString>
|
||||
|
||||
class DomModel;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QMenu;
|
||||
class QTreeView;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void openFile();
|
||||
|
||||
private:
|
||||
DomModel *model;
|
||||
QMenu *fileMenu;
|
||||
QString xmlPath;
|
||||
QTreeView *view;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
@ -0,0 +1,14 @@
|
||||
HEADERS = domitem.h \
|
||||
dommodel.h \
|
||||
mainwindow.h
|
||||
SOURCES = domitem.cpp \
|
||||
dommodel.cpp \
|
||||
main.cpp \
|
||||
mainwindow.cpp
|
||||
QT += xml widgets
|
||||
requires(qtConfig(filedialog))
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/simpledommodel
|
||||
INSTALLS += target
|
||||
|
Reference in New Issue
Block a user