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)
}
相关推荐
是孑然呀13 分钟前
【小记】word批量生成准考证
笔记·学习·excel
ll7788113 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
LuckyLay4 小时前
React百日学习计划——Deepseek版
前端·学习·react.js
安和昂4 小时前
【iOS】SDWebImage源码学习
学习·ios
菜一头包4 小时前
c++ std库中的文件操作学习笔记
c++·笔记·学习
猴子请来的逗比4894 小时前
tomcat搭建内网论坛
学习·tomcat
belldeep5 小时前
如何阅读、学习 Git 核心源代码 ?
git·学习·源代码
Kazefuku5 小时前
python文件打包成exe文件
python·学习
threelab5 小时前
08.webgl_buffergeometry_attributes_none ,three官方示例+编辑器+AI快速学习
学习
嵌入式仿真实验教学平台6 小时前
「国产嵌入式仿真平台:高精度虚实融合如何终结Proteus时代?」——从教学实验到低空经济,揭秘新一代AI赋能的产业级教学工具
人工智能·学习·proteus·无人机·低空经济·嵌入式仿真·实验教学