241 lines
6.7 KiB
C++
241 lines
6.7 KiB
C++
#include <QApplication>
|
|
#include <QWidget>
|
|
|
|
#ifdef _WINDOWS
|
|
#include <QTextCodec>
|
|
#include <Windows.h>
|
|
#endif
|
|
#include <QStyle>
|
|
#include <QWidget>
|
|
#include <QtWidgets>
|
|
#include "GenerateMockData.h"
|
|
#include "base/Graphic.h"
|
|
#include "base/Plot.h"
|
|
#include "component/Table/Table_View.h"
|
|
#include "component/Tree/Tree_View.h"
|
|
#include "component/simple_widget.h"
|
|
#include "plottable/Afterglow.h"
|
|
#include "plottable/Planisphere.h"
|
|
#include "plottable/Planisphere_p.h"
|
|
#include "plottable/Spectrum.h"
|
|
#include "plottable/Spectrum_p.h"
|
|
#include "plottable/WaterFall.h"
|
|
#include "plottable/WaterFall_p.h"
|
|
#include "DemoGallery/AfterglowPlot.h"
|
|
#include "DemoGallery/DemoGallery.h"
|
|
#include "DemoGallery/PlanispherePlot.h"
|
|
#include "DemoGallery/SpectrumPlot.h"
|
|
#include "DemoGallery/WaterFallPlot.h"
|
|
|
|
#include <QApplication>
|
|
#include <QTreeWidget>
|
|
#include <QTreeWidgetItem>
|
|
#include <QVBoxLayout>
|
|
#include <QWidget>
|
|
|
|
|
|
struct TreeNode {
|
|
QStringList data; // 节点数据,每个字段一个字符串
|
|
QList<TreeNode*> children; // 子节点列表
|
|
TreeNode* parent; // 父节点指针
|
|
|
|
TreeNode(const QStringList& data, TreeNode* parent = nullptr)
|
|
: data(data), parent(parent) {}
|
|
~TreeNode() {
|
|
qDeleteAll(children);
|
|
}
|
|
};
|
|
|
|
|
|
class TreeModel : public QAbstractItemModel {
|
|
public:
|
|
TreeModel(const QStringList& headers, QObject* parent = nullptr)
|
|
: QAbstractItemModel(parent), headers(headers) {
|
|
rootItem = new TreeNode(headers);
|
|
}
|
|
|
|
~TreeModel() {
|
|
delete rootItem;
|
|
}
|
|
|
|
// 必要的虚函数实现
|
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override {
|
|
TreeNode* parentNode = nodeFromIndex(parent);
|
|
return parentNode->children.count();
|
|
}
|
|
|
|
int columnCount(const QModelIndex& parent = QModelIndex()) const override {
|
|
return headers.count();
|
|
}
|
|
|
|
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
|
|
if (!index.isValid() || role != Qt::DisplayRole)
|
|
return QVariant();
|
|
|
|
TreeNode* item = nodeFromIndex(index);
|
|
return item->data.value(index.column());
|
|
}
|
|
|
|
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override {
|
|
if (!hasIndex(row, column, parent))
|
|
return QModelIndex();
|
|
|
|
TreeNode* parentNode = nodeFromIndex(parent);
|
|
TreeNode* childItem = parentNode->children.value(row);
|
|
if (childItem)
|
|
return createIndex(row, column, childItem);
|
|
else
|
|
return QModelIndex();
|
|
}
|
|
|
|
QModelIndex parent(const QModelIndex& index) const override {
|
|
if (!index.isValid())
|
|
return QModelIndex();
|
|
|
|
TreeNode* childItem = nodeFromIndex(index);
|
|
TreeNode* parentItem = childItem->parent;
|
|
|
|
if (parentItem == rootItem || !parentItem)
|
|
return QModelIndex();
|
|
|
|
int row = parentItem->parent->children.indexOf(parentItem);
|
|
return createIndex(row, 0, parentItem);
|
|
}
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
|
return headers.value(section);
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
// 添加节点的函数
|
|
void addNode(const QStringList& data, const QModelIndex& parent = QModelIndex()) {
|
|
TreeNode* parentNode = nodeFromIndex(parent);
|
|
beginInsertRows(parent, parentNode->children.count(), parentNode->children.count());
|
|
TreeNode* newNode = new TreeNode(data, parentNode);
|
|
parentNode->children.append(newNode);
|
|
endInsertRows();
|
|
}
|
|
|
|
private:
|
|
TreeNode* rootItem;
|
|
QStringList headers;
|
|
|
|
TreeNode* nodeFromIndex(const QModelIndex& index) const {
|
|
if (index.isValid())
|
|
return static_cast<TreeNode*>(index.internalPointer());
|
|
else
|
|
return rootItem;
|
|
}
|
|
};
|
|
|
|
|
|
class TreeDelegate : public QStyledItemDelegate {
|
|
public:
|
|
TreeDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
|
|
|
|
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
|
|
const QModelIndex& index) const override {
|
|
// 根据需要创建编辑器,例如 QLineEdit
|
|
QLineEdit* editor = new QLineEdit(parent);
|
|
return editor;
|
|
}
|
|
|
|
void setEditorData(QWidget* editor, const QModelIndex& index) const override {
|
|
QString value = index.model()->data(index, Qt::EditRole).toString();
|
|
QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
|
|
lineEdit->setText(value);
|
|
}
|
|
|
|
void setModelData(QWidget* editor, QAbstractItemModel* model,
|
|
const QModelIndex& index) const override {
|
|
QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
|
|
model->setData(index, lineEdit->text(), Qt::EditRole);
|
|
}
|
|
|
|
void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
|
|
const QModelIndex& index) const override {
|
|
editor->setGeometry(option.rect);
|
|
}
|
|
};
|
|
|
|
class TreeView : public QTreeView{
|
|
public:
|
|
};
|
|
|
|
int main(int argc, char* argv[]) {
|
|
QApplication app(argc, argv);
|
|
// 定义表头
|
|
QStringList headers = {"字段1", "字段2", "字段3"};
|
|
// 创建模型并设置表头
|
|
TreeModel* model = new TreeModel(headers);
|
|
// 添加层次化数据
|
|
QStringList parentData = {"父节点1", "数据2", "数据3"};
|
|
model->addNode(parentData);
|
|
QModelIndex parentIndex = model->index(0, 0);
|
|
QStringList childData = {"子节点1", "数据2", "数据3"};
|
|
model->addNode(childData, parentIndex);
|
|
|
|
// 创建视图并设置模型
|
|
TreeView* treeView = new TreeView;
|
|
treeView->setModel(model);
|
|
|
|
// 设置自定义委托
|
|
TreeDelegate* delegate = new TreeDelegate;
|
|
treeView->setItemDelegate(delegate);
|
|
|
|
// 展开所有节点
|
|
treeView->expandAll();
|
|
|
|
// 显示窗口
|
|
QMainWindow mainWindow;
|
|
mainWindow.setCentralWidget(treeView);
|
|
mainWindow.resize(600, 400);
|
|
|
|
|
|
mainWindow.show();
|
|
|
|
return app.exec();
|
|
}
|
|
|
|
|
|
int main2(int argc, char *argv[]) {
|
|
QApplication app(argc, argv);
|
|
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
|
|
#ifdef _WINDOWS
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
system("chcp 65001");
|
|
//setbuf(stdout, 0);
|
|
#endif
|
|
QElapsedTimer timer;
|
|
timer.start();
|
|
// DemoGallery w;
|
|
// w.resize(1000, 600);
|
|
// w.show();
|
|
|
|
// YSG::Graphic g;
|
|
// g.resize(1000, 600);
|
|
// g.show();
|
|
|
|
// auto it = new YSG::SelectColorDialog();
|
|
// it->resize(1000, 600);
|
|
// it->show();
|
|
|
|
Tree_View w;
|
|
w.resize(400, 400);
|
|
w.show();
|
|
|
|
|
|
|
|
YSG::startAllRenderThread();
|
|
qDebug() << "启动耗时 " << timer.elapsed();
|
|
|
|
|
|
|
|
//f();
|
|
|
|
|
|
return QApplication::exec();
|
|
}
|