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)
}
相关推荐
吃好睡好便好3 小时前
泰戈尔的诗歌7
学习·生活
-To be number.wan3 小时前
数据库系统 | 规范化理论
数据库·学习
星夜夏空994 小时前
C++学习(2) —— 类与对象基础
开发语言·c++·学习
-To be number.wan4 小时前
数据库系统 | 数据库安全与完整性
数据库·学习
czysoft4 小时前
se被限速
科技·学习·it·技术·魔法·先进·领先
子不语1805 小时前
从0开始学习S7-1200+ET200SP(3)——两台S7-1200通过TCP连接
网络协议·学习·tcp/ip
llllliznc6 小时前
LLM 学习笔记 Day 5:Agent 核心组件——Planner、Memory 与 Reflection
笔记·学习
hyhsandy18036 小时前
STM32F103 TIM学习笔记
笔记·stm32·学习
GuHenryCheng6 小时前
【ESP32】ESP-IDF开发环境搭建(cursor)
git·stm32·单片机·学习
编程圈子7 小时前
电机驱动开发学习18. SVPWM空间矢量调制算法详解与实现
驱动开发·学习·算法