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)
}
相关推荐
钟屿18 分钟前
Back to Basics: Let Denoising Generative Models Denoise 论文阅读学习
论文阅读·人工智能·笔记·学习·计算机视觉
d111111111d36 分钟前
SPI通信协议--在STM32中介绍(学习笔记)
笔记·stm32·单片机·嵌入式硬件·学习
断水客1 小时前
如何在手机上搭建Linux学习环境
linux·运维·学习
j***12152 小时前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
爬虫·学习·selenium
✎ ﹏梦醒͜ღ҉繁华落℘3 小时前
freeRTOS学习笔记(十四)--内存
笔记·学习
又是忙碌的一天3 小时前
mysql 学习第二天 SQL语句
sql·学习·mysql
拼好饭和她皆失4 小时前
C#学习入门
开发语言·学习·c#
LFly_ice4 小时前
学习React-22-Zustand
前端·学习·react.js
q***3755 小时前
爬虫学习 01 Web Scraper的使用
前端·爬虫·学习
车端域控测试工程师6 小时前
Autosar网络管理测试用例 - TC003
c语言·开发语言·学习·汽车·测试用例·capl·canoe