Modern File System Explorer
A QML app utilizing customized Qt Quick Controls to display text files from a filesystem.
In this example, a modern layout is used that consists of three major components. There is an icon-based Sidebar to the left, followed by a resizable TreeView displaying the file system from a QFileSystemModel, and finally the TextArea displaying the selected text files. There is a common look and feel across all operating systems. We accomplish this by using customized quick controls and frameless windows, with our own window decorations.

Running the Example
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Modern layout and colors
To begin with, we are providing the colors throughout a singleton QML object. In this way, we can provide more structured control over the appearance of the application.
pragma Singleton import QtQuick QtObject { readonly property color background: "#23272E" readonly property color surface1: "#1E2227" readonly property color surface2: "#090A0C" readonly property color text: "#ABB2BF" readonly property color textFile: "#C5CAD3" readonly property color disabledText: "#454D5F" readonly property color selection: "#2C313A" readonly property color active: "#23272E" readonly property color inactive: "#3E4452" readonly property color folder: "#3D4451" readonly property color icon: "#3D4451" readonly property color iconIndicator: "#E5C07B" readonly property color color1: "#E06B74" readonly property color color2: "#62AEEF" }
Since we do not want to rely on the operating system's window decoration and instead want to provide our own, we use the FramelessWindowHint flag inside the ApplicationWindow. In order to achieve an equivalent interaction with the window, we override the background property of our customized MenuBar and display some information text as well as interaction possibilities for dragging or closing the application. Inline Components have been used to simplify this process.
background: Rectangle {
color: Colors.surface2
// Make the empty space drag the specified root window.
WindowDragHandler { dragWindow: rootWindow }
Text {
id: windowInfo
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
color: Colors.text
}
component InteractionButton: Rectangle {
signal action;
property alias hovered: hoverHandler.hovered
width: root.height
anchors.top: parent.top
anchors.bottom: parent.bottom
color: hovered ? Colors.background : "transparent"
HoverHandler { id: hoverHandler }
TapHandler { onTapped: action() }
}
The Sidebar on the left includes checkable navigation buttons on top and one-shot buttons on the bottom. A ButtonGroup and a Container are used to ensure that only one entry is active at any given time. It is then possible to provide different views using a property alias for the current position, along with a StackLayout.
This technique allows us to simply extend the functionality by adding another button and the corresponding element inside the StackLayout.
StackLayout {
currentIndex: sidebar.currentTabIndex
anchors.fill: parent
// Shows the help text.
MyTextArea {
readOnly: true
text: qsTr("This example shows how to use and visualize the file system.\n\n"
+ "Customized Qt Quick Components have been used to achieve this look.\n\n"
+ "You can edit the files but they won't be changed on the file system.\n\n"
+ "Click on the folder icon to the left to get started.")
wrapMode: TextArea.Wrap
}
// Shows the files on the file system.
FileSystemView {
id: fileSystemView
color: Colors.surface1
onFileClicked: (path) => root.currentFilePath = path
}
}
The StackLayout includes, besides some simple information text, the FileSystemView. This custom component displays files and folders and populates it with data from a C++ model. We can then select the files and read them accordingly.
QString FileSystemModel::readFile(const QString &filePath) { // Don't issue errors for an empty path, as the initial binding // will result in an empty path, and that's OK. if (filePath.isEmpty()) return {}; QFile file(filePath); if (file.size() >= 2'000'000) return tr("File size is too big.\nYou can read files up to %1 MB.").arg(2); static const QMimeDatabase db; const QMimeType mime = db.mimeTypeForFile(QFileInfo(file)); // Check if the mimetype is supported and return the content. const auto mimeTypesForFile = mime.parentMimeTypes(); for (const auto &m : mimeTypesForFile) { if (m.contains("text", Qt::CaseInsensitive) || mime.comment().contains("text", Qt::CaseInsensitive)) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return tr("Error opening the File!"); QTextStream stream(&file); return stream.readAll(); } } return tr("Filetype not supported!"); }
By using a SplitView, we are able to dynamically share the space between the StackLayout and the ScrollView. Our ScrollView contains an embedded TextArea that displays the opened file. Using this method, we can resize the view vertically and enable scrolling within the TextArea.
...
ScrollView {
leftPadding: 20
topPadding: 20
bottomPadding: 20
clip: true
SplitView.fillWidth: true
SplitView.fillHeight: true
property alias textArea: textArea
MyTextArea {
id: textArea
text: FileSystemModel.readFile(root.currentFilePath)
}
}
Custom components
For a better understanding of the customization process, investigate this article first. We are using reusable and customized components throughout this example.
For example, the MyMenu component customizes Menu's background property as well as its delegates' contentItem and background properties.
// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick import QtQuick.Controls.Basic import FileSystemModule Menu { id: root background: Rectangle { implicitWidth: 200 implicitHeight: 40 color: Colors.surface2 } delegate: MenuItem { id: menuItem implicitWidth: 200 implicitHeight: 40 contentItem: Item { Text { anchors.verticalCenter: parent.verticalCenter anchors.left: parent.left anchors.leftMargin: 5 text: menuItem.text color: enabled ? Colors.text : Colors.disabledText } Rectangle { anchors.verticalCenter: parent.verticalCenter anchors.right: parent.right width: 6 height: parent.height visible: menuItem.highlighted color: Colors.color2 } } background: Rectangle { color: menuItem.highlighted ? Colors.active : "transparent" } } }
Another example is the customization of the ScrollIndicator inside the FileSystemView, which additionally uses customized animations. Here we also override the contentItem.
ScrollIndicator.vertical: ScrollIndicator {
active: true
implicitWidth: 15
contentItem: Rectangle {
implicitWidth: 6
implicitHeight: 6
color: Colors.color1
opacity: fileSystemTreeView.movingVertically ? 0.5 : 0.0
Behavior on opacity {
OpacityAnimator {
duration: 500
}
}
}
}