基礎輸入
FileDropZone 檔案拖放區
接受本機檔案拖放,並在點擊瀏覽按鈕時請求應用程式開啟檔案選擇器。
範例
屬性預覽載入失敗。請再次點選屬性預覽重試。

用法
新增檔案
filesDropped 會提供拖放後的本機 URL;連接 browseRequested 以開啟應用程式的檔案選擇器,並由應用程式檢查檔案類型與大小。
#include <FluentQt/BasicInput.h>
using namespace fluent::basicinput;
auto* panel = new QWidget(this);
auto* layout = new QVBoxLayout(panel);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(24);
auto* drop = new FileDropZone(panel);
drop->setObjectName("fileDropZone");
drop->setDescription("Add documents, images or a ZIP archive.");
drop->setHintText("PDF, PNG, JPG, ZIP · Up to 20 MB per file");
layout->addWidget(drop);
auto* heading = new Label("Files", panel);
heading->setFluentTypography(Typography::FontRole::BodyStrong);
layout->addWidget(heading);
auto* model = new QStandardItemModel(panel);
auto addFile = [model](const QString& name, const QString& detail,
FileListView::Status state, qreal progress) {
auto* item = new QStandardItem(name);
item->setData(detail, FileListView::MetadataRole);
item->setData(state, FileListView::StatusRole);
item->setData(progress, FileListView::ProgressRole);
model->appendRow(item);
};
addFile("brand-guidelines.pdf", "2.4 MB · Ready to use", FileListView::Ready, 1.0);
addFile("product-shot.png", "3.6 / 5.8 MB · Uploading", FileListView::Uploading, 0.62);
addFile("archive.zip", "28 MB · Exceeds the 20 MB limit", FileListView::Rejected, 0.0);
auto* files = new FileListView(panel);
files->setObjectName("selectedFiles");
files->setAccessibleName("Selected files");
files->setBackgroundVisible(false);
files->setBorderVisible(false);
files->setProperty("fluentPreserveParentSurface", true);
files->viewport()->setProperty("fluentPreserveParentSurface", true);
files->setMinimumHeight(260);
files->setModel(model);
layout->addWidget(files);
QObject::connect(files, &FileListView::removeRequested, panel,
[model](const QModelIndex& index) { model->removeRow(index.row()); });
auto acceptFile = [drop, addFile](const QString& name, qint64 bytes) {
const QString suffix = QFileInfo(name).suffix().toLower();
const bool typeAllowed = QStringList{"pdf", "png", "jpg", "jpeg", "zip"}.contains(suffix);
const bool sizeAllowed = bytes <= 20 * 1024 * 1024;
if (!typeAllowed || !sizeAllowed) {
const QString reason = !typeAllowed ? "This file type is not supported."
: "Choose a file under 20 MB.";
drop->setErrorMessage(name + " · " + reason);
addFile(name, reason, FileListView::Rejected, 0.0);
return;
}
drop->clearError();
addFile(name, QString::number(bytes / (1024.0 * 1024.0), 'f', 1) + " MB · Ready to use",
FileListView::Ready, 1.0);
};
QObject::connect(drop, &FileDropZone::filesDropped, panel,
[acceptFile](const QList<QUrl>& urls) {
for (const QUrl& url : urls) {
const QFileInfo file(url.toLocalFile());
if (file.isFile())
acceptFile(file.fileName(), file.size());
}
});
QObject::connect(drop, &FileDropZone::browseRequested, panel, [panel, acceptFile]() {
platform::chooseFiles(panel, "Documents (*.pdf *.png *.jpg *.jpeg *.zip)", acceptFile);
});import fluentqt
from PySide6.QtCore import QFileInfo, Qt
from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QFileDialog, QVBoxLayout, QWidget
panel = QWidget()
layout = QVBoxLayout(panel)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(24)
model = QStandardItemModel(panel)
roles = fluentqt.FileListView.DataRole
states = fluentqt.FileListView.Status
def add_file(name, detail, state, progress):
item = QStandardItem(name)
item.setData(detail, roles.MetadataRole)
item.setData(state, roles.StatusRole)
item.setData(progress, roles.ProgressRole)
model.appendRow(item)
add_file("brand-guidelines.pdf", "2.4 MB · Ready to use", states.Ready, 1.0)
add_file("product-shot.png", "3.6 / 5.8 MB · Uploading", states.Uploading, 0.62)
add_file("archive.zip", "28 MB · Exceeds the 20 MB limit", states.Rejected, 0.0)
drop = fluentqt.FileDropZone(panel)
drop.setObjectName("fileDropZone")
drop.setDescription("Add documents, images or a ZIP archive.")
drop.setHintText("PDF, PNG, JPG, ZIP · Up to 20 MB per file")
layout.addWidget(drop)
heading = fluentqt.Label("Files", panel)
heading.setFluentTypography(fluentqt.FontRole.BodyStrong)
layout.addWidget(heading)
files = fluentqt.FileListView(panel)
files.setObjectName("selectedFiles")
files.setAccessibleName("Selected files")
files.setBackgroundVisible(False)
files.setBorderVisible(False)
files.setProperty("fluentPreserveParentSurface", True)
files.viewport().setProperty("fluentPreserveParentSurface", True)
files.setMinimumHeight(260)
files.setModel(model)
layout.addWidget(files)
files.removeRequested.connect(lambda index: model.removeRow(index.row()))
def accept_file(path):
file = QFileInfo(path)
if not file.isFile():
return
type_allowed = file.suffix().lower() in ("pdf", "png", "jpg", "jpeg", "zip")
size_allowed = file.size() <= 20 * 1024 * 1024
if not type_allowed or not size_allowed:
reason = "Choose a file under 20 MB."
if not type_allowed:
reason = "This file type is not supported."
drop.setErrorMessage(file.fileName() + " · " + reason)
add_file(file.fileName(), reason, states.Rejected, 0.0)
return
drop.clearError()
detail = f"{file.size() / (1024 * 1024):.1f} MB · Ready to use"
add_file(file.fileName(), detail, states.Ready, 1.0)
def dropped(urls):
for url in urls:
accept_file(url.toLocalFile())
def browse():
dialog = QFileDialog(panel, "Choose files")
dialog.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
dialog.setFileMode(QFileDialog.FileMode.ExistingFiles)
dialog.setNameFilter("Documents (*.pdf *.png *.jpg *.jpeg *.zip)")
dialog.filesSelected.connect(
lambda paths: [accept_file(path) for path in paths]
)
dialog.open()
panel._file_dialog = dialog
drop.filesDropped.connect(dropped)
drop.browseRequested.connect(browse)API
檢視宣告沒有符合的 API 成員。
fluent::basicinput::FileDropZone
繼承自 QWidget · FluentElement · QMLPlus
本機檔案拖放區域,附帶可開啟應用程式所提供檔案選擇器的按鈕。僅接受本機檔案(含遠端 URL 的拖曳會予以忽略);檔案對話方塊與 I/O 由應用程式處理。
建構函式
方法
QString hintText() const可選的格式或大小限制提示文字。
void setHintText(const QString& text)可選的格式或大小限制提示文字。
QString errorMessage() const驗證錯誤文字,在清除前會暫時取代說明文字;後續拖放不會自動清除。
void setErrorMessage(const QString& message)驗證錯誤文字,在清除前會暫時取代說明文字;後續拖放不會自動清除。
屬性
QString title- 讀取
title()設定setTitle()變更訊號titleChanged() QString description- 讀取
description()設定setDescription()變更訊號descriptionChanged() QString hintText可選的格式或大小限制提示文字。
讀取hintText()設定setHintText()變更訊號hintTextChanged()QString browseText- 讀取
browseText()設定setBrowseText()變更訊號browseTextChanged() QString errorMessage驗證錯誤文字,在清除前會暫時取代說明文字;後續拖放不會自動清除。
讀取errorMessage()設定setErrorMessage()變更訊號errorMessageChanged()bool dragActive- 讀取
isDragActive()變更訊號dragActiveChanged()
訊號
void hintTextChanged(const QString& text)可選的格式或大小限制提示文字。
void errorMessageChanged(const QString& message)驗證錯誤文字,在清除前會暫時取代說明文字;後續拖放不會自動清除。
void browseRequested()請求應用程式提供的檔案選擇器。
void filesDropped(const QList<QUrl>& urls)依原始順序發出拖放的本機檔案 URL 清單。
受保護成員
方法
繼承自 fluent::FluentElement
列舉
enum Theme解析設計 token 時使用的全域視覺佈景主題。
Light = 0Dark = 1HighContrast = 2
方法
static void setTheme(Theme theme)更新全域主題,並通知使用中的 FluentElement 執行個體。
static void setThemeDeferred(Theme theme)排程以可見元素優先的批次主題重新整理,避免阻斷目前的互動。
static Theme currentTheme()傳回目前生效的全域主題。
static bool themeUsesDarkAppearance(Theme theme)傳回主題是否使用深色底的視窗框架與材質 token。
static void refreshTheme()在設計 token 變更後重新廣播目前佈景主題以觸發重繪。
static int themeGeneration()每次主題變更時遞增的單調計數器,用於過期檢查。
Theme effectiveTheme() const解析此元素所使用的佈景主題,並遵循繼承的 QWidget 覆寫。
bool effectiveThemeUsesDarkAppearance() const傳回此元素的實際主題是否使用深色底 chrome。
bool setThemeOverrides(const QJsonObject& overrides)取代此元素的稀疏色彩、圓角與字型 token 覆寫。
QJsonObject themeOverrides() const傳回此元素的覆寫設定。
void clearThemeOverrides()將全部 token 欄位還原為全域主題預設值。
const Colors& themeColorsRef() const對目前使用中色組的不複製存取(適用於繪製熱路徑)。
Material::AcrylicToken themeAcrylic() const材質、高度層級與自適應版面配置 token。
QColor themeBackdrop(bool active) const指定啟用狀態下的視窗 chrome 背景色。
virtual void onThemeUpdated()在全域主題變更後呼叫。
型別與別名
struct Colors傳回給元件使用的設計 token 快照。
繼承自 fluent::QMLPlus
方法
AnchorLayout::Anchors* anchors()傳回 QWidget 宿主的可變錨點。
void setState(const QString& name)依名稱套用已註冊的狀態,或在 @p name 為空時還原為預設狀態。未知名稱會被忽略,離開該狀態後原有的繫結會恢復同步。
void addState(const QMLState& state)註冊或取代具名狀態定義;若取代目前啟用中的狀態則會立即套用。
void evaluateStates()依註冊順序重新評估狀態條件並啟用第一個符合的狀態,若無符合項目則恢復為預設狀態。相依屬性發出通知時也會自動執行;使用 extend 的衍生狀態會繼承上層變更,除非發生循環或缺失。
QMLPlusRuntime* runtime()傳回由此宿主延遲建立並擁有的狀態觀察器。
void setAnchors(const AnchorLayout::Anchors& anchors)取代所有宿主錨點,並使父層版面配置失效。
void resetAnchors()清除所有宿主錨點,並使父版面配置失效。
void bind(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction dir = PropertyBinder::OneWay)透過 PropertyBinder::bind() 將 @p source 上的 @p sourceProp 繫結至此 QWidget 宿主上的 @p targetProp。
BindingHandle bindManaged(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction direction = PropertyBinder::OneWay, BindingOptions options = {})在此 QWidget 宿主上安裝受管理的直接繫結。
BindingHandle bindComputed(const char* targetProp, const QVector<BindingDependency>& dependencies, std::function<QVariant()> evaluate, BindingOptions options = {})在此 QWidget 宿主上安裝使用明確相依項目的 C++ 運算式繫結。
void setBehavior(const char* propertyName, const PropertyAnimation& animation)設定透過 writeProperty() 或繫結寫入 @p propertyName 時使用的預設動畫。明確的 StateTransition 規則優先;直接呼叫 C++ setter 不會有動畫。
void clearBehavior(const char* propertyName)移除後續受管理寫入所使用的預設動畫。
bool writeProperty(const char* propertyName, const QVariant& value)透過 Behavior 寫入宿主屬性,且不移除該屬性的繫結。
Qt 基底類別文件:QWidget