Skip to main content

moregeek program

qt实现文件树-多极客编程

#include <QtCore/QUrl>
#include <QtCore/QVariant>
#include <QtXmlPatterns/QXmlNamePool>
#include "filetree.h"

FileTree::FileTree(const QXmlNamePool& pool)
: QSimpleXmlNodeModel(pool),
m_filterAllowAll(QDir::AllEntries |
QDir::AllDirs |
QDir::NoDotAndDotDot |
QDir::Hidden),
m_sortFlags(QDir::Name)
{
QXmlNamePool np = namePool();
m_names.resize(7);
m_names[File] = QXmlName(np, QLatin1String("file"));
m_names[Directory] = QXmlName(np, QLatin1String("directory"));
m_names[AttributeFileName] = QXmlName(np, QLatin1String("fileName"));
m_names[AttributeFilePath] = QXmlName(np, QLatin1String("filePath"));
m_names[AttributeSize] = QXmlName(np, QLatin1String("size"));
m_names[AttributeMIMEType] = QXmlName(np, QLatin1String("mimeType"));
m_names[AttributeSuffix] = QXmlName(np, QLatin1String("suffix"));
}

QXmlNodeModelIndex FileTree::nodeFor(const QString& dirName) const
{
QFileInfo dirInfo(QDir::cleanPath(dirName));
Q_ASSERT(dirInfo.exists());
return toNodeIndex(dirInfo);
}

const QFileInfo&
FileTree::toFileInfo(const QXmlNodeModelIndex &nodeIndex) const
{
return m_fileInfos.at(nodeIndex.data());
}

QXmlNodeModelIndex
FileTree::toNodeIndex(const QFileInfo &fileInfo, Type attributeName) const
{
const int indexOf = m_fileInfos.indexOf(fileInfo);

if (indexOf == -1) {
m_fileInfos.append(fileInfo);
return createIndex(m_fileInfos.count()-1, attributeName);
}
else
return createIndex(indexOf, attributeName);
}

QXmlNodeModelIndex FileTree::toNodeIndex(const QFileInfo &fileInfo) const
{
return toNodeIndex(fileInfo, fileInfo.isDir() ? Directory : File);
}

QXmlNodeModelIndex FileTree::nextSibling(const QXmlNodeModelIndex &nodeIndex,
const QFileInfo &fileInfo,
qint8 offset) const
{
Q_ASSERT(offset == -1 || offset == 1);

// Get the context node's parent.
const QXmlNodeModelIndex parent(nextFromSimpleAxis(Parent, nodeIndex));

if (parent.isNull())
return QXmlNodeModelIndex();

// Get the parent's child list.
const QFileInfo parentFI(toFileInfo(parent));
Q_ASSERT(Type(parent.additionalData()) == Directory);
const QFileInfoList siblings(QDir(parentFI.absoluteFilePath()).entryInfoList(QStringList(),
m_filterAllowAll,
m_sortFlags));
Q_ASSERT_X(!siblings.isEmpty(), Q_FUNC_INFO, "Can't happen! We started at a child.");

// Find the index of the child where we started.
const int indexOfMe = siblings.indexOf(fileInfo);

// Apply the offset.
const int siblingIndex = indexOfMe + offset;
if (siblingIndex < 0 || siblingIndex > siblings.count() - 1)
return QXmlNodeModelIndex();
else
return toNodeIndex(siblings.at(siblingIndex));
}
//! [5]


