mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 07:45:30 +08:00
qt 6.5.1 original
This commit is contained in:
50
examples/widgets/itemviews/simpletreemodel/CMakeLists.txt
Normal file
50
examples/widgets/itemviews/simpletreemodel/CMakeLists.txt
Normal file
@ -0,0 +1,50 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(simpletreemodel LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/simpletreemodel")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(simpletreemodel
|
||||
main.cpp
|
||||
treeitem.cpp treeitem.h
|
||||
treemodel.cpp treemodel.h
|
||||
)
|
||||
|
||||
set_target_properties(simpletreemodel PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(simpletreemodel PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(simpletreemodel_resource_files
|
||||
"default.txt"
|
||||
)
|
||||
|
||||
qt_add_resources(simpletreemodel "simpletreemodel"
|
||||
PREFIX
|
||||
"/"
|
||||
FILES
|
||||
${simpletreemodel_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS simpletreemodel
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
40
examples/widgets/itemviews/simpletreemodel/default.txt
Normal file
40
examples/widgets/itemviews/simpletreemodel/default.txt
Normal file
@ -0,0 +1,40 @@
|
||||
Getting Started How to familiarize yourself with Qt Designer
|
||||
Launching Designer Running the Qt Designer application
|
||||
The User Interface How to interact with Qt Designer
|
||||
|
||||
Designing a Component Creating a GUI for your application
|
||||
Creating a Dialog How to create a dialog
|
||||
Composing the Dialog Putting widgets into the dialog example
|
||||
Creating a Layout Arranging widgets on a form
|
||||
Signal and Slot Connections Making widget communicate with each other
|
||||
|
||||
Using a Component in Your Application Generating code from forms
|
||||
The Direct Approach Using a form without any adjustments
|
||||
The Single Inheritance Approach Subclassing a form's base class
|
||||
The Multiple Inheritance Approach Subclassing the form itself
|
||||
Automatic Connections Connecting widgets using a naming scheme
|
||||
A Dialog Without Auto-Connect How to connect widgets without a naming scheme
|
||||
A Dialog With Auto-Connect Using automatic connections
|
||||
|
||||
Form Editing Mode How to edit a form in Qt Designer
|
||||
Managing Forms Loading and saving forms
|
||||
Editing a Form Basic editing techniques
|
||||
The Property Editor Changing widget properties
|
||||
The Object Inspector Examining the hierarchy of objects on a form
|
||||
Layouts Objects that arrange widgets on a form
|
||||
Applying and Breaking Layouts Managing widgets in layouts
|
||||
Horizontal and Vertical Layouts Standard row and column layouts
|
||||
The Grid Layout Arranging widgets in a matrix
|
||||
Previewing Forms Checking that the design works
|
||||
|
||||
Using Containers How to group widgets together
|
||||
General Features Common container features
|
||||
Frames QFrame
|
||||
Group Boxes QGroupBox
|
||||
Stacked Widgets QStackedWidget
|
||||
Tab Widgets QTabWidget
|
||||
Toolbox Widgets QToolBox
|
||||
|
||||
Connection Editing Mode Connecting widgets together with signals and slots
|
||||
Connecting Objects Making connections in Qt Designer
|
||||
Editing Connections Changing existing connections
|
26
examples/widgets/itemviews/simpletreemodel/main.cpp
Normal file
26
examples/widgets/itemviews/simpletreemodel/main.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "treemodel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
#include <QTreeView>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(simpletreemodel);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QFile file(":/default.txt");
|
||||
file.open(QIODevice::ReadOnly);
|
||||
TreeModel model(file.readAll());
|
||||
file.close();
|
||||
|
||||
QTreeView view;
|
||||
view.setModel(&model);
|
||||
view.setWindowTitle(QObject::tr("Simple Tree Model"));
|
||||
view.show();
|
||||
return app.exec();
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(treeview))
|
||||
|
||||
HEADERS = treeitem.h \
|
||||
treemodel.h
|
||||
RESOURCES = simpletreemodel.qrc
|
||||
SOURCES = treeitem.cpp \
|
||||
treemodel.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/simpletreemodel
|
||||
INSTALLS += target
|
@ -0,0 +1,5 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource>
|
||||
<file>default.txt</file>
|
||||
</qresource>
|
||||
</RCC>
|
79
examples/widgets/itemviews/simpletreemodel/treeitem.cpp
Normal file
79
examples/widgets/itemviews/simpletreemodel/treeitem.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
/*
|
||||
treeitem.cpp
|
||||
|
||||
A container for items of data supplied by the simple tree model.
|
||||
*/
|
||||
|
||||
#include "treeitem.h"
|
||||
|
||||
//! [0]
|
||||
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
|
||||
: m_itemData(data), m_parentItem(parent)
|
||||
{}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
TreeItem::~TreeItem()
|
||||
{
|
||||
qDeleteAll(m_childItems);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void TreeItem::appendChild(TreeItem *item)
|
||||
{
|
||||
m_childItems.append(item);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
TreeItem *TreeItem::child(int row)
|
||||
{
|
||||
if (row < 0 || row >= m_childItems.size())
|
||||
return nullptr;
|
||||
return m_childItems.at(row);
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
int TreeItem::childCount() const
|
||||
{
|
||||
return m_childItems.count();
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
int TreeItem::columnCount() const
|
||||
{
|
||||
return m_itemData.count();
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
QVariant TreeItem::data(int column) const
|
||||
{
|
||||
if (column < 0 || column >= m_itemData.size())
|
||||
return QVariant();
|
||||
return m_itemData.at(column);
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
TreeItem *TreeItem::parentItem()
|
||||
{
|
||||
return m_parentItem;
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
int TreeItem::row() const
|
||||
{
|
||||
if (m_parentItem)
|
||||
return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this));
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [8]
|
33
examples/widgets/itemviews/simpletreemodel/treeitem.h
Normal file
33
examples/widgets/itemviews/simpletreemodel/treeitem.h
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef TREEITEM_H
|
||||
#define TREEITEM_H
|
||||
|
||||
#include <QVariant>
|
||||
#include <QList>
|
||||
|
||||
//! [0]
|
||||
class TreeItem
|
||||
{
|
||||
public:
|
||||
explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = nullptr);
|
||||
~TreeItem();
|
||||
|
||||
void appendChild(TreeItem *child);
|
||||
|
||||
TreeItem *child(int row);
|
||||
int childCount() const;
|
||||
int columnCount() const;
|
||||
QVariant data(int column) const;
|
||||
int row() const;
|
||||
TreeItem *parentItem();
|
||||
|
||||
private:
|
||||
QList<TreeItem *> m_childItems;
|
||||
QList<QVariant> m_itemData;
|
||||
TreeItem *m_parentItem;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // TREEITEM_H
|
177
examples/widgets/itemviews/simpletreemodel/treemodel.cpp
Normal file
177
examples/widgets/itemviews/simpletreemodel/treemodel.cpp
Normal file
@ -0,0 +1,177 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
/*
|
||||
treemodel.cpp
|
||||
|
||||
Provides a simple tree model to show how to create and use hierarchical
|
||||
models.
|
||||
*/
|
||||
|
||||
#include "treemodel.h"
|
||||
#include "treeitem.h"
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
//! [0]
|
||||
TreeModel::TreeModel(const QString &data, QObject *parent)
|
||||
: QAbstractItemModel(parent)
|
||||
{
|
||||
rootItem = new TreeItem({tr("Title"), tr("Summary")});
|
||||
setupModelData(data.split('\n'), rootItem);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
TreeModel::~TreeModel()
|
||||
{
|
||||
delete rootItem;
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
int TreeModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
|
||||
return rootItem->columnCount();
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
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());
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return Qt::NoItemFlags;
|
||||
|
||||
return QAbstractItemModel::flags(index);
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
||||
int role) const
|
||||
{
|
||||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
||||
return rootItem->data(section);
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if (!hasIndex(row, column, parent))
|
||||
return QModelIndex();
|
||||
|
||||
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);
|
||||
return QModelIndex();
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
QModelIndex TreeModel::parent(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QModelIndex();
|
||||
|
||||
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
|
||||
TreeItem *parentItem = childItem->parentItem();
|
||||
|
||||
if (parentItem == rootItem)
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(parentItem->row(), 0, parentItem);
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
int TreeModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
TreeItem *parentItem;
|
||||
if (parent.column() > 0)
|
||||
return 0;
|
||||
|
||||
if (!parent.isValid())
|
||||
parentItem = rootItem;
|
||||
else
|
||||
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
||||
|
||||
return parentItem->childCount();
|
||||
}
|
||||
//! [8]
|
||||
|
||||
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
||||
{
|
||||
QList<TreeItem *> parents;
|
||||
QList<int> indentations;
|
||||
parents << parent;
|
||||
indentations << 0;
|
||||
|
||||
int number = 0;
|
||||
|
||||
while (number < lines.count()) {
|
||||
int position = 0;
|
||||
while (position < lines[number].length()) {
|
||||
if (lines[number].at(position) != ' ')
|
||||
break;
|
||||
position++;
|
||||
}
|
||||
|
||||
const QString lineData = lines[number].mid(position).trimmed();
|
||||
|
||||
if (!lineData.isEmpty()) {
|
||||
// Read the column data from the rest of the line.
|
||||
const QStringList columnStrings =
|
||||
lineData.split(QLatin1Char('\t'), Qt::SkipEmptyParts);
|
||||
QList<QVariant> columnData;
|
||||
columnData.reserve(columnStrings.count());
|
||||
for (const QString &columnString : columnStrings)
|
||||
columnData << columnString;
|
||||
|
||||
if (position > indentations.last()) {
|
||||
// The last child of the current parent is now the new parent
|
||||
// unless the current parent has no children.
|
||||
|
||||
if (parents.last()->childCount() > 0) {
|
||||
parents << parents.last()->child(parents.last()->childCount()-1);
|
||||
indentations << position;
|
||||
}
|
||||
} else {
|
||||
while (position < indentations.last() && parents.count() > 0) {
|
||||
parents.pop_back();
|
||||
indentations.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
// Append a new item to the current parent's list of children.
|
||||
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
|
||||
}
|
||||
++number;
|
||||
}
|
||||
}
|
39
examples/widgets/itemviews/simpletreemodel/treemodel.h
Normal file
39
examples/widgets/itemviews/simpletreemodel/treemodel.h
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef TREEMODEL_H
|
||||
#define TREEMODEL_H
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QModelIndex>
|
||||
#include <QVariant>
|
||||
|
||||
class TreeItem;
|
||||
|
||||
//! [0]
|
||||
class TreeModel : public QAbstractItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TreeModel(const QString &data, QObject *parent = nullptr);
|
||||
~TreeModel();
|
||||
|
||||
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 &index) const override;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
private:
|
||||
void setupModelData(const QStringList &lines, TreeItem *parent);
|
||||
|
||||
TreeItem *rootItem;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // TREEMODEL_H
|
Reference in New Issue
Block a user