美文网首页
美化QFileDialog

美化QFileDialog

作者: 雨田哥工作号 | 来源:发表于2018-10-31 20:05 被阅读0次

    效果图

    在这里插入图片描述

    #简述

    在看Qt QFileDialog源码的时候,发现Qt自己做了一套文件管理框,UI如下;反之,Qt自己做了一套自己的UI文件,那么我们就能美化它。


    在这里插入图片描述

    功能

    1.美化QFileDialog
    2.支持QFileDialog的都支持
    3.支持文件和文件夹多选

    思路

    自定义一个Dialog,UI如下


    在这里插入图片描述

    代码篇

    CustomFileDialog::CustomFileDialog(QWidget *parent)
        : QDialog(parent)
    {
        ui.setupUi(this);
        //设置QFileDialog 透明,无边框,子窗口属性
        m_fileDialog = new QFileDialog(this, Qt::SubWindow | Qt::FramelessWindowHint);
        m_fileDialog->setAttribute(Qt::WA_TranslucentBackground);
        //使用Qt的文件选项框
        m_fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
        //QFileDialog 添加到自定义UI中
        ui.bodylayout->addWidget(m_fileDialog);
    
        this->setAttribute(Qt::WA_TranslucentBackground);
        this->setWindowFlags(Qt::FramelessWindowHint);
        setTitleName(QString::fromLocal8Bit("文件选择框"));
    
        //支持多选,即支持文件和文件夹选中
        QListView *pListView = m_fileDialog->findChild<QListView*>("listView");
        if (pListView){
            pListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
            pListView->setItemDelegate(new NoFocusDelegate(pListView));
        }
        QTreeView *pTreeView = m_fileDialog->findChild<QTreeView*>("treeView");
        if (pTreeView){
            pTreeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
            pTreeView->setItemDelegate(new NoFocusDelegate(pTreeView));
            pTreeView->header()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        }
        QLabel* lookinLabel = m_fileDialog->findChild<QLabel*>("lookInLabel");
        if (lookinLabel)
            lookinLabel->setText(QString::fromLocal8Bit("文件目录:"));
    
        QComboBox* fileTypeCombo = m_fileDialog->findChild<QComboBox*>("fileTypeCombo");
        if (fileTypeCombo)
            fileTypeCombo->setMinimumHeight(24);
        QComboBox* lookInCombo = m_fileDialog->findChild<QComboBox*>("lookInCombo");
        if (lookInCombo)
            lookInCombo->setMinimumHeight(24); 
        QLineEdit* fileNameEdit = m_fileDialog->findChild<QLineEdit*>("fileNameEdit");
        if (fileNameEdit)
            fileNameEdit->setMinimumHeight(24);
    
        QDialogButtonBox *buttonBox = m_fileDialog->findChild<QDialogButtonBox *>("buttonBox");
        if (QPushButton *button = buttonBox->button(QDialogButtonBox::Open)){
            button->setStyleSheet("QPushButton{background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #00BAFF, stop: 1 #00A1FF);font-size: 12px;color: #FFFFFF;border-radius:2px;}"
                                  "QPushButton:hover{background: #00C1FF;font-size: 12px;color: #FFFFFF;border-radius:2px;}"
                                  "QPushButton:disabled{background: #F0F0F0;border: 1px solid #DDDDDD;font-size: 12px;color: #BBBBBB;border-radius:2px;}");
            button->setFixedSize(68, 24);
        }
        if (QPushButton *button = buttonBox->button(QDialogButtonBox::Save)){
            button->setStyleSheet("QPushButton{background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #00BAFF, stop: 1 #00A1FF);font-size: 12px;color: #FFFFFF;border-radius:2px;}"
                                  "QPushButton:hover{background: #00C1FF;font-size: 12px;color: #FFFFFF;border-radius:2px;}"
                                  "QPushButton:disabled{background: #F0F0F0;border: 1px solid #DDDDDD;font-size: 12px;color: #BBBBBB;border-radius:2px;}");
            button->setFixedSize(68, 24);
        }
        if (QPushButton *button = buttonBox->button(QDialogButtonBox::Cancel)){
            button->setStyleSheet("QPushButton{border: 1px solid #DDDDDD;font-size: 12px;color: #666666;border-radius:2px;}"
                                  "QPushButton:hover{font-size: 12px;color: #00A1FF;border: 1px solid #00A1FF;border-radius:2px;}"
                                  "QPushButton:disabled{background: #F0F0F0;border: 1px solid #DDDDDD;font-size: 12px;color: #BBBBBB;border-radius:2px;}");
            button->setText(QString::fromLocal8Bit("取消"));
            button->setFixedSize(68, 24);
        }
    
        buttonBox->disconnect();
        connect(buttonBox, SIGNAL(accepted()), this, SLOT(onAccepted()));//改成自己的槽
        connect(buttonBox, SIGNAL(rejected()), this, SLOT(onRejected()));//改成自己的槽
    
        connect(ui.sysMin, SIGNAL(clicked(bool)), this, SLOT(onShowMin(bool)));
        connect(ui.sysClose, SIGNAL(clicked(bool)), this, SLOT(onShowClose(bool)));
    }
    

    工程文件

    Qt交流大会 853086607 收费群中


    在这里插入图片描述

    结尾

    不定期上传新作品,解答作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界!

    相关文章

      网友评论

          本文标题:美化QFileDialog

          本文链接:https://www.haomeiwen.com/subject/tulztqtx.html