QXmlNodeModelIndex
FileTree::nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &nodeIndex) const
{
const QFileInfo fi(toFileInfo(nodeIndex));
const Type type = Type(nodeIndex.additionalData());

if (type != File && type != Directory) {
Q_ASSERT_X(axis == Parent, Q_FUNC_INFO, "An attribute only has a parent!");
return toNodeIndex(fi, Directory);
}

switch (axis) {
case Parent:
return toNodeIndex(QFileInfo(fi.path()), Directory);

case FirstChild:
{
if (type == File) // A file has no children.
return QXmlNodeModelIndex();
else {
Q_ASSERT(type == Directory);
Q_ASSERT_X(fi.isDir(), Q_FUNC_INFO, "It isn't really a directory!");
const QDir dir(fi.absoluteFilePath());
Q_ASSERT(dir.exists());

const QFileInfoList children(dir.entryInfoList(QStringList(),
m_filterAllowAll,
m_sortFlags));
if (children.isEmpty())
return QXmlNodeModelIndex();
const QFileInfo firstChild(children.first());
return toNodeIndex(firstChild);
}
}

case PreviousSibling:
return nextSibling(nodeIndex, fi, -1);

case NextSibling:
return nextSibling(nodeIndex, fi, 1);
}

Q_ASSERT_X(false, Q_FUNC_INFO, "Don't ever get here!");
return QXmlNodeModelIndex();
}

QUrl FileTree::documentUri(const QXmlNodeModelIndex &node) const
{
Q_UNUSED(node);
return QUrl("file:///");
}

QXmlNodeModelIndex::NodeKind
FileTree::kind(const QXmlNodeModelIndex &node) const
{
switch (Type(node.additionalData())) {
case Directory:
case File:
return QXmlNodeModelIndex::Element;
default:
return QXmlNodeModelIndex::Attribute;
}
}


QXmlNodeModelIndex::DocumentOrder
FileTree::compareOrder(const QXmlNodeModelIndex&,
const QXmlNodeModelIndex&) const
{
return QXmlNodeModelIndex::Is;
}

QXmlName FileTree::name(const QXmlNodeModelIndex &node) const
{
return m_names.at(node.additionalData());
}

QXmlNodeModelIndex FileTree::root(const QXmlNodeModelIndex &node) const
{
Q_UNUSED(node);
return toNodeIndex(QFileInfo(QLatin1String("/")));
}

QVariant FileTree::typedValue(const QXmlNodeModelIndex &node) const
{
const QFileInfo &fi = toFileInfo(node);

switch (Type(node.additionalData())) {
case Directory:
// deliberate fall through.
case File:
return QString();
case AttributeFileName:
return fi.fileName();
case AttributeFilePath:
return fi.filePath();
case AttributeSize:
return fi.size();
case AttributeMIMEType:
{
/* We don't have any MIME detection code currently, so return
* the most generic one. */
return QLatin1String("application/octet-stream");
}
case AttributeSuffix:
return fi.suffix();
}

Q_ASSERT_X(false, Q_FUNC_INFO, "This line should never be reached.");
return QString();
}


QVector<QXmlNodeModelIndex>
FileTree::attributes(const QXmlNodeModelIndex &element) const
{
QVector<QXmlNodeModelIndex> result;

/* Both elements has this attribute. */
const QFileInfo &forElement = toFileInfo(element);
result.append(toNodeIndex(forElement, AttributeFilePath));
result.append(toNodeIndex(forElement, AttributeFileName));

if (Type(element.additionalData() == File)) {
result.append(toNodeIndex(forElement, AttributeSize));
result.append(toNodeIndex(forElement, AttributeSuffix));
//result.append(toNodeIndex(forElement, AttributeMIMEType));
}
else {
Q_ASSERT(element.additionalData() == Directory);
}

return result;
}

©著作权归作者所有:来自51CTO博客作者五个板栗的原创作品,请联系作者获取转载授权,否则将追究法律责任

qt实现扇形图-多极客编程

#include "donutbreakdownchart.h"#include "mainslice.h"#include <QtCharts/QPieSlice>#include <QtCharts/QPieLegendMarker>QT_CHARTS_USE_NAMESPACEDonutBreakdownChart::DonutBreakdownChart(QGrap

【c语言】静态内存开辟₀、动态内存开辟¹、栈、堆。-多极客编程

👋静态内存开辟₀再说动态内存开辟的时候,我们先来介绍下我们已经学会的开辟内存空间的方法吧。char arr[10] = {0}; //在所处的栈上连续开辟10个字节的内存空间int a = 1; //在栈上开辟4个字节空间这些就是我们前面所学的知识点常用开辟内存空间的办法↓我们所开辟的内存空间大小是固定的。我们在申明数组的时候,必须要指定数组当中的长度,这样它所需要的内存空间才能被编译系统所知道。

