QFileDialog的简单了解

ps:写了点垃圾(哈哈哈)

现在感觉Qt库应该是调用了Windows提供的这块的接口了。

它继承自QDialog

这是Windows自己的文件夹

这是两者的对比图:

通过看QFileDialog的源码,来分析它是怎么实现这样的效果的。

源码组成:

qfiledialog.h

qfiledialog_p.h(它是实现也在qfiledialog.cpp中)

qfiledialog.cpp

qfiledialog.ui

(1)

左侧感觉是用QFileDialogTreeView实现的

(2)

左侧感觉是用QFileDialogListView实现的(可能吧,我的猜测)

(3)

这是用什么实现的呢?(不知道)

(4)

这个做的还挺好的

cpp 复制代码
void QFileDialogComboBox::paintEvent(QPaintEvent *)
{
    QStylePainter painter(this);
    painter.setPen(palette().color(QPalette::Text));

    // draw the combobox frame, focusrect and selected etc.
    QStyleOptionComboBox opt;
    initStyleOption(&opt);

    QRect editRect = style()->subControlRect(QStyle::CC_ComboBox, &opt,
                                                QStyle::SC_ComboBoxEditField, this);
    int size = editRect.width() - opt.iconSize.width() - 4;
    opt.currentText = opt.fontMetrics.elidedText(opt.currentText, Qt::ElideMiddle, size);
    painter.drawComplexControl(QStyle::CC_ComboBox, opt);

    // draw the icon and text
    painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}

(5)

使用到的Model:

QAbstractProxyModel *proxyModel;

QFileSystemModel *model;

(6)

发现了ui文件,这样前面的一些猜测都可以被验证是否正确了。

(7)

cpp 复制代码
    QObject::connect(qFileDialogUi->listView, SIGNAL(customContextMenuRequested(QPoint)),
                    q, SLOT(_q_showContextMenu(QPoint)));
cpp 复制代码
void QFileDialogPrivate::_q_showContextMenu(const QPoint &position)
{
#if !QT_CONFIG(menu)
    Q_UNUSED(position);
#else
    Q_Q(QFileDialog);
    QAbstractItemView *view = nullptr;
    if (q->viewMode() == QFileDialog::Detail)
        view = qFileDialogUi->treeView;
    else
        view = qFileDialogUi->listView;
    QModelIndex index = view->indexAt(position);
    index = mapToSource(index.sibling(index.row(), 0));

    QMenu menu(view);
    if (index.isValid()) {
        // file context menu
        const bool ro = model && model->isReadOnly();
        QFile::Permissions p(index.parent().data(QFileSystemModel::FilePermissions).toInt());
        renameAction->setEnabled(!ro && p & QFile::WriteUser);
        menu.addAction(renameAction);
        deleteAction->setEnabled(!ro && p & QFile::WriteUser);
        menu.addAction(deleteAction);
        menu.addSeparator();
    }
    menu.addAction(showHiddenAction);
    if (qFileDialogUi->newFolderButton->isVisible()) {
        newFolderAction->setEnabled(qFileDialogUi->newFolderButton->isEnabled());
        menu.addAction(newFolderAction);
    }
    menu.exec(view->viewport()->mapToGlobal(position));
#endif // QT_CONFIG(menu)
}
相关推荐
通信小呆呆4 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
H__Rick4 天前
自动对焦学习-3
人工智能·学习·计算机视觉
Daisy Lee4 天前
量化学习-第1章-什么是量化金融
学习·金融·datawhale
Alsn864 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
YM52e4 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨4 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Animation 动画效果实现-PC版本
学习·华为·harmonyos·鸿蒙
cqbzcsq4 天前
CellFlow虚拟细胞论文阅读
论文阅读·人工智能·笔记·学习·生物信息
YangYang9YangYan4 天前
2026初入职场学习数据分析的价值
学习·数据挖掘·数据分析
guslegend4 天前
理论学习:什么是 Coding Agent?
学习
自传.4 天前
尚硅谷 Vibe Coding|第三章(1) Claude Code深度使用与进阶技巧 学习笔记
笔记·学习·尚硅谷·vibecoding