Qt学习笔记之实现简单文件浏览器

#-------------------------------------------------
#
# Project created by QtCreator 2020-02-20T20:09:24
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = myGui6
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

RESOURCES += \
    res/myres.qrc
#include "widget.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //! 加载样式表
    QFile file(":/qss/psblack.css");
    if (file.open(QFile::ReadOnly)) {
        QString qss = QLatin1String(file.readAll());
        qApp->setStyleSheet(qss);
        file.close();
    }
    Widget w;
    w.show();

    return a.exec();
}
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtNetwork>
#include <QLabel>
#include <QTextBrowser>
#include <QLayout>
#include <QTextCodec>
#include <QDebug>
#include <QTreeWidget>
#include <QTreeWidgetItem>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private:

private slots:


};

#endif // WIDGET_H
#include "widget.h"
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QDir>
#include <QVector>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout* horizontalLayout_4 = new QHBoxLayout(this);
    horizontalLayout_4->setObjectName(QStringLiteral("horizontalLayout_4"));
    QTreeWidget *treeWidget = new QTreeWidget();
    QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
    __qtreewidgetitem->setText(0, QStringLiteral("1"));
    treeWidget->setHeaderItem(__qtreewidgetitem);
    treeWidget->setObjectName(QStringLiteral("treeWidget"));

    horizontalLayout_4->addWidget(treeWidget);

    treeWidget->clear();
    treeWidget->setHeaderLabel("文件浏览器");

    QFileInfoList fileInfoList = QDir::drives();
    for (int i = 0; i < fileInfoList.size();i++){
        QString str = fileInfoList[i].absoluteFilePath();
        QStringList strList =  str.split(":");
        QTreeWidgetItem *group = new QTreeWidgetItem(treeWidget);
        group->setText(0, strList[0]);
        QDir dir;
        dir.setPath(fileInfoList[i].absoluteFilePath());
        //! 打出文件夹下文件
        QStringList fileList = dir.entryList();
        for (int i = 0; i < fileList.size(); i++){
            //qDebug()<<fileList[i];
            QTreeWidgetItem *subItem11 = new QTreeWidgetItem(group);
            subItem11->setText(0, fileList[i]);
        }
    }
}

Widget::~Widget()
{

}

猜你喜欢

转载自blog.csdn.net/a8039974/article/details/105083237