【c语言】malloc 函数-多极客编程

那么这篇文章来介绍下动态内存开辟的函数之malloc()的使用,知道如何用了,那么就相当于学会了这个动态内存开辟。  👏malloc()函数malloc()函数的声明,如下所示↓void* malloc (size_t size);分配一个大小为字节的内存块,返回一个指向该块开头的指针。新分配的内存块的内容没有初始化,剩下不确定的值。如果size为0,则返回值取决于特定的库实现(它可能是一个空指针

c++——构造函数的使用注意事项及static用法-多极客编程

1.构造函数1.构造函数赋值和初始化列表#include<iostream>using namespace std;class date{public: date(int year = 1, int month = 1, int day = 1) { //函数体赋值 _year = year; _month = month; _day = day; }

stl库之集合基本使用方法-多极客编程

引言:集合是数学中的一个概念,集合是由一些不重复的数据组成的。在C++中我们常用的集合是set。一、引用库c++中set的实现在一个<set>头文件中,在代码开头引入这个头文件,并且同样加上一句using namespace std二、构造一个集合C++中直接构造一个set的语句是:set<int>a.这样我们就定义了一个名为a的、储存数据类型为int的集合(与vector

qt的qgroupbox-多极客编程

#include <QtWidgets>#include "window.h"Window::Window(QWidget *parent) : QWidget(parent){ QGridLayout *grid = new QGridLayout; grid->addWidget(createFirstExclusiveGroup(), 0, 0);

qt实现扇形图-多极客编程

#include "donutbreakdownchart.h"#include "mainslice.h"#include <QtCharts/QPieSlice>#include <QtCharts/QPieLegendMarker>QT_CHARTS_USE_NAMESPACEDonutBreakdownChart::DonutBreakdownChart(QGrap

python爬虫-第二章-2-函数-多极客编程

sorted - 排序 filter - 筛选 map - 映射# Demo Describe:常用内置函数 '''本章内容: sorted - 排序 sorted(可迭代数据类型,显示顺序,key=排序规则) filter - 筛选 filter(删选规则,可迭代数据类型) map - 映射 map(映射规则,可迭代数据类型)'''ls

【编程实践】提高工作效率,避免重复且枯燥的操作,利用python自动发送邮件-多极客编程

前言在实际的工作中,当我们需要向部门或者公司同事发送一封邮件时,总需要抄送多份,虽然现在发送邮件可以选中多个收件人或者多个可以把同事全部拉进一个QQ群发送群邮件的方式去解决同时向多人发送邮件,但是拉进一个群对于公司同事可能容易实现,但是需要向多个客户发送通知邮件,把所有客户拉近一个群不符合现实,且不容易操作,如果使用抄送的方式,好友太多,选择起来也是需要大量且耗时的操作,太废手了,所以这时候如果你

js:vue.js快速原型开发方案@vue/cli-多极客编程

(目录) 方式一:全局安装@vue/cli 文档 https://cli.vuejs.org/zh/guide/ npm install -g @vue/cli # 查看版本 vue --version # 创建项目 vue create hello-world 以上方式有一个缺点,需要全局安装命令行工具,而且安装依赖的时候有点慢,如果只是想简单快速的测试某个小功能,有点不划算。 虽然有

java线程中的wait、notify和notifyall解析-多极客编程

等待唤醒案例:线程间的通信 顾客去包子铺买包子,告知老板自身需求后,进入等待(调用wait()方法)老板处理的过程,此时顾客的状态为 WAITING,老板做好包子后,告知(调用notify()方法)顾客包子做好了。 💡线程间的通信的主要思想是生产者消费者机制。 代码实现 思路如下: 创建一个顾客线程(消费者):告知老板要的包子的种类和数量,调用wait方法,放弃CPU的执行,进入到WAITIN

c++——构造函数的使用注意事项及static用法-多极客编程

1.构造函数1.构造函数赋值和初始化列表#include<iostream>using namespace std;class date{public: date(int year = 1, int month = 1, int day = 1) { //函数体赋值 _year = year; _month = month; _day = day; }